MemorySpool.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. return 0;
  61. }
  62. if (!$transport->isStarted()) {
  63. $transport->start();
  64. }
  65. $count = 0;
  66. while ($message = array_pop($this->messages)) {
  67. $count += $transport->send($message, $failedRecipients);
  68. }
  69. return $count;
  70. }
  71. }