XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
AntiFloodPlugin.php
Go to the documentation of this file.
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 //@require 'Swift/Events/SendListener.php';
12 //@require 'Swift/Events/SendEvent.php';
13 //@require 'Swift/Plugins/Sleeper.php';
14 
23 {
24 
30  private $_threshold;
31 
37  private $_sleep;
38 
44  private $_counter = 0;
45 
51  private $_sleeper;
52 
59  public function __construct($threshold = 99, $sleep = 0,
60  Swift_Plugins_Sleeper $sleeper = null)
61  {
62  $this->setThreshold($threshold);
63  $this->setSleepTime($sleep);
64  $this->_sleeper = $sleeper;
65  }
66 
71  public function setThreshold($threshold)
72  {
73  $this->_threshold = $threshold;
74  }
75 
80  public function getThreshold()
81  {
82  return $this->_threshold;
83  }
84 
89  public function setSleepTime($sleep)
90  {
91  $this->_sleep = $sleep;
92  }
93 
98  public function getSleepTime()
99  {
100  return $this->_sleep;
101  }
102 
108  {
109  }
110 
115  public function sendPerformed(Swift_Events_SendEvent $evt)
116  {
117  ++$this->_counter;
118  if ($this->_counter >= $this->_threshold)
119  {
120  $transport = $evt->getTransport();
121  $transport->stop();
122  if ($this->_sleep)
123  {
124  $this->sleep($this->_sleep);
125  }
126  $transport->start();
127  $this->_counter = 0;
128  }
129  }
130 
135  public function sleep($seconds)
136  {
137  if (isset($this->_sleeper))
138  {
139  $this->_sleeper->sleep($seconds);
140  }
141  else
142  {
143  sleep($seconds);
144  }
145  }
146 
147 }