vendor/swiftmailer/lib/classes/Swift/Plugins/AntiFloodPlugin.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  * Reduces network flooding when sending large amounts of mail.
       
    14  * @package Swift
       
    15  * @subpackage Plugins
       
    16  * @author Chris Corbyn
       
    17  */
       
    18 class Swift_Plugins_AntiFloodPlugin
       
    19   implements Swift_Events_SendListener, Swift_Plugins_Sleeper
       
    20 {
       
    21   
       
    22   /**
       
    23    * The number of emails to send before restarting Transport.
       
    24    * @var int
       
    25    * @access private
       
    26    */
       
    27   private $_threshold;
       
    28   
       
    29   /**
       
    30    * The number of seconds to sleep for during a restart.
       
    31    * @var int
       
    32    * @access private
       
    33    */
       
    34   private $_sleep;
       
    35   
       
    36   /**
       
    37    * The internal counter.
       
    38    * @var int
       
    39    * @access private
       
    40    */
       
    41   private $_counter = 0;
       
    42   
       
    43   /**
       
    44    * The Sleeper instance for sleeping.
       
    45    * @var Swift_Plugins_Sleeper
       
    46    * @access private
       
    47    */
       
    48   private $_sleeper;
       
    49   
       
    50   /**
       
    51    * Create a new AntiFloodPlugin with $threshold and $sleep time.
       
    52    * @param int $threshold
       
    53    * @param int $sleep time
       
    54    * @param Swift_Plugins_Sleeper $sleeper (not needed really)
       
    55    */
       
    56   public function __construct($threshold = 99, $sleep = 0,
       
    57     Swift_Plugins_Sleeper $sleeper = null)
       
    58   {
       
    59     $this->setThreshold($threshold);
       
    60     $this->setSleepTime($sleep);
       
    61     $this->_sleeper = $sleeper;
       
    62   }
       
    63   
       
    64   /**
       
    65    * Set the number of emails to send before restarting.
       
    66    * @param int $threshold
       
    67    */
       
    68   public function setThreshold($threshold)
       
    69   {
       
    70     $this->_threshold = $threshold;
       
    71   }
       
    72   
       
    73   /**
       
    74    * Get the number of emails to send before restarting.
       
    75    * @return int
       
    76    */
       
    77   public function getThreshold()
       
    78   {
       
    79     return $this->_threshold;
       
    80   }
       
    81   
       
    82   /**
       
    83    * Set the number of seconds to sleep for during a restart.
       
    84    * @param int $sleep time
       
    85    */
       
    86   public function setSleepTime($sleep)
       
    87   {
       
    88     $this->_sleep = $sleep;
       
    89   }
       
    90   
       
    91   /**
       
    92    * Get the number of seconds to sleep for during a restart.
       
    93    * @return int
       
    94    */
       
    95   public function getSleepTime()
       
    96   {
       
    97     return $this->_sleep;
       
    98   }
       
    99   
       
   100   /**
       
   101    * Invoked immediately before the Message is sent.
       
   102    * @param Swift_Events_SendEvent $evt
       
   103    */
       
   104   public function beforeSendPerformed(Swift_Events_SendEvent $evt)
       
   105   {
       
   106   }
       
   107   
       
   108   /**
       
   109    * Invoked immediately after the Message is sent.
       
   110    * @param Swift_Events_SendEvent $evt
       
   111    */
       
   112   public function sendPerformed(Swift_Events_SendEvent $evt)
       
   113   {
       
   114     ++$this->_counter;
       
   115     if ($this->_counter >= $this->_threshold)
       
   116     {
       
   117       $transport = $evt->getTransport();
       
   118       $transport->stop();
       
   119       if ($this->_sleep)
       
   120       {
       
   121         $this->sleep($this->_sleep);
       
   122       }
       
   123       $transport->start();
       
   124       $this->_counter = 0;
       
   125     }
       
   126   }
       
   127   
       
   128   /**
       
   129    * Sleep for $seconds.
       
   130    * @param int $seconds
       
   131    */
       
   132   public function sleep($seconds)
       
   133   {
       
   134     if (isset($this->_sleeper))
       
   135     {
       
   136       $this->_sleeper->sleep($seconds);
       
   137     }
       
   138     else
       
   139     {
       
   140       sleep($seconds);
       
   141     }
       
   142   }
       
   143   
       
   144 }