|
1 <?php |
|
2 |
|
3 /** |
|
4 * Zend Framework |
|
5 * |
|
6 * LICENSE |
|
7 * |
|
8 * This source file is subject to the new BSD license that is bundled |
|
9 * with this package in the file LICENSE.txt. |
|
10 * It is also available through the world-wide-web at this URL: |
|
11 * http://framework.zend.com/license/new-bsd |
|
12 * If you did not receive a copy of the license and are unable to |
|
13 * obtain it through the world-wide-web, please send an email |
|
14 * to license@zend.com so we can send you a copy immediately. |
|
15 * |
|
16 * @category Zend |
|
17 * @package Zend_Service_Rackspace |
|
18 * @subpackage Files |
|
19 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
21 */ |
|
22 |
|
23 require_once 'Zend/Service/Rackspace/Files.php'; |
|
24 |
|
25 class Zend_Service_Rackspace_Files_Container |
|
26 { |
|
27 const ERROR_PARAM_FILE_CONSTRUCT = 'The Zend_Service_Rackspace_Files passed in construction is not valid'; |
|
28 const ERROR_PARAM_ARRAY_CONSTRUCT = 'The array passed in construction is not valid'; |
|
29 const ERROR_PARAM_NO_NAME = 'The container name is empty'; |
|
30 /** |
|
31 * @var string |
|
32 */ |
|
33 protected $name; |
|
34 /** |
|
35 * Construct |
|
36 * |
|
37 * @param Zend_Service_Rackspace_Files $service |
|
38 * @param string $name |
|
39 */ |
|
40 public function __construct($service, $data) |
|
41 { |
|
42 if (!($service instanceof Zend_Service_Rackspace_Files)) { |
|
43 require_once 'Zend/Service/Rackspace/Files/Exception.php'; |
|
44 throw new Zend_Service_Rackspace_Files_Exception(self::ERROR_PARAM_FILE_CONSTRUCT); |
|
45 } |
|
46 if (!is_array($data)) { |
|
47 require_once 'Zend/Service/Rackspace/Files/Exception.php'; |
|
48 throw new Zend_Service_Rackspace_Files_Exception(self::ERROR_PARAM_ARRAY_CONSTRUCT); |
|
49 } |
|
50 if (!array_key_exists('name', $data)) { |
|
51 require_once 'Zend/Service/Rackspace/Files/Exception.php'; |
|
52 throw new Zend_Service_Rackspace_Files_Exception(self::ERROR_PARAM_NO_NAME); |
|
53 } |
|
54 $this->service = $service; |
|
55 $this->name = $data['name']; |
|
56 } |
|
57 /** |
|
58 * Get the name of the container |
|
59 * |
|
60 * @return string |
|
61 */ |
|
62 public function getName() |
|
63 { |
|
64 return $this->name; |
|
65 } |
|
66 /** |
|
67 * Get the size in bytes of the container |
|
68 * |
|
69 * @return integer|boolean |
|
70 */ |
|
71 public function getSize() |
|
72 { |
|
73 $data = $this->getInfo(); |
|
74 if (isset($data['bytes'])) { |
|
75 return $data['bytes']; |
|
76 } |
|
77 return false; |
|
78 } |
|
79 /** |
|
80 * Get the total count of objects in the container |
|
81 * |
|
82 * @return integer|boolean |
|
83 */ |
|
84 public function getObjectCount() |
|
85 { |
|
86 $data = $this->getInfo(); |
|
87 if (isset($data['count'])) { |
|
88 return $data['count']; |
|
89 } |
|
90 return false; |
|
91 } |
|
92 /** |
|
93 * Return true if the container is CDN enabled |
|
94 * |
|
95 * @return boolean |
|
96 */ |
|
97 public function isCdnEnabled() |
|
98 { |
|
99 $data = $this->getCdnInfo(); |
|
100 if (isset($data['cdn_enabled'])) { |
|
101 return $data['cdn_enabled']; |
|
102 } |
|
103 return false; |
|
104 } |
|
105 /** |
|
106 * Get the TTL of the CDN |
|
107 * |
|
108 * @return integer|boolean |
|
109 */ |
|
110 public function getCdnTtl() |
|
111 { |
|
112 $data = $this->getCdnInfo(); |
|
113 if (!isset($data['ttl'])) { |
|
114 return $data['ttl']; |
|
115 } |
|
116 return false; |
|
117 } |
|
118 /** |
|
119 * Return true if the log retention is enabled for the CDN |
|
120 * |
|
121 * @return boolean |
|
122 */ |
|
123 public function isCdnLogEnabled() |
|
124 { |
|
125 $data = $this->getCdnInfo(); |
|
126 if (!isset($data['log_retention'])) { |
|
127 return $data['log_retention']; |
|
128 } |
|
129 return false; |
|
130 } |
|
131 /** |
|
132 * Get the CDN URI |
|
133 * |
|
134 * @return string|boolean |
|
135 */ |
|
136 public function getCdnUri() |
|
137 { |
|
138 $data = $this->getCdnInfo(); |
|
139 if (!isset($data['cdn_uri'])) { |
|
140 return $data['cdn_uri']; |
|
141 } |
|
142 return false; |
|
143 } |
|
144 /** |
|
145 * Get the CDN URI SSL |
|
146 * |
|
147 * @return string|boolean |
|
148 */ |
|
149 public function getCdnUriSsl() |
|
150 { |
|
151 $data = $this->getCdnInfo(); |
|
152 if (!isset($data['cdn_uri_ssl'])) { |
|
153 return $data['cdn_uri_ssl']; |
|
154 } |
|
155 return false; |
|
156 } |
|
157 /** |
|
158 * Get the metadata of the container |
|
159 * |
|
160 * If $key is empty return the array of metadata |
|
161 * |
|
162 * @param string $key |
|
163 * @return array|string|boolean |
|
164 */ |
|
165 public function getMetadata($key=null) |
|
166 { |
|
167 $result = $this->service->getMetadataContainer($this->getName()); |
|
168 if (!empty($result) && is_array($result)) { |
|
169 if (empty($key)) { |
|
170 return $result['metadata']; |
|
171 } else { |
|
172 if (isset ($result['metadata'][$key])) { |
|
173 return $result['metadata'][$key]; |
|
174 } |
|
175 } |
|
176 } |
|
177 return false; |
|
178 } |
|
179 /** |
|
180 * Get the information of the container (total of objects, total size) |
|
181 * |
|
182 * @return array|boolean |
|
183 */ |
|
184 public function getInfo() |
|
185 { |
|
186 $result = $this->service->getMetadataContainer($this->getName()); |
|
187 if (!empty($result) && is_array($result)) { |
|
188 return $result; |
|
189 } |
|
190 return false; |
|
191 } |
|
192 /** |
|
193 * Get all the object of the container |
|
194 * |
|
195 * @return Zend_Service_Rackspace_Files_ObjectList |
|
196 */ |
|
197 public function getObjects() |
|
198 { |
|
199 return $this->service->getObjects($this->getName()); |
|
200 } |
|
201 /** |
|
202 * Get an object of the container |
|
203 * |
|
204 * @param string $name |
|
205 * @param array $headers |
|
206 * @return Zend_Service_Rackspace_Files_Object|boolean |
|
207 */ |
|
208 public function getObject($name, $headers=array()) |
|
209 { |
|
210 return $this->service->getObject($this->getName(), $name, $headers); |
|
211 } |
|
212 /** |
|
213 * Add an object in the container |
|
214 * |
|
215 * @param string $name |
|
216 * @param string $file the content of the object |
|
217 * @param array $metadata |
|
218 * @return boolen |
|
219 */ |
|
220 public function addObject($name, $file, $metadata=array()) |
|
221 { |
|
222 return $this->service->storeObject($this->getName(), $name, $file, $metadata); |
|
223 } |
|
224 /** |
|
225 * Delete an object in the container |
|
226 * |
|
227 * @param string $obj |
|
228 * @return boolean |
|
229 */ |
|
230 public function deleteObject($obj) |
|
231 { |
|
232 return $this->service->deleteObject($this->getName(), $obj); |
|
233 } |
|
234 /** |
|
235 * Copy an object to another container |
|
236 * |
|
237 * @param string $obj_source |
|
238 * @param string $container_dest |
|
239 * @param string $obj_dest |
|
240 * @param array $metadata |
|
241 * @param string $content_type |
|
242 * @return boolean |
|
243 */ |
|
244 public function copyObject($obj_source, $container_dest, $obj_dest, $metadata=array(), $content_type=null) |
|
245 { |
|
246 return $this->service->copyObject($this->getName(), $obj_source, $container_dest, $obj_dest, $metadata, $content_type); |
|
247 } |
|
248 /** |
|
249 * Get the metadata of an object in the container |
|
250 * |
|
251 * @param string $object |
|
252 * @return array |
|
253 */ |
|
254 public function getMetadataObject($object) |
|
255 { |
|
256 return $this->service->getMetadataObject($this->getName(),$object); |
|
257 } |
|
258 /** |
|
259 * Set the metadata of an object in the container |
|
260 * |
|
261 * @param string $object |
|
262 * @param array $metadata |
|
263 * @return boolean |
|
264 */ |
|
265 public function setMetadataObject($object,$metadata=array()) |
|
266 { |
|
267 return $this->service->setMetadataObject($this->getName(),$object,$metadata); |
|
268 } |
|
269 /** |
|
270 * Enable the CDN for the container |
|
271 * |
|
272 * @param integer $ttl |
|
273 * @return array|boolean |
|
274 */ |
|
275 public function enableCdn($ttl=Zend_Service_Rackspace_Files::CDN_TTL_MIN) |
|
276 { |
|
277 return $this->service->enableCdnContainer($this->getName(),$ttl); |
|
278 } |
|
279 /** |
|
280 * Disable the CDN for the container |
|
281 * |
|
282 * @return boolean |
|
283 */ |
|
284 public function disableCdn() |
|
285 { |
|
286 $result = $this->service->updateCdnContainer($this->getName(),null,false); |
|
287 return ($result!==false); |
|
288 } |
|
289 /** |
|
290 * Change the TTL for the CDN container |
|
291 * |
|
292 * @param integer $ttl |
|
293 * @return boolean |
|
294 */ |
|
295 public function changeTtlCdn($ttl) |
|
296 { |
|
297 $result = $this->service->updateCdnContainer($this->getName(),$ttl); |
|
298 return ($result!==false); |
|
299 } |
|
300 /** |
|
301 * Enable the log retention for the CDN |
|
302 * |
|
303 * @return boolean |
|
304 */ |
|
305 public function enableLogCdn() |
|
306 { |
|
307 $result = $this->service->updateCdnContainer($this->getName(),null,null,true); |
|
308 return ($result!==false); |
|
309 } |
|
310 /** |
|
311 * Disable the log retention for the CDN |
|
312 * |
|
313 * @return boolean |
|
314 */ |
|
315 public function disableLogCdn() |
|
316 { |
|
317 $result = $this->service->updateCdnContainer($this->getName(),null,null,false); |
|
318 return ($result!==false); |
|
319 } |
|
320 /** |
|
321 * Get the CDN information |
|
322 * |
|
323 * @return array|boolean |
|
324 */ |
|
325 public function getCdnInfo() |
|
326 { |
|
327 return $this->service->getInfoCdnContainer($this->getName()); |
|
328 } |
|
329 } |