1<?php 2 3/** 4 * Swift Mailer Bandwidth Monitoring Plugin 5 * Please read the LICENSE file 6 * @author Chris Corbyn <chris@w3style.co.uk> 7 * @package Swift_Plugin 8 * @license GNU Lesser General Public License 9 */ 10 11require_once dirname(__FILE__) . "/../ClassLoader.php"; 12Swift_ClassLoader::load("Swift_Events_CommandListener"); 13Swift_ClassLoader::load("Swift_Events_ResponseListener"); 14 15/** 16 * Swift Bandwidth Monitor. 17 * Tracks bytes in and out of the connection. 18 * @package Swift_Plugin 19 * @author Chris Corbyn <chris@w3style.co.uk> 20 */ 21class Swift_Plugin_BandwidthMonitor implements Swift_Events_CommandListener, Swift_Events_ResponseListener 22{ 23 /** 24 * The number of bytes received 25 * @var int 26 */ 27 protected $in = 0; 28 /** 29 * The number of bytes sent 30 * @var int 31 */ 32 protected $out = 0; 33 34 /** 35 * Part of the interface which is notified after a command is sent. 36 * @param Swift_Events_CommandEvent 37 */ 38 public function commandSent(Swift_Events_CommandEvent $e) 39 { 40 $code = $e->getCode(); 41 $add = 0; 42 if ($code != -1) $add = 2; 43 $bytes = strlen($e->getString()) + $add; 44 $this->addBytesOut($bytes); 45 } 46 /** 47 * Part of the interface which is notified when a response is received 48 * @param Swift_Events_ResponseEvent 49 */ 50 public function responseReceived(Swift_Events_ResponseEvent $e) 51 { 52 $bytes = strlen($e->getString()) + 2; 53 $this->addBytesIn($bytes); 54 } 55 /** 56 * Add some bytes to the running totals for incoming bandwidth 57 * @param int Bytes in 58 */ 59 public function addBytesIn($num) 60 { 61 $num = abs((int)$num); 62 $this->setBytesIn($this->getBytesIn() + $num); 63 } 64 /** 65 * Add some bytes to the running totals for outgoing bandwidth 66 * @param int Bytes out 67 */ 68 public function addBytesOut($num) 69 { 70 $num = abs((int)$num); 71 $this->setBytesOut($this->getBytesOut() + $num); 72 } 73 /** 74 * Get the total number of bytes received 75 * @return int 76 */ 77 public function getBytesIn() 78 { 79 return $this->in; 80 } 81 /** 82 * Get the total number of bytes sent 83 * @return int 84 */ 85 public function getBytesOut() 86 { 87 return $this->out; 88 } 89 /** 90 * Set the total number of bytes received. 91 * Can be used to reset the counters at runtime. 92 * @param int The bytes in 93 */ 94 public function setBytesIn($num) 95 { 96 $this->in = abs((int)$num); 97 } 98 /** 99 * Set the total number of bytes sent. 100 * Can be used to reset the counters at runtime. 101 * @param int The bytes out 102 */ 103 public function setBytesOut($num) 104 { 105 $this->out = abs((int)$num); 106 } 107} 108