PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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