4
* This file is part of SwiftMailer.
5
* (c) 2004-2009 Chris Corbyn
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
11
//@require 'Swift/Transport.php';
12
//@require 'Swift/Mime/Message.php';
13
//@require 'Swift/Events/EventListener.php';
16
* Redudantly and rotationally uses several Transports when sending.
19
* @subpackage Transport
20
* @author Chris Corbyn
22
class Swift_Transport_LoadBalancedTransport implements Swift_Transport
25
/** Transports which are deemed useless */
26
private $_deadTransports = array();
29
* The Transports which are used in rotation.
31
* @var array Swift_Transport
34
protected $_transports = array();
37
* Creates a new LoadBalancedTransport.
39
public function __construct()
44
* Set $transports to delegate to.
46
* @param array $transports Swift_Transport
48
public function setTransports(array $transports)
50
$this->_transports = $transports;
51
$this->_deadTransports = array();
55
* Get $transports to delegate to.
57
* @return array Swift_Transport
59
public function getTransports(array $transports)
61
return array_merge($this->_transports, $this->_deadTransports);
65
* Test if this Transport mechanism has started.
69
public function isStarted()
71
return count($this->_transports) > 0;
75
* Start this Transport mechanism.
77
public function start()
79
$this->_transports = array_merge($this->_transports, $this->_deadTransports);
83
* Stop this Transport mechanism.
85
public function stop()
87
foreach ($this->_transports as $transport)
94
* Send the given Message.
96
* Recipient/sender data will be retreived from the Message API.
97
* The return value is the number of recipients who were accepted for delivery.
99
* @param Swift_Mime_Message $message
100
* @param string[] &$failedRecipients to collect failures by-reference
103
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
105
$maxTransports = count($this->_transports);
108
for ($i = 0; $i < $maxTransports
109
&& $transport = $this->_getNextTransport(); ++$i)
113
if (!$transport->isStarted())
117
if ($sent = $transport->send($message, $failedRecipients))
122
catch (Swift_TransportException $e)
124
$this->_killCurrentTransport();
128
if (count($this->_transports) == 0)
130
throw new Swift_TransportException(
131
'All Transports in LoadBalancedTransport failed, or no Transports available'
141
* @param Swift_Events_EventListener $plugin
143
public function registerPlugin(Swift_Events_EventListener $plugin)
145
foreach ($this->_transports as $transport)
147
$transport->registerPlugin($plugin);
151
// -- Protected methods
154
* Rotates the transport list around and returns the first instance.
156
* @return Swift_Transport
159
protected function _getNextTransport()
161
if ($next = array_shift($this->_transports))
163
$this->_transports[] = $next;
169
* Tag the currently used (top of stack) transport as dead/useless.
173
protected function _killCurrentTransport()
175
if ($transport = array_pop($this->_transports))
184
$this->_deadTransports[] = $transport;