TYPO3  7.6
Transport/LoadBalancedTransport.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
23  private $_deadTransports = array();
24 
30  protected $_transports = array();
31 
35  public function __construct()
36  {
37  }
38 
44  public function setTransports(array $transports)
45  {
46  $this->_transports = $transports;
47  $this->_deadTransports = array();
48  }
49 
55  public function getTransports()
56  {
57  return array_merge($this->_transports, $this->_deadTransports);
58  }
59 
65  public function isStarted()
66  {
67  return count($this->_transports) > 0;
68  }
69 
73  public function start()
74  {
75  $this->_transports = array_merge($this->_transports, $this->_deadTransports);
76  }
77 
81  public function stop()
82  {
83  foreach ($this->_transports as $transport) {
84  $transport->stop();
85  }
86  }
87 
99  public function send(Swift_Mime_Message $message, &$failedRecipients = null)
100  {
101  $maxTransports = count($this->_transports);
102  $sent = 0;
103 
104  for ($i = 0; $i < $maxTransports
105  && $transport = $this->_getNextTransport(); ++$i) {
106  try {
107  if (!$transport->isStarted()) {
108  $transport->start();
109  }
110  if ($sent = $transport->send($message, $failedRecipients)) {
111  break;
112  }
113  } catch (Swift_TransportException $e) {
114  $this->_killCurrentTransport();
115  }
116  }
117 
118  if (count($this->_transports) == 0) {
119  throw new Swift_TransportException(
120  'All Transports in LoadBalancedTransport failed, or no Transports available'
121  );
122  }
123 
124  return $sent;
125  }
126 
133  {
134  foreach ($this->_transports as $transport) {
135  $transport->registerPlugin($plugin);
136  }
137  }
138 
144  protected function _getNextTransport()
145  {
146  if ($next = array_shift($this->_transports)) {
147  $this->_transports[] = $next;
148  }
149 
150  return $next;
151  }
152 
156  protected function _killCurrentTransport()
157  {
158  if ($transport = array_pop($this->_transports)) {
159  try {
160  $transport->stop();
161  } catch (Exception $e) {
162  }
163  $this->_deadTransports[] = $transport;
164  }
165  }
166 }