      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\ObjectStore\Resource;
     19:
     20: use OpenCloud\Common\Exceptions;
     21: use OpenCloud\Common\Service\ServiceInterface;
     22: use OpenCloud\ObjectStore\Constants\Header as HeaderConst;
     23:
     24: /**
     25:  * Abstract class holding shared functionality for containers.
     26:  */
     27: abstract class AbstractContainer extends AbstractResource
     28: {
     29:     protected $metadataClass = 'OpenCloud\\ObjectStore\\Resource\\ContainerMetadata';
     30:
     31:     /**
     32:      * The name of the container.
     33:      *
     34:      * The only restrictions on container names is that they cannot contain a
     35:      * forward slash (/) and must be less than 256 bytes in length. Please note
     36:      * that the length restriction applies to the name after it has been URL
     37:      * encoded. For example, a container named Course Docs would be URL encoded
     38:      * as Course%20Docs - which is 13 bytes in length rather than the expected 11.
     39:      *
     40:      * @var string
     41:      */
     42:     public $name;
     43:
     44:     public function __construct(ServiceInterface $service, $data = null)
     45:     {
     46:         $this->service = $service;
     47:         $this->metadata = new $this->metadataClass;
     48:
     49:         // Populate data if set
     50:         $this->populate($data);
     51:     }
     52:
     53:     public function getTransId()
     54:     {
     55:         return $this->metadata->getProperty(HeaderConst::TRANS_ID);
     56:     }
     57:
     58:     abstract public function isCdnEnabled();
     59:
     60:     public function hasLogRetention()
     61:     {
     62:         if ($this instanceof CDNContainer) {
     63:             return $this->metadata->getProperty(HeaderConst::LOG_RETENTION) == 'True';
     64:         } else {
     65:             return $this->metadata->propertyExists(HeaderConst::ACCESS_LOGS);
     66:         }
     67:     }
     68:
     69:     public function primaryKeyField()
     70:     {
     71:         return 'name';
     72:     }
     73:
     74:     public function getUrl($path = null, array $params = array())
     75:     {
     76:         if (strlen($this->getName()) == 0) {
     77:             throw new Exceptions\NoNameError('Container does not have a name');
     78:         }
     79:
     80:         $url = $this->getService()->getUrl();
     81:
     82:         return $url->addPath((string) $this->getName())->addPath((string) $path)->setQuery($params);
     83:     }
     84:
     85:     protected function createRefreshRequest()
     86:     {
     87:         return $this->getClient()->head($this->getUrl(), array('Accept' => '*/*'));
     88:     }
     89:
     90:     /**
     91:      * This method will enable your CDN-enabled container to serve out HTML content like a website.
     92:      *
     93:      * @param $indexPage The data object name (i.e. a .html file) that will serve as the main index page.
     94:      * @return \Guzzle\Http\Message\Response
     95:      */
     96:     public function setStaticIndexPage($page)
     97:     {
     98:         if ($this instanceof CDNContainer) {
     99:             $this->getLogger()->warning(
    100:                 'This method cannot be called on the CDN object - please execute it on the normal Container'
    101:             );
    102:         }
    103:
    104:         $headers = array('X-Container-Meta-Web-Index' => $page);
    105:
    106:         return $this->getClient()->post($this->getUrl(), $headers)->send();
    107:     }
    108:
    109:     /**
    110:      * Set the default error page for your static site.
    111:      *
    112:      * @param $name The data object name (i.e. a .html file) that will serve as the main error page.
    113:      * @return \Guzzle\Http\Message\Response
    114:      */
    115:     public function setStaticErrorPage($page)
    116:     {
    117:         if ($this instanceof CDNContainer) {
    118:             $this->getLogger()->warning(
    119:                 'This method cannot be called on the CDN object - please execute it on the normal Container'
    120:             );
    121:         }
    122:
    123:         $headers = array('X-Container-Meta-Web-Error' => $page);
    124:
    125:         return $this->getClient()->post($this->getUrl(), $headers)->send();
    126:     }
    127: }
    128: