MemorySpool.php 1.7 KB

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