Container.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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-2015 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 $data
  36. *
  37. * @throws Zend_Service_Rackspace_Files_Exception
  38. */
  39. public function __construct($service, $data)
  40. {
  41. if (!($service instanceof Zend_Service_Rackspace_Files)) {
  42. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  43. throw new Zend_Service_Rackspace_Files_Exception(
  44. self::ERROR_PARAM_FILE_CONSTRUCT
  45. );
  46. }
  47. if (!is_array($data)) {
  48. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  49. throw new Zend_Service_Rackspace_Files_Exception(
  50. self::ERROR_PARAM_ARRAY_CONSTRUCT
  51. );
  52. }
  53. if (!array_key_exists('name', $data)) {
  54. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  55. throw new Zend_Service_Rackspace_Files_Exception(
  56. self::ERROR_PARAM_NO_NAME
  57. );
  58. }
  59. $this->service = $service;
  60. $this->name = $data['name'];
  61. }
  62. /**
  63. * Get the name of the container
  64. *
  65. * @return string
  66. */
  67. public function getName()
  68. {
  69. return $this->name;
  70. }
  71. /**
  72. * Get the size in bytes of the container
  73. *
  74. * @return integer|bool
  75. */
  76. public function getSize()
  77. {
  78. $data = $this->getInfo();
  79. if (isset($data['bytes'])) {
  80. return $data['bytes'];
  81. }
  82. return false;
  83. }
  84. /**
  85. * Get the total count of objects in the container
  86. *
  87. * @return integer|bool
  88. */
  89. public function getObjectCount()
  90. {
  91. $data = $this->getInfo();
  92. if (isset($data['count'])) {
  93. return $data['count'];
  94. }
  95. return false;
  96. }
  97. /**
  98. * Return true if the container is CDN enabled
  99. *
  100. * @return bool
  101. */
  102. public function isCdnEnabled()
  103. {
  104. $data = $this->getCdnInfo();
  105. if (isset($data['cdn_enabled'])) {
  106. return $data['cdn_enabled'];
  107. }
  108. return false;
  109. }
  110. /**
  111. * Get the TTL of the CDN
  112. *
  113. * @return integer|bool
  114. */
  115. public function getCdnTtl()
  116. {
  117. $data = $this->getCdnInfo();
  118. if (isset($data['ttl'])) {
  119. return $data['ttl'];
  120. }
  121. return false;
  122. }
  123. /**
  124. * Return true if the log retention is enabled for the CDN
  125. *
  126. * @return bool
  127. */
  128. public function isCdnLogEnabled()
  129. {
  130. $data = $this->getCdnInfo();
  131. if (isset($data['log_retention'])) {
  132. return $data['log_retention'];
  133. }
  134. return false;
  135. }
  136. /**
  137. * Get the CDN URI
  138. *
  139. * @return string|bool
  140. */
  141. public function getCdnUri()
  142. {
  143. $data = $this->getCdnInfo();
  144. if (isset($data['cdn_uri'])) {
  145. return $data['cdn_uri'];
  146. }
  147. return false;
  148. }
  149. /**
  150. * Get the CDN URI SSL
  151. *
  152. * @return string|bool
  153. */
  154. public function getCdnUriSsl()
  155. {
  156. $data = $this->getCdnInfo();
  157. if (isset($data['cdn_uri_ssl'])) {
  158. return $data['cdn_uri_ssl'];
  159. }
  160. return false;
  161. }
  162. /**
  163. * Get the metadata of the container
  164. *
  165. * If $key is empty return the array of metadata
  166. *
  167. * @param string $key
  168. *
  169. * @return array|string|bool
  170. */
  171. public function getMetadata($key = null)
  172. {
  173. $result = $this->service->getMetadataContainer($this->getName());
  174. if (!empty($result) && is_array($result)) {
  175. if (empty($key)) {
  176. return $result['metadata'];
  177. } else {
  178. if (isset ($result['metadata'][$key])) {
  179. return $result['metadata'][$key];
  180. }
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. * Get the information of the container (total of objects, total size)
  187. *
  188. * @return array|bool
  189. */
  190. public function getInfo()
  191. {
  192. $result = $this->service->getMetadataContainer($this->getName());
  193. if (!empty($result) && is_array($result)) {
  194. return $result;
  195. }
  196. return false;
  197. }
  198. /**
  199. * Get all the object of the container
  200. *
  201. * @return Zend_Service_Rackspace_Files_ObjectList
  202. */
  203. public function getObjects()
  204. {
  205. return $this->service->getObjects($this->getName());
  206. }
  207. /**
  208. * Get an object of the container
  209. *
  210. * @param string $name
  211. * @param array $headers
  212. *
  213. * @return Zend_Service_Rackspace_Files_Object|bool
  214. */
  215. public function getObject($name, $headers = array())
  216. {
  217. return $this->service->getObject($this->getName(), $name, $headers);
  218. }
  219. /**
  220. * Add an object in the container
  221. *
  222. * @param string $name
  223. * @param string $file the content of the object
  224. * @param array $metadata
  225. *
  226. * @return bool
  227. */
  228. public function addObject($name, $file, $metadata = array())
  229. {
  230. return $this->service->storeObject(
  231. $this->getName(), $name, $file, $metadata
  232. );
  233. }
  234. /**
  235. * Delete an object in the container
  236. *
  237. * @param string $obj
  238. *
  239. * @return bool
  240. */
  241. public function deleteObject($obj)
  242. {
  243. return $this->service->deleteObject($this->getName(), $obj);
  244. }
  245. /**
  246. * Copy an object to another container
  247. *
  248. * @param string $obj_source
  249. * @param string $container_dest
  250. * @param string $obj_dest
  251. * @param array $metadata
  252. * @param string $content_type
  253. *
  254. * @return bool
  255. */
  256. public function copyObject(
  257. $obj_source, $container_dest, $obj_dest, $metadata = array(),
  258. $content_type = null
  259. )
  260. {
  261. return $this->service->copyObject(
  262. $this->getName(),
  263. $obj_source,
  264. $container_dest,
  265. $obj_dest,
  266. $metadata,
  267. $content_type
  268. );
  269. }
  270. /**
  271. * Get the metadata of an object in the container
  272. *
  273. * @param string $object
  274. *
  275. * @return array
  276. */
  277. public function getMetadataObject($object)
  278. {
  279. return $this->service->getMetadataObject($this->getName(), $object);
  280. }
  281. /**
  282. * Set the metadata of an object in the container
  283. *
  284. * @param string $object
  285. * @param array $metadata
  286. *
  287. * @return bool
  288. */
  289. public function setMetadataObject($object, $metadata = array())
  290. {
  291. return $this->service->setMetadataObject(
  292. $this->getName(), $object, $metadata
  293. );
  294. }
  295. /**
  296. * Enable the CDN for the container
  297. *
  298. * @param integer $ttl
  299. *
  300. * @return array|bool
  301. */
  302. public function enableCdn($ttl = Zend_Service_Rackspace_Files::CDN_TTL_MIN)
  303. {
  304. return $this->service->enableCdnContainer($this->getName(), $ttl);
  305. }
  306. /**
  307. * Disable the CDN for the container
  308. *
  309. * @return bool
  310. */
  311. public function disableCdn()
  312. {
  313. $result =
  314. $this->service->updateCdnContainer($this->getName(), null, false);
  315. return ($result !== false);
  316. }
  317. /**
  318. * Change the TTL for the CDN container
  319. *
  320. * @param integer $ttl
  321. *
  322. * @return bool
  323. */
  324. public function changeTtlCdn($ttl)
  325. {
  326. $result = $this->service->updateCdnContainer($this->getName(), $ttl);
  327. return ($result !== false);
  328. }
  329. /**
  330. * Enable the log retention for the CDN
  331. *
  332. * @return bool
  333. */
  334. public function enableLogCdn()
  335. {
  336. $result = $this->service->updateCdnContainer(
  337. $this->getName(), null, null, true
  338. );
  339. return ($result !== false);
  340. }
  341. /**
  342. * Disable the log retention for the CDN
  343. *
  344. * @return bool
  345. */
  346. public function disableLogCdn()
  347. {
  348. $result = $this->service->updateCdnContainer(
  349. $this->getName(), null, null, false
  350. );
  351. return ($result !== false);
  352. }
  353. /**
  354. * Get the CDN information
  355. *
  356. * @return array|bool
  357. */
  358. public function getCdnInfo()
  359. {
  360. return $this->service->getInfoCdnContainer($this->getName());
  361. }
  362. }