vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/FailoverTransport.php line 16

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of SwiftMailer.
  4.  * (c) 2004-2009 Chris Corbyn
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. /**
  10.  * Contains a list of redundant Transports so when one fails, the next is used.
  11.  *
  12.  * @author Chris Corbyn
  13.  */
  14. class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTransport
  15. {
  16.     /**
  17.      * Registered transport currently used.
  18.      *
  19.      * @var Swift_Transport
  20.      */
  21.     private $currentTransport;
  22.     // needed as __construct is called from elsewhere explicitly
  23.     public function __construct()
  24.     {
  25.         parent::__construct();
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public function ping()
  31.     {
  32.         $maxTransports = \count($this->transports);
  33.         for ($i 0$i $maxTransports
  34.             && $transport $this->getNextTransport(); ++$i) {
  35.             if ($transport->ping()) {
  36.                 return true;
  37.             } else {
  38.                 $this->killCurrentTransport();
  39.             }
  40.         }
  41.         return \count($this->transports) > 0;
  42.     }
  43.     /**
  44.      * Send the given Message.
  45.      *
  46.      * Recipient/sender data will be retrieved from the Message API.
  47.      * The return value is the number of recipients who were accepted for delivery.
  48.      *
  49.      * @param string[] $failedRecipients An array of failures by-reference
  50.      *
  51.      * @return int
  52.      */
  53.     public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients null)
  54.     {
  55.         $maxTransports = \count($this->transports);
  56.         $sent 0;
  57.         $this->lastUsedTransport null;
  58.         for ($i 0$i $maxTransports
  59.             && $transport $this->getNextTransport(); ++$i) {
  60.             try {
  61.                 if (!$transport->isStarted()) {
  62.                     $transport->start();
  63.                 }
  64.                 if ($sent $transport->send($message$failedRecipients)) {
  65.                     $this->lastUsedTransport $transport;
  66.                     return $sent;
  67.                 }
  68.             } catch (Swift_TransportException $e) {
  69.                 $this->killCurrentTransport();
  70.             }
  71.         }
  72.         if (== \count($this->transports)) {
  73.             throw new Swift_TransportException('All Transports in FailoverTransport failed, or no Transports available');
  74.         }
  75.         return $sent;
  76.     }
  77.     protected function getNextTransport()
  78.     {
  79.         if (!isset($this->currentTransport)) {
  80.             $this->currentTransport parent::getNextTransport();
  81.         }
  82.         return $this->currentTransport;
  83.     }
  84.     protected function killCurrentTransport()
  85.     {
  86.         $this->currentTransport null;
  87.         parent::killCurrentTransport();
  88.     }
  89. }