/library/Zend/Cloud/QueueService/Adapter/ZendQueue.php

https://github.com/praveensingh85/MyEventDashboard1 · PHP · 301 lines · 153 code · 21 blank · 127 comment · 17 complexity · c6ee959f0b034b01c107eff472eb5235 MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_Cloud
  15. * @subpackage QueueService
  16. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
  20. require_once 'Zend/Cloud/QueueService/Exception.php';
  21. require_once 'Zend/Queue.php';
  22. /**
  23. * WindowsAzure adapter for simple queue service.
  24. *
  25. * @category Zend
  26. * @package Zend_Cloud
  27. * @subpackage QueueService
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cloud_QueueService_Adapter_ZendQueue
  32. extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
  33. {
  34. /**
  35. * Options array keys for the Zend_Queue adapter.
  36. */
  37. const ADAPTER = 'adapter';
  38. /**
  39. * Storage client
  40. *
  41. * @var Zend_Queue
  42. */
  43. protected $_queue = null;
  44. /**
  45. * @var array All queues
  46. */
  47. protected $_queues = array();
  48. /**
  49. * Constructor
  50. *
  51. * @param array|Zend_Config $options
  52. * @return void
  53. */
  54. public function __construct ($options = array())
  55. {
  56. if ($options instanceof Zend_Config) {
  57. $options = $options->toArray();
  58. }
  59. if (!is_array($options)) {
  60. throw new Zend_Cloud_QueueService_Exception('Invalid options provided');
  61. }
  62. if (isset($options[self::MESSAGE_CLASS])) {
  63. $this->setMessageClass($options[self::MESSAGE_CLASS]);
  64. }
  65. if (isset($options[self::MESSAGESET_CLASS])) {
  66. $this->setMessageSetClass($options[self::MESSAGESET_CLASS]);
  67. }
  68. // Build Zend_Service_WindowsAzure_Storage_Blob instance
  69. if (!isset($options[self::ADAPTER])) {
  70. throw new Zend_Cloud_QueueService_Exception('No Zend_Queue adapter provided');
  71. } else {
  72. $adapter = $options[self::ADAPTER];
  73. unset($options[self::ADAPTER]);
  74. }
  75. try {
  76. $this->_queue = new Zend_Queue($adapter, $options);
  77. } catch (Zend_Queue_Exception $e) {
  78. throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
  79. }
  80. }
  81. /**
  82. * Create a queue. Returns the ID of the created queue (typically the URL).
  83. * It may take some time to create the queue. Check your vendor's
  84. * documentation for details.
  85. *
  86. * @param string $name
  87. * @param array $options
  88. * @return string Queue ID (typically URL)
  89. */
  90. public function createQueue($name, $options = null)
  91. {
  92. try {
  93. $this->_queues[$name] = $this->_queue->createQueue($name, isset($options[Zend_Queue::TIMEOUT])?$options[Zend_Queue::TIMEOUT]:null);
  94. return $name;
  95. } catch (Zend_Queue_Exception $e) {
  96. throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
  97. }
  98. }
  99. /**
  100. * Delete a queue. All messages in the queue will also be deleted.
  101. *
  102. * @param string $queueId
  103. * @param array $options
  104. * @return boolean true if successful, false otherwise
  105. */
  106. public function deleteQueue($queueId, $options = null)
  107. {
  108. if (!isset($this->_queues[$queueId])) {
  109. return false;
  110. }
  111. try {
  112. if ($this->_queues[$queueId]->deleteQueue()) {
  113. unset($this->_queues[$queueId]);
  114. return true;
  115. }
  116. } catch (Zend_Queue_Exception $e) {
  117. throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
  118. }
  119. }
  120. /**
  121. * List all queues.
  122. *
  123. * @param array $options
  124. * @return array Queue IDs
  125. */
  126. public function listQueues($options = null)
  127. {
  128. try {
  129. return $this->_queue->getQueues();
  130. } catch (Zend_Queue_Exception $e) {
  131. throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
  132. }
  133. }
  134. /**
  135. * Get a key/value array of metadata for the given queue.
  136. *
  137. * @param string $queueId
  138. * @param array $options
  139. * @return array
  140. */
  141. public function fetchQueueMetadata($queueId, $options = null)
  142. {
  143. if (!isset($this->_queues[$queueId])) {
  144. return false;
  145. }
  146. try {
  147. return $this->_queues[$queueId]->getOptions();
  148. } catch (Zend_Queue_Exception $e) {
  149. throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
  150. }
  151. }
  152. /**
  153. * Store a key/value array of metadata for the specified queue.
  154. * WARNING: This operation overwrites any metadata that is located at
  155. * $destinationPath. Some adapters may not support this method.
  156. *
  157. * @param string $queueId
  158. * @param array $metadata
  159. * @param array $options
  160. * @return void
  161. */
  162. public function storeQueueMetadata($queueId, $metadata, $options = null)
  163. {
  164. if (!isset($this->_queues[$queueId])) {
  165. throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
  166. }
  167. try {
  168. return $this->_queues[$queueId]->setOptions($metadata);
  169. } catch (Zend_Queue_Exception $e) {
  170. throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e);
  171. }
  172. }
  173. /**
  174. * Send a message to the specified queue.
  175. *
  176. * @param string $queueId
  177. * @param string $message
  178. * @param array $options
  179. * @return string Message ID
  180. */
  181. public function sendMessage($queueId, $message, $options = null)
  182. {
  183. if (!isset($this->_queues[$queueId])) {
  184. throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
  185. }
  186. try {
  187. return $this->_queues[$queueId]->send($message);
  188. } catch (Zend_Queue_Exception $e) {
  189. throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
  190. }
  191. }
  192. /**
  193. * Recieve at most $max messages from the specified queue and return the
  194. * message IDs for messages recieved.
  195. *
  196. * @param string $queueId
  197. * @param int $max
  198. * @param array $options
  199. * @return array
  200. */
  201. public function receiveMessages($queueId, $max = 1, $options = null)
  202. {
  203. if (!isset($this->_queues[$queueId])) {
  204. throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
  205. }
  206. try {
  207. $res = $this->_queues[$queueId]->receive($max, isset($options[Zend_Queue::TIMEOUT])?$options[Zend_Queue::TIMEOUT]:null);
  208. if ($res instanceof Iterator) {
  209. return $this->_makeMessages($res);
  210. } else {
  211. return $this->_makeMessages(array($res));
  212. }
  213. } catch (Zend_Queue_Exception $e) {
  214. throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
  215. }
  216. }
  217. /**
  218. * Create Zend_Cloud_QueueService_Message array for
  219. * Azure messages.
  220. *
  221. * @param array $messages
  222. * @return Zend_Cloud_QueueService_Message[]
  223. */
  224. protected function _makeMessages($messages)
  225. {
  226. $messageClass = $this->getMessageClass();
  227. $setClass = $this->getMessageSetClass();
  228. $result = array();
  229. foreach ($messages as $message) {
  230. $result[] = new $messageClass($message->body, $message);
  231. }
  232. return new $setClass($result);
  233. }
  234. /**
  235. * Delete the specified message from the specified queue.
  236. *
  237. * @param string $queueId
  238. * @param Zend_Cloud_QueueService_Message $message Message ID or message
  239. * @param array $options
  240. * @return void
  241. */
  242. public function deleteMessage($queueId, $message, $options = null)
  243. {
  244. if (!isset($this->_queues[$queueId])) {
  245. throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
  246. }
  247. try {
  248. if ($message instanceof Zend_Cloud_QueueService_Message) {
  249. $message = $message->getMessage();
  250. }
  251. if (!($message instanceof Zend_Queue_Message)) {
  252. throw new Zend_Cloud_QueueService_Exception('Cannot delete the message: Zend_Queue_Message object required');
  253. }
  254. return $this->_queues[$queueId]->deleteMessage($message);
  255. } catch (Zend_Queue_Exception $e) {
  256. throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
  257. }
  258. }
  259. /**
  260. * Peek at the messages from the specified queue without removing them.
  261. *
  262. * @param string $queueId
  263. * @param int $num How many messages
  264. * @param array $options
  265. * @return Zend_Cloud_QueueService_Message[]
  266. */
  267. public function peekMessages($queueId, $num = 1, $options = null)
  268. {
  269. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  270. throw new Zend_Cloud_OperationNotAvailableException('ZendQueue doesn\'t currently support message peeking');
  271. }
  272. /**
  273. * Get Azure implementation
  274. * @return Zend_Queue
  275. */
  276. public function getClient()
  277. {
  278. return $this->_queue;
  279. }
  280. }