Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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/SendEvent.php';
12
//@require 'Swift/Plugins/BandwidthMonitorPlugin.php';
13
//@require 'Swift/Plugins/Sleeper.php';
14
//@require 'Swift/Plugins/Timer.php';
15
 
16
/**
17
 * Throttles the rate at which emails are sent.
18
 * @package Swift
19
 * @subpackage Plugins
20
 * @author Chris Corbyn
21
 */
22
class Swift_Plugins_ThrottlerPlugin
23
  extends Swift_Plugins_BandwidthMonitorPlugin
24
  implements Swift_Plugins_Sleeper, Swift_Plugins_Timer
25
{
26
 
27
  /** Flag for throttling in bytes per minute */
28
  const BYTES_PER_MINUTE = 0x01;
29
 
30
  /** Flag for throttling in emails per minute */
31
  const MESSAGES_PER_MINUTE = 0x10;
32
 
33
  /**
34
   * The Sleeper instance for sleeping.
35
   * @var Swift_Plugins_Sleeper
36
   * @access private
37
   */
38
  private $_sleeper;
39
 
40
  /**
41
   * The Timer instance which provides the timestamp.
42
   * @var Swift_Plugins_Timer
43
   * @access private
44
   */
45
  private $_timer;
46
 
47
  /**
48
   * The time at which the first email was sent.
49
   * @var int
50
   * @access private
51
   */
52
  private $_start;
53
 
54
  /**
55
   * The rate at which messages should be sent.
56
   * @var int
57
   * @access private
58
   */
59
  private $_rate;
60
 
61
  /**
62
   * The mode for throttling.
63
   * This is {@link BYTES_PER_MINUTE} or {@link MESSAGES_PER_MINUTE}
64
   * @var int
65
   * @access private
66
   */
67
  private $_mode;
68
 
69
  /**
70
   * An internal counter of the number of messages sent.
71
   * @var int
72
   * @access private
73
   */
74
  private $_messages = 0;
75
 
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)
82
   */
83
  public function __construct($rate, $mode = self::BYTES_PER_MINUTE,
84
    Swift_Plugins_Sleeper $sleeper = null, Swift_Plugins_Timer $timer = null)
85
  {
86
    $this->_rate = $rate;
87
    $this->_mode = $mode;
88
    $this->_sleeper = $sleeper;
89
    $this->_timer = $timer;
90
  }
91
 
92
  /**
93
   * Invoked immediately before the Message is sent.
94
   * @param Swift_Events_SendEvent $evt
95
   */
96
  public function beforeSendPerformed(Swift_Events_SendEvent $evt)
97
  {
98
    $time = $this->getTimestamp();
99
    if (!isset($this->_start))
100
    {
101
      $this->_start = $time;
102
    }
103
    $duration = $time - $this->_start;
104
 
105
    if (self::BYTES_PER_MINUTE == $this->_mode)
106
    {
107
      $sleep = $this->_throttleBytesPerMinute($duration);
108
    }
109
    else
110
    {
111
      $sleep = $this->_throttleMessagesPerMinute($duration);
112
    }
113
 
114
    if ($sleep > 0)
115
    {
116
      $this->sleep($sleep);
117
    }
118
  }
119
 
120
  /**
121
   * Invoked when a Message is sent.
122
   * @param Swift_Events_SendEvent $evt
123
   */
124
  public function sendPerformed(Swift_Events_SendEvent $evt)
125
  {
126
    parent::sendPerformed($evt);
127
    ++$this->_messages;
128
  }
129
 
130
  /**
131
   * Sleep for $seconds.
132
   * @param int $seconds
133
   */
134
  public function sleep($seconds)
135
  {
136
    if (isset($this->_sleeper))
137
    {
138
      $this->_sleeper->sleep($seconds);
139
    }
140
    else
141
    {
142
      sleep($seconds);
143
    }
144
  }
145
 
146
  /**
147
   * Get the current UNIX timestamp
148
   * @return int
149
   */
150
  public function getTimestamp()
151
  {
152
    if (isset($this->_timer))
153
    {
154
      return $this->_timer->getTimestamp();
155
    }
156
    else
157
    {
158
      return time();
159
    }
160
  }
161
 
162
  // -- Private methods
163
 
164
  /**
165
   * Get a number of seconds to sleep for.
166
   * @param int $timePassed
167
   * @return int
168
   * @access private
169
   */
170
  private function _throttleBytesPerMinute($timePassed)
171
  {
172
    $expectedDuration = $this->getBytesOut() / ($this->_rate / 60);
173
    return (int) ceil($expectedDuration - $timePassed);
174
  }
175
 
176
  /**
177
   * Get a number of seconds to sleep for.
178
   * @param int $timePassed
179
   * @return int
180
   * @access private
181
   */
182
  private function _throttleMessagesPerMinute($timePassed)
183
  {
184
    $expectedDuration = $this->_messages / ($this->_rate / 60);
185
    return (int) ceil($expectedDuration - $timePassed);
186
  }
187
 
188
}