TYPO3 API  SVNRelease
AntiFloodPlugin.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/Events/SendListener.php';
00012 //@require 'Swift/Events/SendEvent.php';
00013 //@require 'Swift/Plugins/Sleeper.php';
00014 
00015 /**
00016  * Reduces network flooding when sending large amounts of mail.
00017  * @package Swift
00018  * @subpackage Plugins
00019  * @author Chris Corbyn
00020  */
00021 class Swift_Plugins_AntiFloodPlugin
00022   implements Swift_Events_SendListener, Swift_Plugins_Sleeper
00023 {
00024 
00025   /**
00026    * The number of emails to send before restarting Transport.
00027    * @var int
00028    * @access private
00029    */
00030   private $_threshold;
00031 
00032   /**
00033    * The number of seconds to sleep for during a restart.
00034    * @var int
00035    * @access private
00036    */
00037   private $_sleep;
00038 
00039   /**
00040    * The internal counter.
00041    * @var int
00042    * @access private
00043    */
00044   private $_counter = 0;
00045 
00046   /**
00047    * The Sleeper instance for sleeping.
00048    * @var Swift_Plugins_Sleeper
00049    * @access private
00050    */
00051   private $_sleeper;
00052 
00053   /**
00054    * Create a new AntiFloodPlugin with $threshold and $sleep time.
00055    * @param int $threshold
00056    * @param int $sleep time
00057    * @param Swift_Plugins_Sleeper $sleeper (not needed really)
00058    */
00059   public function __construct($threshold = 99, $sleep = 0,
00060     Swift_Plugins_Sleeper $sleeper = null)
00061   {
00062     $this->setThreshold($threshold);
00063     $this->setSleepTime($sleep);
00064     $this->_sleeper = $sleeper;
00065   }
00066 
00067   /**
00068    * Set the number of emails to send before restarting.
00069    * @param int $threshold
00070    */
00071   public function setThreshold($threshold)
00072   {
00073     $this->_threshold = $threshold;
00074   }
00075 
00076   /**
00077    * Get the number of emails to send before restarting.
00078    * @return int
00079    */
00080   public function getThreshold()
00081   {
00082     return $this->_threshold;
00083   }
00084 
00085   /**
00086    * Set the number of seconds to sleep for during a restart.
00087    * @param int $sleep time
00088    */
00089   public function setSleepTime($sleep)
00090   {
00091     $this->_sleep = $sleep;
00092   }
00093 
00094   /**
00095    * Get the number of seconds to sleep for during a restart.
00096    * @return int
00097    */
00098   public function getSleepTime()
00099   {
00100     return $this->_sleep;
00101   }
00102 
00103   /**
00104    * Invoked immediately before the Message is sent.
00105    * @param Swift_Events_SendEvent $evt
00106    */
00107   public function beforeSendPerformed(Swift_Events_SendEvent $evt)
00108   {
00109   }
00110 
00111   /**
00112    * Invoked immediately after the Message is sent.
00113    * @param Swift_Events_SendEvent $evt
00114    */
00115   public function sendPerformed(Swift_Events_SendEvent $evt)
00116   {
00117     ++$this->_counter;
00118     if ($this->_counter >= $this->_threshold)
00119     {
00120       $transport = $evt->getTransport();
00121       $transport->stop();
00122       if ($this->_sleep)
00123       {
00124         $this->sleep($this->_sleep);
00125       }
00126       $transport->start();
00127       $this->_counter = 0;
00128     }
00129   }
00130 
00131   /**
00132    * Sleep for $seconds.
00133    * @param int $seconds
00134    */
00135   public function sleep($seconds)
00136   {
00137     if (isset($this->_sleeper))
00138     {
00139       $this->_sleeper->sleep($seconds);
00140     }
00141     else
00142     {
00143       sleep($seconds);
00144     }
00145   }
00146 
00147 }