AntiFloodPlugin.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @package Swift
  12. * @subpackage Plugins
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Plugins_AntiFloodPlugin implements Swift_Events_SendListener, Swift_Plugins_Sleeper
  16. {
  17. /**
  18. * The number of emails to send before restarting Transport.
  19. * @var int
  20. * @access private
  21. */
  22. private $_threshold;
  23. /**
  24. * The number of seconds to sleep for during a restart.
  25. * @var int
  26. * @access private
  27. */
  28. private $_sleep;
  29. /**
  30. * The internal counter.
  31. * @var int
  32. * @access private
  33. */
  34. private $_counter = 0;
  35. /**
  36. * The Sleeper instance for sleeping.
  37. * @var Swift_Plugins_Sleeper
  38. * @access private
  39. */
  40. private $_sleeper;
  41. /**
  42. * Create a new AntiFloodPlugin with $threshold and $sleep time.
  43. * @param int $threshold
  44. * @param int $sleep time
  45. * @param Swift_Plugins_Sleeper $sleeper (not needed really)
  46. */
  47. public function __construct($threshold = 99, $sleep = 0, Swift_Plugins_Sleeper $sleeper = null)
  48. {
  49. $this->setThreshold($threshold);
  50. $this->setSleepTime($sleep);
  51. $this->_sleeper = $sleeper;
  52. }
  53. /**
  54. * Set the number of emails to send before restarting.
  55. * @param int $threshold
  56. */
  57. public function setThreshold($threshold)
  58. {
  59. $this->_threshold = $threshold;
  60. }
  61. /**
  62. * Get the number of emails to send before restarting.
  63. * @return int
  64. */
  65. public function getThreshold()
  66. {
  67. return $this->_threshold;
  68. }
  69. /**
  70. * Set the number of seconds to sleep for during a restart.
  71. * @param int $sleep time
  72. */
  73. public function setSleepTime($sleep)
  74. {
  75. $this->_sleep = $sleep;
  76. }
  77. /**
  78. * Get the number of seconds to sleep for during a restart.
  79. * @return int
  80. */
  81. public function getSleepTime()
  82. {
  83. return $this->_sleep;
  84. }
  85. /**
  86. * Invoked immediately before the Message is sent.
  87. * @param Swift_Events_SendEvent $evt
  88. */
  89. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  90. {
  91. }
  92. /**
  93. * Invoked immediately after the Message is sent.
  94. * @param Swift_Events_SendEvent $evt
  95. */
  96. public function sendPerformed(Swift_Events_SendEvent $evt)
  97. {
  98. ++$this->_counter;
  99. if ($this->_counter >= $this->_threshold) {
  100. $transport = $evt->getTransport();
  101. $transport->stop();
  102. if ($this->_sleep) {
  103. $this->sleep($this->_sleep);
  104. }
  105. $transport->start();
  106. $this->_counter = 0;
  107. }
  108. }
  109. /**
  110. * Sleep for $seconds.
  111. * @param int $seconds
  112. */
  113. public function sleep($seconds)
  114. {
  115. if (isset($this->_sleeper)) {
  116. $this->_sleeper->sleep($seconds);
  117. } else {
  118. sleep($seconds);
  119. }
  120. }
  121. }