BandwidthMonitorPlugin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_BandwidthMonitorPlugin implements Swift_Events_SendListener, Swift_Events_CommandListener, Swift_Events_ResponseListener, Swift_InputByteStream
  15. {
  16. /**
  17. * The outgoing traffic counter.
  18. *
  19. * @var int
  20. */
  21. private $out = 0;
  22. /**
  23. * The incoming traffic counter.
  24. *
  25. * @var int
  26. */
  27. private $in = 0;
  28. /** Bound byte streams */
  29. private $mirrors = [];
  30. /**
  31. * Not used.
  32. */
  33. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  34. {
  35. }
  36. /**
  37. * Invoked immediately after the Message is sent.
  38. */
  39. public function sendPerformed(Swift_Events_SendEvent $evt)
  40. {
  41. $message = $evt->getMessage();
  42. $message->toByteStream($this);
  43. }
  44. /**
  45. * Invoked immediately following a command being sent.
  46. */
  47. public function commandSent(Swift_Events_CommandEvent $evt)
  48. {
  49. $command = $evt->getCommand();
  50. $this->out += \strlen($command);
  51. }
  52. /**
  53. * Invoked immediately following a response coming back.
  54. */
  55. public function responseReceived(Swift_Events_ResponseEvent $evt)
  56. {
  57. $response = $evt->getResponse();
  58. $this->in += \strlen($response);
  59. }
  60. /**
  61. * Called when a message is sent so that the outgoing counter can be increased.
  62. *
  63. * @param string $bytes
  64. */
  65. public function write($bytes)
  66. {
  67. $this->out += \strlen($bytes);
  68. foreach ($this->mirrors as $stream) {
  69. $stream->write($bytes);
  70. }
  71. }
  72. /**
  73. * Not used.
  74. */
  75. public function commit()
  76. {
  77. }
  78. /**
  79. * Attach $is to this stream.
  80. *
  81. * The stream acts as an observer, receiving all data that is written.
  82. * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
  83. */
  84. public function bind(Swift_InputByteStream $is)
  85. {
  86. $this->mirrors[] = $is;
  87. }
  88. /**
  89. * Remove an already bound stream.
  90. *
  91. * If $is is not bound, no errors will be raised.
  92. * If the stream currently has any buffered data it will be written to $is
  93. * before unbinding occurs.
  94. */
  95. public function unbind(Swift_InputByteStream $is)
  96. {
  97. foreach ($this->mirrors as $k => $stream) {
  98. if ($is === $stream) {
  99. unset($this->mirrors[$k]);
  100. }
  101. }
  102. }
  103. /**
  104. * Not used.
  105. */
  106. public function flushBuffers()
  107. {
  108. foreach ($this->mirrors as $stream) {
  109. $stream->flushBuffers();
  110. }
  111. }
  112. /**
  113. * Get the total number of bytes sent to the server.
  114. *
  115. * @return int
  116. */
  117. public function getBytesOut()
  118. {
  119. return $this->out;
  120. }
  121. /**
  122. * Get the total number of bytes received from the server.
  123. *
  124. * @return int
  125. */
  126. public function getBytesIn()
  127. {
  128. return $this->in;
  129. }
  130. /**
  131. * Reset the internal counters to zero.
  132. */
  133. public function reset()
  134. {
  135. $this->out = 0;
  136. $this->in = 0;
  137. }
  138. }