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: /**
54: * Return the transaction ID for an HTTP API operation. Useful for debugging.
55: *
56: * @return string Transaction ID
57: */
58: public function getTransId()
59: {
60: return $this->metadata->getProperty(HeaderConst::TRANS_ID);
61: }
62:
63: /**
64: * Returns whether this container is CDN-enabled or not.
65: *
66: * @return boolean true if this container is CDN-enabled; false, otherwise.
67: */
68: abstract public function isCdnEnabled();
69:
70: /**
71: * Returns whether this container has log retention enabled or not.
72: *
73: * @return boolean true if this container has log retention enabled; false, otherwise.
74: */
75: public function hasLogRetention()
76: {
77: if ($this instanceof CDNContainer) {
78: return $this->metadata->getProperty(HeaderConst::LOG_RETENTION) == 'True';
79: } else {
80: return $this->metadata->propertyExists(HeaderConst::ACCESS_LOGS);
81: }
82: }
83:
84: /**
85: * For internal use only
86: *
87: * @return string Name of the primary key field for this resource
88: */
89: public function primaryKeyField()
90: {
91: return 'name';
92: }
93:
94: /**
95: * For internal use only
96: *
97: * @param string $path Path to add to URL. Optional.
98: * @param array $params Query parameters to add to URL. Optional.
99: * @return Url URL of this container + path + query parameters.
100: */
101: public function getUrl($path = null, array $params = array())
102: {
103: if (strlen($this->getName()) == 0) {
104: throw new Exceptions\NoNameError('Container does not have a name');
105: }
106:
107: $url = $this->getService()->getUrl();
108:
109: return $url->addPath((string) $this->getName())->addPath((string) $path)->setQuery($params);
110: }
111:
112: protected function createRefreshRequest()
113: {
114: return $this->getClient()->head($this->getUrl(), array('Accept' => '*/*'));
115: }
116:
117: /**
118: * This method will enable your CDN-enabled container to serve out HTML content like a website.
119: *
120: * @param $indexPage The data object name (i.e. a .html file) that will serve as the main index page.
121: * @return \Guzzle\Http\Message\Response The HTTP response for this API operation.
122: */
123: public function setStaticIndexPage($page)
124: {
125: if ($this instanceof CDNContainer) {
126: $this->getLogger()->warning(
127: 'This method cannot be called on the CDN object - please execute it on the normal Container'
128: );
129: }
130:
131: $headers = array('X-Container-Meta-Web-Index' => $page);
132:
133: return $this->getClient()->post($this->getUrl(), $headers)->send();
134: }
135:
136: /**
137: * Set the default error page for your static site.
138: *
139: * @param $name The data object name (i.e. a .html file) that will serve as the main error page.
140: * @return \Guzzle\Http\Message\Response The HTTP response for this operation.
141: */
142: public function setStaticErrorPage($page)
143: {
144: if ($this instanceof CDNContainer) {
145: $this->getLogger()->warning(
146: 'This method cannot be called on the CDN object - please execute it on the normal Container'
147: );
148: }
149:
150: $headers = array('X-Container-Meta-Web-Error' => $page);
151:
152: return $this->getClient()->post($this->getUrl(), $headers)->send();
153: }
154: }
155: