Message.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_Queue
  17. * @subpackage UnitTests
  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. * @version $Id$
  21. */
  22. /**
  23. * Class for custom messages
  24. *
  25. * We want be able to delete messages and we to serialize objects
  26. * via a getBody() and setBody()
  27. *
  28. * This is different that doing the serialization directly next to the object
  29. * because you may want to update getBody() and setBody() to do a json
  30. * conversion instead of a php serialize
  31. *
  32. * You could also just overload the Zend_Queue::send() and Zend_Queue::receive()
  33. * functions to do the serialization/json encoding, but I wanted to give an
  34. * good example that overloaded near everything except for the adapter.
  35. *
  36. * @category Zend
  37. * @package Zend_Queue
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Custom_Message extends Zend_Queue_Message
  43. {
  44. /**
  45. * We adjusted the constructor to accept both an array and an object.
  46. */
  47. public function __construct($mixed) {
  48. // we still have to support the code in Zend_Queue::receive that
  49. // passes in an array
  50. if (is_array($mixed)) {
  51. parent::__construct($mixed);
  52. } elseif (is_object($mixed)) {
  53. $this->setBody($mixed);
  54. $this->_connected = false;
  55. }
  56. }
  57. /**
  58. * We need to get the underlying body as a string
  59. *
  60. * @return string
  61. */
  62. public function __toString() {
  63. return $this->_data['body'];
  64. }
  65. /**
  66. * Sets the message body
  67. *
  68. * @param serializable $mixed
  69. */
  70. public function setBody($mixed)
  71. {
  72. $this->_data['body'] = serialize($mixed);
  73. }
  74. /**
  75. * Gets the message body
  76. *
  77. * @return $mixed
  78. */
  79. public function getBody()
  80. {
  81. return unserialize($this->_data['body']);
  82. }
  83. /**
  84. * Deletes the message.
  85. *
  86. * Note you cannot be disconnected from queue.
  87. *
  88. * $throw is set to to true, because most of the time you want to know if
  89. * there is an error. However, in Custom_Messages::__destruct() exceptions
  90. * cannot be thrown.
  91. *
  92. * These does not create a circular reference loop. Because deleteMessage
  93. * asks the queue service to delete the message, the message located here
  94. * is NOT deleted.
  95. *
  96. * @param boolean $throw defaults to true. Throw a message if there is an error
  97. *
  98. * @throws Zend_Queue_Exception if not connected
  99. */
  100. public function delete($throw = true)
  101. {
  102. if ($this->_connected) {
  103. if ($this->getQueue()->getAdapter()->isSupported('deleteMessage')) {
  104. $this->getQueue()->deleteMessage($this);
  105. }
  106. } elseif ($throw) {
  107. /**
  108. * @see Zend_Queue_Exception
  109. */
  110. require_once 'Zend/Queue/Exception.php';
  111. throw new Zend_Queue_Exception('Disconnected from queue. Cannot delete message from queue.');
  112. }
  113. }
  114. }