AntiFloodPlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  93. {
  94. }
  95. /**
  96. * Invoked immediately after the Message is sent.
  97. */
  98. public function sendPerformed(Swift_Events_SendEvent $evt)
  99. {
  100. ++$this->counter;
  101. if ($this->counter >= $this->threshold) {
  102. $transport = $evt->getTransport();
  103. $transport->stop();
  104. if ($this->sleep) {
  105. $this->sleep($this->sleep);
  106. }
  107. $transport->start();
  108. $this->counter = 0;
  109. }
  110. }
  111. /**
  112. * Sleep for $seconds.
  113. *
  114. * @param int $seconds
  115. */
  116. public function sleep($seconds)
  117. {
  118. if (isset($this->sleeper)) {
  119. $this->sleeper->sleep($seconds);
  120. } else {
  121. sleep($seconds);
  122. }
  123. }
  124. }