View file vendor/swiftmailer/swiftmailer/lib/classes/Swift/Plugins/ThrottlerPlugin.php

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