SpoolTransport.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2009 Fabien Potencier <fabien.potencier@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Stores Messages in a queue.
  11. *
  12. * @author Fabien Potencier
  13. */
  14. class Swift_Transport_SpoolTransport implements Swift_Transport
  15. {
  16. /** The spool instance */
  17. private $_spool;
  18. /** The event dispatcher from the plugin API */
  19. private $_eventDispatcher;
  20. /**
  21. * Constructor.
  22. */
  23. public function __construct(Swift_Events_EventDispatcher $eventDispatcher, Swift_Spool $spool = null)
  24. {
  25. $this->_eventDispatcher = $eventDispatcher;
  26. $this->_spool = $spool;
  27. }
  28. /**
  29. * Sets the spool object.
  30. *
  31. * @param Swift_Spool $spool
  32. *
  33. * @return Swift_Transport_SpoolTransport
  34. */
  35. public function setSpool(Swift_Spool $spool)
  36. {
  37. $this->_spool = $spool;
  38. return $this;
  39. }
  40. /**
  41. * Get the spool object.
  42. *
  43. * @return Swift_Spool
  44. */
  45. public function getSpool()
  46. {
  47. return $this->_spool;
  48. }
  49. /**
  50. * Tests if this Transport mechanism has started.
  51. *
  52. * @return bool
  53. */
  54. public function isStarted()
  55. {
  56. return true;
  57. }
  58. /**
  59. * Starts this Transport mechanism.
  60. */
  61. public function start()
  62. {
  63. }
  64. /**
  65. * Stops this Transport mechanism.
  66. */
  67. public function stop()
  68. {
  69. }
  70. /**
  71. * Sends the given message.
  72. *
  73. * @param Swift_Mime_Message $message
  74. * @param string[] $failedRecipients An array of failures by-reference
  75. *
  76. * @return int The number of sent e-mail's
  77. */
  78. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  79. {
  80. if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
  81. $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
  82. if ($evt->bubbleCancelled()) {
  83. return 0;
  84. }
  85. }
  86. $success = $this->_spool->queueMessage($message);
  87. if ($evt) {
  88. $evt->setResult($success ? Swift_Events_SendEvent::RESULT_SUCCESS : Swift_Events_SendEvent::RESULT_FAILED);
  89. $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
  90. }
  91. return 1;
  92. }
  93. /**
  94. * Register a plugin.
  95. *
  96. * @param Swift_Events_EventListener $plugin
  97. */
  98. public function registerPlugin(Swift_Events_EventListener $plugin)
  99. {
  100. $this->_eventDispatcher->bindEventListener($plugin);
  101. }
  102. }