PageRenderTime 72ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Service/Rackspace/Files/Container.php

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