MemorySpool.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2011 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 memory.
  11. *
  12. * @package Swift
  13. * @author Fabien Potencier
  14. */
  15. class Swift_MemorySpool implements Swift_Spool
  16. {
  17. protected $messages = array();
  18. /**
  19. * Tests if this Transport mechanism has started.
  20. *
  21. * @return boolean
  22. */
  23. public function isStarted()
  24. {
  25. return true;
  26. }
  27. /**
  28. * Starts this Transport mechanism.
  29. */
  30. public function start()
  31. {
  32. }
  33. /**
  34. * Stops this Transport mechanism.
  35. */
  36. public function stop()
  37. {
  38. }
  39. /**
  40. * Stores a message in the queue.
  41. *
  42. * @param Swift_Mime_Message $message The message to store
  43. *
  44. * @return boolean Whether the operation has succeeded
  45. */
  46. public function queueMessage(Swift_Mime_Message $message)
  47. {
  48. $this->messages[] = $message;
  49. return true;
  50. }
  51. /**
  52. * Sends messages using the given transport instance.
  53. *
  54. * @param Swift_Transport $transport A transport instance
  55. * @param string[] $failedRecipients An array of failures by-reference
  56. *
  57. * @return integer The number of sent emails
  58. */
  59. public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
  60. {
  61. if (!$this->messages) {
  62. return 0;
  63. }
  64. if (!$transport->isStarted()) {
  65. $transport->start();
  66. }
  67. $count = 0;
  68. while ($message = array_pop($this->messages)) {
  69. $count += $transport->send($message, $failedRecipients);
  70. }
  71. return $count;
  72. }
  73. }