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