Removed dep on API
[ninja.git] / application / vendor / swiftmailer / classes / Swift / Plugins / ThrottlerPlugin.php
blob43bb1f48c7cef1e7b2e345300e02b0fe5a9d4605
1 <?php
3 /*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
11 //@require 'Swift/Events/SendEvent.php';
12 //@require 'Swift/Plugins/BandwidthMonitorPlugin.php';
13 //@require 'Swift/Plugins/Sleeper.php';
14 //@require 'Swift/Plugins/Timer.php';
16 /**
17 * Throttles the rate at which emails are sent.
18 * @package Swift
19 * @subpackage Plugins
20 * @author Chris Corbyn
22 class Swift_Plugins_ThrottlerPlugin
23 extends Swift_Plugins_BandwidthMonitorPlugin
24 implements Swift_Plugins_Sleeper, Swift_Plugins_Timer
27 /** Flag for throttling in bytes per minute */
28 const BYTES_PER_MINUTE = 0x01;
30 /** Flag for throttling in emails per minute */
31 const MESSAGES_PER_MINUTE = 0x10;
33 /**
34 * The Sleeper instance for sleeping.
35 * @var Swift_Plugins_Sleeper
36 * @access private
38 private $_sleeper;
40 /**
41 * The Timer instance which provides the timestamp.
42 * @var Swift_Plugins_Timer
43 * @access private
45 private $_timer;
47 /**
48 * The time at which the first email was sent.
49 * @var int
50 * @access private
52 private $_start;
54 /**
55 * The rate at which messages should be sent.
56 * @var int
57 * @access private
59 private $_rate;
61 /**
62 * The mode for throttling.
63 * This is {@link BYTES_PER_MINUTE} or {@link MESSAGES_PER_MINUTE}
64 * @var int
65 * @access private
67 private $_mode;
69 /**
70 * An internal counter of the number of messages sent.
71 * @var int
72 * @access private
74 private $_messages = 0;
76 /**
77 * Create a new ThrottlerPlugin.
78 * @param int $rate
79 * @param int $mode, defaults to {@link BYTES_PER_MINUTE}
80 * @param Swift_Plugins_Sleeper $sleeper (only needed in testing)
81 * @param Swift_Plugins_Timer $timer (only needed in testing)
83 public function __construct($rate, $mode = self::BYTES_PER_MINUTE,
84 Swift_Plugins_Sleeper $sleeper = null, Swift_Plugins_Timer $timer = null)
86 $this->_rate = $rate;
87 $this->_mode = $mode;
88 $this->_sleeper = $sleeper;
89 $this->_timer = $timer;
92 /**
93 * Invoked immediately before the Message is sent.
94 * @param Swift_Events_SendEvent $evt
96 public function beforeSendPerformed(Swift_Events_SendEvent $evt)
98 $time = $this->getTimestamp();
99 if (!isset($this->_start))
101 $this->_start = $time;
103 $duration = $time - $this->_start;
105 if (self::BYTES_PER_MINUTE == $this->_mode)
107 $sleep = $this->_throttleBytesPerMinute($duration);
109 else
111 $sleep = $this->_throttleMessagesPerMinute($duration);
114 if ($sleep > 0)
116 $this->sleep($sleep);
121 * Invoked when a Message is sent.
122 * @param Swift_Events_SendEvent $evt
124 public function sendPerformed(Swift_Events_SendEvent $evt)
126 parent::sendPerformed($evt);
127 ++$this->_messages;
131 * Sleep for $seconds.
132 * @param int $seconds
134 public function sleep($seconds)
136 if (isset($this->_sleeper))
138 $this->_sleeper->sleep($seconds);
140 else
142 sleep($seconds);
147 * Get the current UNIX timestamp
148 * @return int
150 public function getTimestamp()
152 if (isset($this->_timer))
154 return $this->_timer->getTimestamp();
156 else
158 return time();
162 // -- Private methods
165 * Get a number of seconds to sleep for.
166 * @param int $timePassed
167 * @return int
168 * @access private
170 private function _throttleBytesPerMinute($timePassed)
172 $expectedDuration = $this->getBytesOut() / ($this->_rate / 60);
173 return (int) ceil($expectedDuration - $timePassed);
177 * Get a number of seconds to sleep for.
178 * @param int $timePassed
179 * @return int
180 * @access private
182 private function _throttleMessagesPerMinute($timePassed)
184 $expectedDuration = $this->_messages / ($this->_rate / 60);
185 return (int) ceil($expectedDuration - $timePassed);