MemorySpool.php 1.6KB

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