BandwidthMonitorPlugin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = array();
  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. * @param Swift_Events_SendEvent $evt
  40. */
  41. public function sendPerformed(Swift_Events_SendEvent $evt)
  42. {
  43. $message = $evt->getMessage();
  44. $message->toByteStream($this);
  45. }
  46. /**
  47. * Invoked immediately following a command being sent.
  48. *
  49. * @param Swift_Events_CommandEvent $evt
  50. */
  51. public function commandSent(Swift_Events_CommandEvent $evt)
  52. {
  53. $command = $evt->getCommand();
  54. $this->_out += strlen($command);
  55. }
  56. /**
  57. * Invoked immediately following a response coming back.
  58. *
  59. * @param Swift_Events_ResponseEvent $evt
  60. */
  61. public function responseReceived(Swift_Events_ResponseEvent $evt)
  62. {
  63. $response = $evt->getResponse();
  64. $this->_in += strlen($response);
  65. }
  66. /**
  67. * Called when a message is sent so that the outgoing counter can be increased.
  68. *
  69. * @param string $bytes
  70. */
  71. public function write($bytes)
  72. {
  73. $this->_out += strlen($bytes);
  74. foreach ($this->_mirrors as $stream) {
  75. $stream->write($bytes);
  76. }
  77. }
  78. /**
  79. * Not used.
  80. */
  81. public function commit()
  82. {
  83. }
  84. /**
  85. * Attach $is to this stream.
  86. *
  87. * The stream acts as an observer, receiving all data that is written.
  88. * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
  89. *
  90. * @param Swift_InputByteStream $is
  91. */
  92. public function bind(Swift_InputByteStream $is)
  93. {
  94. $this->_mirrors[] = $is;
  95. }
  96. /**
  97. * Remove an already bound stream.
  98. *
  99. * If $is is not bound, no errors will be raised.
  100. * If the stream currently has any buffered data it will be written to $is
  101. * before unbinding occurs.
  102. *
  103. * @param Swift_InputByteStream $is
  104. */
  105. public function unbind(Swift_InputByteStream $is)
  106. {
  107. foreach ($this->_mirrors as $k => $stream) {
  108. if ($is === $stream) {
  109. unset($this->_mirrors[$k]);
  110. }
  111. }
  112. }
  113. /**
  114. * Not used.
  115. */
  116. public function flushBuffers()
  117. {
  118. foreach ($this->_mirrors as $stream) {
  119. $stream->flushBuffers();
  120. }
  121. }
  122. /**
  123. * Get the total number of bytes sent to the server.
  124. *
  125. * @return int
  126. */
  127. public function getBytesOut()
  128. {
  129. return $this->_out;
  130. }
  131. /**
  132. * Get the total number of bytes received from the server.
  133. *
  134. * @return int
  135. */
  136. public function getBytesIn()
  137. {
  138. return $this->_in;
  139. }
  140. /**
  141. * Reset the internal counters to zero.
  142. */
  143. public function reset()
  144. {
  145. $this->_out = 0;
  146. $this->_in = 0;
  147. }
  148. }