[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/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 = array();
  24  
  25      /**
  26       * The Transports which are used in rotation.
  27       *
  28       * @var Swift_Transport[]
  29       */
  30      protected $_transports = array();
  31  
  32      /**
  33       * Creates a new LoadBalancedTransport.
  34       */
  35      public function __construct()
  36      {
  37      }
  38  
  39      /**
  40       * Set $transports to delegate to.
  41       *
  42       * @param Swift_Transport[] $transports
  43       */
  44      public function setTransports(array $transports)
  45      {
  46          $this->_transports = $transports;
  47          $this->_deadTransports = array();
  48      }
  49  
  50      /**
  51       * Get $transports to delegate to.
  52       *
  53       * @return Swift_Transport[]
  54       */
  55      public function getTransports()
  56      {
  57          return array_merge($this->_transports, $this->_deadTransports);
  58      }
  59  
  60      /**
  61       * Test if this Transport mechanism has started.
  62       *
  63       * @return bool
  64       */
  65      public function isStarted()
  66      {
  67          return count($this->_transports) > 0;
  68      }
  69  
  70      /**
  71       * Start this Transport mechanism.
  72       */
  73      public function start()
  74      {
  75          $this->_transports = array_merge($this->_transports, $this->_deadTransports);
  76      }
  77  
  78      /**
  79       * Stop this Transport mechanism.
  80       */
  81      public function stop()
  82      {
  83          foreach ($this->_transports as $transport) {
  84              $transport->stop();
  85          }
  86      }
  87  
  88      /**
  89       * Send the given Message.
  90       *
  91       * Recipient/sender data will be retrieved from the Message API.
  92       * The return value is the number of recipients who were accepted for delivery.
  93       *
  94       * @param Swift_Mime_Message $message
  95       * @param string[]           $failedRecipients An array of failures by-reference
  96       *
  97       * @return int
  98       */
  99      public function send(Swift_Mime_Message $message, &$failedRecipients = null)
 100      {
 101          $maxTransports = count($this->_transports);
 102          $sent = 0;
 103  
 104          for ($i = 0; $i < $maxTransports
 105              && $transport = $this->_getNextTransport(); ++$i) {
 106              try {
 107                  if (!$transport->isStarted()) {
 108                      $transport->start();
 109                  }
 110                  if ($sent = $transport->send($message, $failedRecipients)) {
 111                      break;
 112                  }
 113              } catch (Swift_TransportException $e) {
 114                  $this->_killCurrentTransport();
 115              }
 116          }
 117  
 118          if (count($this->_transports) == 0) {
 119              throw new Swift_TransportException(
 120                  'All Transports in LoadBalancedTransport failed, or no Transports available'
 121                  );
 122          }
 123  
 124          return $sent;
 125      }
 126  
 127      /**
 128       * Register a plugin.
 129       *
 130       * @param Swift_Events_EventListener $plugin
 131       */
 132      public function registerPlugin(Swift_Events_EventListener $plugin)
 133      {
 134          foreach ($this->_transports as $transport) {
 135              $transport->registerPlugin($plugin);
 136          }
 137      }
 138  
 139      /**
 140       * Rotates the transport list around and returns the first instance.
 141       *
 142       * @return Swift_Transport
 143       */
 144      protected function _getNextTransport()
 145      {
 146          if ($next = array_shift($this->_transports)) {
 147              $this->_transports[] = $next;
 148          }
 149  
 150          return $next;
 151      }
 152  
 153      /**
 154       * Tag the currently used (top of stack) transport as dead/useless.
 155       */
 156      protected function _killCurrentTransport()
 157      {
 158          if ($transport = array_pop($this->_transports)) {
 159              try {
 160                  $transport->stop();
 161              } catch (Exception $e) {
 162              }
 163              $this->_deadTransports[] = $transport;
 164          }
 165      }
 166  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1