/framework/vendor/swift/lib/classes/Swift/Transport/LoadBalancedTransport.php

http://zoop.googlecode.com/ · PHP · 188 lines · 93 code · 20 blank · 75 comment · 8 complexity · 85af76b41b15cdf54b4f6ef8e10d24a1 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/Transport.php';
  10. //@require 'Swift/Mime/Message.php';
  11. //@require 'Swift/Events/EventListener.php';
  12. /**
  13. * Redudantly and rotationally uses several Transports when sending.
  14. *
  15. * @package Swift
  16. * @subpackage Transport
  17. * @author Chris Corbyn
  18. */
  19. class Swift_Transport_LoadBalancedTransport implements Swift_Transport
  20. {
  21. /** Transports which are deemed useless */
  22. private $_deadTransports = array();
  23. /**
  24. * The Transports which are used in rotation.
  25. *
  26. * @var array Swift_Transport
  27. * @access protected
  28. */
  29. protected $_transports = array();
  30. /**
  31. * Creates a new LoadBalancedTransport.
  32. */
  33. public function __construct()
  34. {
  35. }
  36. /**
  37. * Set $transports to delegate to.
  38. *
  39. * @param array $transports Swift_Transport
  40. */
  41. public function setTransports(array $transports)
  42. {
  43. $this->_transports = $transports;
  44. $this->_deadTransports = array();
  45. }
  46. /**
  47. * Get $transports to delegate to.
  48. *
  49. * @return array Swift_Transport
  50. */
  51. public function getTransports(array $transports)
  52. {
  53. return array_merge($this->_transports, $this->_deadTransports);
  54. }
  55. /**
  56. * Test if this Transport mechanism has started.
  57. *
  58. * @return boolean
  59. */
  60. public function isStarted()
  61. {
  62. return count($this->_transports) > 0;
  63. }
  64. /**
  65. * Start this Transport mechanism.
  66. */
  67. public function start()
  68. {
  69. $this->_transports = array_merge($this->_transports, $this->_deadTransports);
  70. }
  71. /**
  72. * Stop this Transport mechanism.
  73. */
  74. public function stop()
  75. {
  76. foreach ($this->_transports as $transport)
  77. {
  78. $transport->stop();
  79. }
  80. }
  81. /**
  82. * Send the given Message.
  83. *
  84. * Recipient/sender data will be retreived from the Message API.
  85. * The return value is the number of recipients who were accepted for delivery.
  86. *
  87. * @param Swift_Mime_Message $message
  88. * @param string[] &$failedRecipients to collect failures by-reference
  89. * @return int
  90. */
  91. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  92. {
  93. $maxTransports = count($this->_transports);
  94. $sent = 0;
  95. for ($i = 0; $i < $maxTransports
  96. && $transport = $this->_getNextTransport(); ++$i)
  97. {
  98. try
  99. {
  100. if (!$transport->isStarted())
  101. {
  102. $transport->start();
  103. }
  104. if ($sent = $transport->send($message, $failedRecipients))
  105. {
  106. break;
  107. }
  108. }
  109. catch (Swift_TransportException $e)
  110. {
  111. $this->_killCurrentTransport();
  112. }
  113. }
  114. if (count($this->_transports) == 0)
  115. {
  116. throw new Swift_TransportException(
  117. 'All Transports in LoadBalancedTransport failed, or no Transports available'
  118. );
  119. }
  120. return $sent;
  121. }
  122. /**
  123. * Register a plugin.
  124. *
  125. * @param Swift_Events_EventListener $plugin
  126. */
  127. public function registerPlugin(Swift_Events_EventListener $plugin)
  128. {
  129. foreach ($this->_transports as $transport)
  130. {
  131. $transport->registerPlugin($plugin);
  132. }
  133. }
  134. // -- Protected methods
  135. /**
  136. * Rotates the transport list around and returns the first instance.
  137. *
  138. * @return Swift_Transport
  139. * @access protected
  140. */
  141. protected function _getNextTransport()
  142. {
  143. if ($next = array_shift($this->_transports))
  144. {
  145. $this->_transports[] = $next;
  146. }
  147. return $next;
  148. }
  149. /**
  150. * Tag the currently used (top of stack) transport as dead/useless.
  151. *
  152. * @access protected
  153. */
  154. protected function _killCurrentTransport()
  155. {
  156. if ($transport = array_pop($this->_transports))
  157. {
  158. try
  159. {
  160. $transport->stop();
  161. }
  162. catch (Exception $e)
  163. {
  164. }
  165. $this->_deadTransports[] = $transport;
  166. }
  167. }
  168. }