AntiFloodPlugin.php 3.1KB

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