AntiFloodPlugin.php 3.1 KB

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