File indexing completed on 2025-01-26 05:25:27

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Service_Rackspace
0018  * @subpackage Files
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 // require_once 'Zend/Service/Rackspace/Files.php';
0024 
0025 class Zend_Service_Rackspace_Files_Container
0026 {
0027     const ERROR_PARAM_FILE_CONSTRUCT = 'The Zend_Service_Rackspace_Files passed in construction is not valid';
0028 
0029     const ERROR_PARAM_ARRAY_CONSTRUCT = 'The array passed in construction is not valid';
0030 
0031     const ERROR_PARAM_NO_NAME = 'The container name is empty';
0032 
0033     /**
0034      * @var string
0035      */
0036     protected $name;
0037 
0038     /**
0039      * Construct
0040      *
0041      * @param Zend_Service_Rackspace_Files $service
0042      * @param                              $data
0043      *
0044      * @throws Zend_Service_Rackspace_Files_Exception
0045      */
0046     public function __construct($service, $data)
0047     {
0048         if (!($service instanceof Zend_Service_Rackspace_Files)) {
0049             // require_once 'Zend/Service/Rackspace/Files/Exception.php';
0050             throw new Zend_Service_Rackspace_Files_Exception(
0051                 self::ERROR_PARAM_FILE_CONSTRUCT
0052             );
0053         }
0054         if (!is_array($data)) {
0055             // require_once 'Zend/Service/Rackspace/Files/Exception.php';
0056             throw new Zend_Service_Rackspace_Files_Exception(
0057                 self::ERROR_PARAM_ARRAY_CONSTRUCT
0058             );
0059         }
0060         if (!array_key_exists('name', $data)) {
0061             // require_once 'Zend/Service/Rackspace/Files/Exception.php';
0062             throw new Zend_Service_Rackspace_Files_Exception(
0063                 self::ERROR_PARAM_NO_NAME
0064             );
0065         }
0066         $this->service = $service;
0067         $this->name    = $data['name'];
0068     }
0069 
0070     /**
0071      * Get the name of the container
0072      *
0073      * @return string
0074      */
0075     public function getName()
0076     {
0077         return $this->name;
0078     }
0079 
0080     /**
0081      * Get the size in bytes of the container
0082      *
0083      * @return integer|bool
0084      */
0085     public function getSize()
0086     {
0087         $data = $this->getInfo();
0088         if (isset($data['bytes'])) {
0089             return $data['bytes'];
0090         }
0091 
0092         return false;
0093     }
0094 
0095     /**
0096      * Get the total count of objects in the container
0097      *
0098      * @return integer|bool
0099      */
0100     public function getObjectCount()
0101     {
0102         $data = $this->getInfo();
0103         if (isset($data['count'])) {
0104             return $data['count'];
0105         }
0106 
0107         return false;
0108     }
0109 
0110     /**
0111      * Return true if the container is CDN enabled
0112      *
0113      * @return bool
0114      */
0115     public function isCdnEnabled()
0116     {
0117         $data = $this->getCdnInfo();
0118         if (isset($data['cdn_enabled'])) {
0119             return $data['cdn_enabled'];
0120         }
0121 
0122         return false;
0123     }
0124 
0125     /**
0126      * Get the TTL of the CDN
0127      *
0128      * @return integer|bool
0129      */
0130     public function getCdnTtl()
0131     {
0132         $data = $this->getCdnInfo();
0133         if (isset($data['ttl'])) {
0134             return $data['ttl'];
0135         }
0136 
0137         return false;
0138     }
0139 
0140     /**
0141      * Return true if the log retention is enabled for the CDN
0142      *
0143      * @return bool
0144      */
0145     public function isCdnLogEnabled()
0146     {
0147         $data = $this->getCdnInfo();
0148         if (isset($data['log_retention'])) {
0149             return $data['log_retention'];
0150         }
0151 
0152         return false;
0153     }
0154 
0155     /**
0156      * Get the CDN URI
0157      *
0158      * @return string|bool
0159      */
0160     public function getCdnUri()
0161     {
0162         $data = $this->getCdnInfo();
0163         if (isset($data['cdn_uri'])) {
0164             return $data['cdn_uri'];
0165         }
0166 
0167         return false;
0168     }
0169 
0170     /**
0171      * Get the CDN URI SSL
0172      *
0173      * @return string|bool
0174      */
0175     public function getCdnUriSsl()
0176     {
0177         $data = $this->getCdnInfo();
0178         if (isset($data['cdn_uri_ssl'])) {
0179             return $data['cdn_uri_ssl'];
0180         }
0181 
0182         return false;
0183     }
0184 
0185     /**
0186      * Get the metadata of the container
0187      *
0188      * If $key is empty return the array of metadata
0189      *
0190      * @param string $key
0191      *
0192      * @return array|string|bool
0193      */
0194     public function getMetadata($key = null)
0195     {
0196         $result = $this->service->getMetadataContainer($this->getName());
0197         if (!empty($result) && is_array($result)) {
0198             if (empty($key)) {
0199                 return $result['metadata'];
0200             } else {
0201                 if (isset ($result['metadata'][$key])) {
0202                     return $result['metadata'][$key];
0203                 }
0204             }
0205         }
0206 
0207         return false;
0208     }
0209 
0210     /**
0211      * Get the information of the container (total of objects, total size)
0212      *
0213      * @return array|bool
0214      */
0215     public function getInfo()
0216     {
0217         $result = $this->service->getMetadataContainer($this->getName());
0218         if (!empty($result) && is_array($result)) {
0219             return $result;
0220         }
0221 
0222         return false;
0223     }
0224 
0225     /**
0226      * Get all the object of the container
0227      *
0228      * @return Zend_Service_Rackspace_Files_ObjectList
0229      */
0230     public function getObjects()
0231     {
0232         return $this->service->getObjects($this->getName());
0233     }
0234 
0235     /**
0236      * Get an object of the container
0237      *
0238      * @param string $name
0239      * @param array  $headers
0240      *
0241      * @return Zend_Service_Rackspace_Files_Object|bool
0242      */
0243     public function getObject($name, $headers = array())
0244     {
0245         return $this->service->getObject($this->getName(), $name, $headers);
0246     }
0247 
0248     /**
0249      * Add an object in the container
0250      *
0251      * @param string $name
0252      * @param string $file the content of the object
0253      * @param array  $metadata
0254      *
0255      * @return bool
0256      */
0257     public function addObject($name, $file, $metadata = array())
0258     {
0259         return $this->service->storeObject(
0260             $this->getName(), $name, $file, $metadata
0261         );
0262     }
0263 
0264     /**
0265      * Delete an object in the container
0266      *
0267      * @param string $obj
0268      *
0269      * @return bool
0270      */
0271     public function deleteObject($obj)
0272     {
0273         return $this->service->deleteObject($this->getName(), $obj);
0274     }
0275 
0276     /**
0277      * Copy an object to another container
0278      *
0279      * @param string $obj_source
0280      * @param string $container_dest
0281      * @param string $obj_dest
0282      * @param array  $metadata
0283      * @param string $content_type
0284      *
0285      * @return bool
0286      */
0287     public function copyObject(
0288         $obj_source, $container_dest, $obj_dest, $metadata = array(),
0289         $content_type = null
0290     )
0291     {
0292         return $this->service->copyObject(
0293             $this->getName(),
0294             $obj_source,
0295             $container_dest,
0296             $obj_dest,
0297             $metadata,
0298             $content_type
0299         );
0300     }
0301 
0302     /**
0303      * Get the metadata of an object in the container
0304      *
0305      * @param string $object
0306      *
0307      * @return array
0308      */
0309     public function getMetadataObject($object)
0310     {
0311         return $this->service->getMetadataObject($this->getName(), $object);
0312     }
0313 
0314     /**
0315      * Set the metadata of an object in the container
0316      *
0317      * @param string $object
0318      * @param array  $metadata
0319      *
0320      * @return bool
0321      */
0322     public function setMetadataObject($object, $metadata = array())
0323     {
0324         return $this->service->setMetadataObject(
0325             $this->getName(), $object, $metadata
0326         );
0327     }
0328 
0329     /**
0330      * Enable the CDN for the container
0331      *
0332      * @param integer $ttl
0333      *
0334      * @return array|bool
0335      */
0336     public function enableCdn($ttl = Zend_Service_Rackspace_Files::CDN_TTL_MIN)
0337     {
0338         return $this->service->enableCdnContainer($this->getName(), $ttl);
0339     }
0340 
0341     /**
0342      * Disable the CDN for the container
0343      *
0344      * @return bool
0345      */
0346     public function disableCdn()
0347     {
0348         $result =
0349             $this->service->updateCdnContainer($this->getName(), null, false);
0350 
0351         return ($result !== false);
0352     }
0353 
0354     /**
0355      * Change the TTL for the CDN container
0356      *
0357      * @param integer $ttl
0358      *
0359      * @return bool
0360      */
0361     public function changeTtlCdn($ttl)
0362     {
0363         $result = $this->service->updateCdnContainer($this->getName(), $ttl);
0364 
0365         return ($result !== false);
0366     }
0367 
0368     /**
0369      * Enable the log retention for the CDN
0370      *
0371      * @return bool
0372      */
0373     public function enableLogCdn()
0374     {
0375         $result = $this->service->updateCdnContainer(
0376             $this->getName(), null, null, true
0377         );
0378 
0379         return ($result !== false);
0380     }
0381 
0382     /**
0383      * Disable the log retention for the CDN
0384      *
0385      * @return bool
0386      */
0387     public function disableLogCdn()
0388     {
0389         $result = $this->service->updateCdnContainer(
0390             $this->getName(), null, null, false
0391         );
0392 
0393         return ($result !== false);
0394     }
0395 
0396     /**
0397      * Get the CDN information
0398      *
0399      * @return array|bool
0400      */
0401     public function getCdnInfo()
0402     {
0403         return $this->service->getInfoCdnContainer($this->getName());
0404     }
0405 }