Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / typo3 / contrib / swiftmailer / classes / Swift / Transport / FailoverTransport.php
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/LoadBalancedTransport.php';
12 //@require 'Swift/Mime/Message.php';
13
14 /**
15 * Contains a list of redundant Transports so when one fails, the next is used.
16 * @package Swift
17 * @subpackage Transport
18 * @author Chris Corbyn
19 */
20 class Swift_Transport_FailoverTransport
21 extends Swift_Transport_LoadBalancedTransport
22 {
23
24 /**
25 * Registered transport curently used.
26 * @var Swift_Transport
27 * @access private
28 */
29 private $_currentTransport;
30
31 /**
32 * Creates a new FailoverTransport.
33 */
34 public function __construct()
35 {
36 parent::__construct();
37 }
38
39 /**
40 * Send the given Message.
41 * Recipient/sender data will be retreived from the Message API.
42 * The return value is the number of recipients who were accepted for delivery.
43 * @param Swift_Mime_Message $message
44 * @param string[] &$failedRecipients to collect failures by-reference
45 * @return int
46 */
47 public function send(Swift_Mime_Message $message, &$failedRecipients = null)
48 {
49 $maxTransports = count($this->_transports);
50 $sent = 0;
51
52 for ($i = 0; $i < $maxTransports
53 && $transport = $this->_getNextTransport(); ++$i)
54 {
55 try
56 {
57 if (!$transport->isStarted())
58 {
59 $transport->start();
60 }
61
62 return $transport->send($message, $failedRecipients);
63 }
64 catch (Swift_TransportException $e)
65 {
66 $this->_killCurrentTransport();
67 }
68 }
69
70 if (count($this->_transports) == 0)
71 {
72 throw new Swift_TransportException(
73 'All Transports in FailoverTransport failed, or no Transports available'
74 );
75 }
76
77 return $sent;
78 }
79
80 // -- Protected methods
81
82 protected function _getNextTransport()
83 {
84 if (!isset($this->_currentTransport))
85 {
86 $this->_currentTransport = parent::_getNextTransport();
87 }
88 return $this->_currentTransport;
89 }
90
91 protected function _killCurrentTransport()
92 {
93 $this->_currentTransport = null;
94 parent::_killCurrentTransport();
95 }
96
97 }