XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
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 
11 //@require 'Swift/Transport.php';
12 //@require 'Swift/Mime/Message.php';
13 //@require 'Swift/Events/EventListener.php';
14 
23 {
24 
26  private $_deadTransports = array();
27 
34  protected $_transports = array();
35 
39  public function __construct()
40  {
41  }
42 
48  public function setTransports(array $transports)
49  {
50  $this->_transports = $transports;
51  $this->_deadTransports = array();
52  }
53 
59  public function getTransports(array $transports)
60  {
61  return array_merge($this->_transports, $this->_deadTransports);
62  }
63 
69  public function isStarted()
70  {
71  return count($this->_transports) > 0;
72  }
73 
77  public function start()
78  {
79  $this->_transports = array_merge($this->_transports, $this->_deadTransports);
80  }
81 
85  public function stop()
86  {
87  foreach ($this->_transports as $transport)
88  {
89  $transport->stop();
90  }
91  }
92 
103  public function send(Swift_Mime_Message $message, &$failedRecipients = null)
104  {
105  $maxTransports = count($this->_transports);
106  $sent = 0;
107 
108  for ($i = 0; $i < $maxTransports
109  && $transport = $this->_getNextTransport(); ++$i)
110  {
111  try
112  {
113  if (!$transport->isStarted())
114  {
115  $transport->start();
116  }
117  if ($sent = $transport->send($message, $failedRecipients))
118  {
119  break;
120  }
121  }
122  catch (Swift_TransportException $e)
123  {
124  $this->_killCurrentTransport();
125  }
126  }
127 
128  if (count($this->_transports) == 0)
129  {
130  throw new Swift_TransportException(
131  'All Transports in LoadBalancedTransport failed, or no Transports available'
132  );
133  }
134 
135  return $sent;
136  }
137 
144  {
145  foreach ($this->_transports as $transport)
146  {
147  $transport->registerPlugin($plugin);
148  }
149  }
150 
151  // -- Protected methods
152 
159  protected function _getNextTransport()
160  {
161  if ($next = array_shift($this->_transports))
162  {
163  $this->_transports[] = $next;
164  }
165  return $next;
166  }
167 
173  protected function _killCurrentTransport()
174  {
175  if ($transport = array_pop($this->_transports))
176  {
177  try
178  {
179  $transport->stop();
180  }
181  catch (Exception $e)
182  {
183  }
184  $this->_deadTransports[] = $transport;
185  }
186  }
187 
188 }