Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / typo3 / contrib / swiftmailer / classes / Swift / Plugins / AntiFloodPlugin.php
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
15 /**
16 * Reduces network flooding when sending large amounts of mail.
17 * @package Swift
18 * @subpackage Plugins
19 * @author Chris Corbyn
20 */
21 class Swift_Plugins_AntiFloodPlugin
22 implements Swift_Events_SendListener, Swift_Plugins_Sleeper
23 {
24
25 /**
26 * The number of emails to send before restarting Transport.
27 * @var int
28 * @access private
29 */
30 private $_threshold;
31
32 /**
33 * The number of seconds to sleep for during a restart.
34 * @var int
35 * @access private
36 */
37 private $_sleep;
38
39 /**
40 * The internal counter.
41 * @var int
42 * @access private
43 */
44 private $_counter = 0;
45
46 /**
47 * The Sleeper instance for sleeping.
48 * @var Swift_Plugins_Sleeper
49 * @access private
50 */
51 private $_sleeper;
52
53 /**
54 * Create a new AntiFloodPlugin with $threshold and $sleep time.
55 * @param int $threshold
56 * @param int $sleep time
57 * @param Swift_Plugins_Sleeper $sleeper (not needed really)
58 */
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
67 /**
68 * Set the number of emails to send before restarting.
69 * @param int $threshold
70 */
71 public function setThreshold($threshold)
72 {
73 $this->_threshold = $threshold;
74 }
75
76 /**
77 * Get the number of emails to send before restarting.
78 * @return int
79 */
80 public function getThreshold()
81 {
82 return $this->_threshold;
83 }
84
85 /**
86 * Set the number of seconds to sleep for during a restart.
87 * @param int $sleep time
88 */
89 public function setSleepTime($sleep)
90 {
91 $this->_sleep = $sleep;
92 }
93
94 /**
95 * Get the number of seconds to sleep for during a restart.
96 * @return int
97 */
98 public function getSleepTime()
99 {
100 return $this->_sleep;
101 }
102
103 /**
104 * Invoked immediately before the Message is sent.
105 * @param Swift_Events_SendEvent $evt
106 */
107 public function beforeSendPerformed(Swift_Events_SendEvent $evt)
108 {
109 }
110
111 /**
112 * Invoked immediately after the Message is sent.
113 * @param Swift_Events_SendEvent $evt
114 */
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
131 /**
132 * Sleep for $seconds.
133 * @param int $seconds
134 */
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 }