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