[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/ -> LoadBalancedTransport.php (source)

   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   * Redundantly and rotationally uses several Transports when sending.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Transport_LoadBalancedTransport implements Swift_Transport
  17  {
  18      /**
  19       * Transports which are deemed useless.
  20       *
  21       * @var Swift_Transport[]
  22       */
  23      private $deadTransports = [];
  24  
  25      /**
  26       * The Transports which are used in rotation.
  27       *
  28       * @var Swift_Transport[]
  29       */
  30      protected $transports = [];
  31  
  32      /**
  33       * The Transport used in the last successful send operation.
  34       *
  35       * @var Swift_Transport
  36       */
  37      protected $lastUsedTransport = null;
  38  
  39      // needed as __construct is called from elsewhere explicitly
  40      public function __construct()
  41      {
  42      }
  43  
  44      /**
  45       * Set $transports to delegate to.
  46       *
  47       * @param Swift_Transport[] $transports
  48       */
  49      public function setTransports(array $transports)
  50      {
  51          $this->transports = $transports;
  52          $this->deadTransports = [];
  53      }
  54  
  55      /**
  56       * Get $transports to delegate to.
  57       *
  58       * @return Swift_Transport[]
  59       */
  60      public function getTransports()
  61      {
  62          return array_merge($this->transports, $this->deadTransports);
  63      }
  64  
  65      /**
  66       * Get the Transport used in the last successful send operation.
  67       *
  68       * @return Swift_Transport
  69       */
  70      public function getLastUsedTransport()
  71      {
  72          return $this->lastUsedTransport;
  73      }
  74  
  75      /**
  76       * Test if this Transport mechanism has started.
  77       *
  78       * @return bool
  79       */
  80      public function isStarted()
  81      {
  82          return count($this->transports) > 0;
  83      }
  84  
  85      /**
  86       * Start this Transport mechanism.
  87       */
  88      public function start()
  89      {
  90          $this->transports = array_merge($this->transports, $this->deadTransports);
  91      }
  92  
  93      /**
  94       * Stop this Transport mechanism.
  95       */
  96      public function stop()
  97      {
  98          foreach ($this->transports as $transport) {
  99              $transport->stop();
 100          }
 101      }
 102  
 103      /**
 104       * {@inheritdoc}
 105       */
 106      public function ping()
 107      {
 108          foreach ($this->transports as $transport) {
 109              if (!$transport->ping()) {
 110                  $this->killCurrentTransport();
 111              }
 112          }
 113  
 114          return count($this->transports) > 0;
 115      }
 116  
 117      /**
 118       * Send the given Message.
 119       *
 120       * Recipient/sender data will be retrieved from the Message API.
 121       * The return value is the number of recipients who were accepted for delivery.
 122       *
 123       * @param string[] $failedRecipients An array of failures by-reference
 124       *
 125       * @return int
 126       */
 127      public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
 128      {
 129          $maxTransports = count($this->transports);
 130          $sent = 0;
 131          $this->lastUsedTransport = null;
 132  
 133          for ($i = 0; $i < $maxTransports
 134              && $transport = $this->getNextTransport(); ++$i) {
 135              try {
 136                  if (!$transport->isStarted()) {
 137                      $transport->start();
 138                  }
 139                  if ($sent = $transport->send($message, $failedRecipients)) {
 140                      $this->lastUsedTransport = $transport;
 141                      break;
 142                  }
 143              } catch (Swift_TransportException $e) {
 144                  $this->killCurrentTransport();
 145              }
 146          }
 147  
 148          if (0 == count($this->transports)) {
 149              throw new Swift_TransportException('All Transports in LoadBalancedTransport failed, or no Transports available');
 150          }
 151  
 152          return $sent;
 153      }
 154  
 155      /**
 156       * Register a plugin.
 157       */
 158      public function registerPlugin(Swift_Events_EventListener $plugin)
 159      {
 160          foreach ($this->transports as $transport) {
 161              $transport->registerPlugin($plugin);
 162          }
 163      }
 164  
 165      /**
 166       * Rotates the transport list around and returns the first instance.
 167       *
 168       * @return Swift_Transport
 169       */
 170      protected function getNextTransport()
 171      {
 172          if ($next = array_shift($this->transports)) {
 173              $this->transports[] = $next;
 174          }
 175  
 176          return $next;
 177      }
 178  
 179      /**
 180       * Tag the currently used (top of stack) transport as dead/useless.
 181       */
 182      protected function killCurrentTransport()
 183      {
 184          if ($transport = array_pop($this->transports)) {
 185              try {
 186                  $transport->stop();
 187              } catch (Exception $e) {
 188              }
 189              $this->deadTransports[] = $transport;
 190          }
 191      }
 192  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1