SpoolTransport.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * @return $this
  32. */
  33. public function setSpool(Swift_Spool $spool)
  34. {
  35. $this->spool = $spool;
  36. return $this;
  37. }
  38. /**
  39. * Get the spool object.
  40. *
  41. * @return Swift_Spool
  42. */
  43. public function getSpool()
  44. {
  45. return $this->spool;
  46. }
  47. /**
  48. * Tests if this Transport mechanism has started.
  49. *
  50. * @return bool
  51. */
  52. public function isStarted()
  53. {
  54. return true;
  55. }
  56. /**
  57. * Starts this Transport mechanism.
  58. */
  59. public function start()
  60. {
  61. }
  62. /**
  63. * Stops this Transport mechanism.
  64. */
  65. public function stop()
  66. {
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function ping()
  72. {
  73. return true;
  74. }
  75. /**
  76. * Sends the given message.
  77. *
  78. * @param string[] $failedRecipients An array of failures by-reference
  79. *
  80. * @return int The number of sent e-mail's
  81. */
  82. public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
  83. {
  84. if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) {
  85. $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
  86. if ($evt->bubbleCancelled()) {
  87. return 0;
  88. }
  89. }
  90. $success = $this->spool->queueMessage($message);
  91. if ($evt) {
  92. $evt->setResult($success ? Swift_Events_SendEvent::RESULT_SPOOLED : Swift_Events_SendEvent::RESULT_FAILED);
  93. $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed');
  94. }
  95. return 1;
  96. }
  97. /**
  98. * Register a plugin.
  99. */
  100. public function registerPlugin(Swift_Events_EventListener $plugin)
  101. {
  102. $this->eventDispatcher->bindEventListener($plugin);
  103. }
  104. }