TYPO3 API  SVNRelease
FailoverTransport.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Transport/LoadBalancedTransport.php';
00012 //@require 'Swift/Mime/Message.php';
00013 
00014 /**
00015  * Contains a list of redundant Transports so when one fails, the next is used.
00016  * @package Swift
00017  * @subpackage Transport
00018  * @author Chris Corbyn
00019  */
00020 class Swift_Transport_FailoverTransport
00021   extends Swift_Transport_LoadBalancedTransport
00022 {
00023 
00024   /**
00025    * Registered transport curently used.
00026    * @var Swift_Transport
00027    * @access private
00028    */
00029   private $_currentTransport;
00030 
00031   /**
00032    * Creates a new FailoverTransport.
00033    */
00034   public function __construct()
00035   {
00036     parent::__construct();
00037   }
00038 
00039   /**
00040    * Send the given Message.
00041    * Recipient/sender data will be retreived from the Message API.
00042    * The return value is the number of recipients who were accepted for delivery.
00043    * @param Swift_Mime_Message $message
00044    * @param string[] &$failedRecipients to collect failures by-reference
00045    * @return int
00046    */
00047   public function send(Swift_Mime_Message $message, &$failedRecipients = null)
00048   {
00049     $maxTransports = count($this->_transports);
00050     $sent = 0;
00051 
00052     for ($i = 0; $i < $maxTransports
00053       && $transport = $this->_getNextTransport(); ++$i)
00054     {
00055       try
00056       {
00057         if (!$transport->isStarted())
00058         {
00059           $transport->start();
00060         }
00061 
00062         return $transport->send($message, $failedRecipients);
00063       }
00064       catch (Swift_TransportException $e)
00065       {
00066         $this->_killCurrentTransport();
00067       }
00068     }
00069 
00070     if (count($this->_transports) == 0)
00071     {
00072       throw new Swift_TransportException(
00073         'All Transports in FailoverTransport failed, or no Transports available'
00074         );
00075     }
00076 
00077     return $sent;
00078   }
00079 
00080   // -- Protected methods
00081 
00082   protected function _getNextTransport()
00083   {
00084     if (!isset($this->_currentTransport))
00085     {
00086       $this->_currentTransport = parent::_getNextTransport();
00087     }
00088     return $this->_currentTransport;
00089   }
00090 
00091   protected function _killCurrentTransport()
00092   {
00093     $this->_currentTransport = null;
00094     parent::_killCurrentTransport();
00095   }
00096 
00097 }