Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / typo3 / contrib / swiftmailer / classes / Swift / Plugins / BandwidthMonitorPlugin.php
1 <?php
2
3 /*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 //@require 'Swift/Events/SendListener.php';
12 //@require 'Swift/Events/SendEvent.php';
13 //@require 'Swift/Events/CommandListener.php';
14 //@require 'Swift/Events/CommandEvent.php';
15 //@require 'Swift/Events/ResponseListener.php';
16 //@require 'Swift/Events/ResponseEvent.php';
17 //@require 'Swift/InputByteStream.php';
18
19 /**
20 * Reduces network flooding when sending large amounts of mail.
21 * @package Swift
22 * @subpackage Plugins
23 * @author Chris Corbyn
24 */
25 class Swift_Plugins_BandwidthMonitorPlugin
26 implements Swift_Events_SendListener, Swift_Events_CommandListener,
27 Swift_Events_ResponseListener, Swift_InputByteStream
28 {
29
30 /**
31 * The outgoing traffic counter.
32 * @var int
33 * @access private
34 */
35 private $_out = 0;
36
37 /**
38 * The incoming traffic counter.
39 * @var int
40 * @access private
41 */
42 private $_in = 0;
43
44 /** Bound byte streams */
45 private $_mirrors = array();
46
47 /**
48 * Not used.
49 */
50 public function beforeSendPerformed(Swift_Events_SendEvent $evt)
51 {
52 }
53
54 /**
55 * Invoked immediately after the Message is sent.
56 * @param Swift_Events_SendEvent $evt
57 */
58 public function sendPerformed(Swift_Events_SendEvent $evt)
59 {
60 $message = $evt->getMessage();
61 $message->toByteStream($this);
62 }
63
64 /**
65 * Invoked immediately following a command being sent.
66 * @param Swift_Events_ResponseEvent $evt
67 */
68 public function commandSent(Swift_Events_CommandEvent $evt)
69 {
70 $command = $evt->getCommand();
71 $this->_out += strlen($command);
72 }
73
74 /**
75 * Invoked immediately following a response coming back.
76 * @param Swift_Events_ResponseEvent $evt
77 */
78 public function responseReceived(Swift_Events_ResponseEvent $evt)
79 {
80 $response = $evt->getResponse();
81 $this->_in += strlen($response);
82 }
83
84 /**
85 * Called when a message is sent so that the outgoing counter can be increased.
86 * @param string $bytes
87 */
88 public function write($bytes)
89 {
90 $this->_out += strlen($bytes);
91 foreach ($this->_mirrors as $stream)
92 {
93 $stream->write($bytes);
94 }
95 }
96
97 /**
98 * Not used.
99 */
100 public function commit()
101 {
102 }
103
104 /**
105 * Attach $is to this stream.
106 * The stream acts as an observer, receiving all data that is written.
107 * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
108 *
109 * @param Swift_InputByteStream $is
110 */
111 public function bind(Swift_InputByteStream $is)
112 {
113 $this->_mirrors[] = $is;
114 }
115
116 /**
117 * Remove an already bound stream.
118 * If $is is not bound, no errors will be raised.
119 * If the stream currently has any buffered data it will be written to $is
120 * before unbinding occurs.
121 *
122 * @param Swift_InputByteStream $is
123 */
124 public function unbind(Swift_InputByteStream $is)
125 {
126 foreach ($this->_mirrors as $k => $stream)
127 {
128 if ($is === $stream)
129 {
130 unset($this->_mirrors[$k]);
131 }
132 }
133 }
134
135 /**
136 * Not used.
137 */
138 public function flushBuffers()
139 {
140 foreach ($this->_mirrors as $stream)
141 {
142 $stream->flushBuffers();
143 }
144 }
145
146 /**
147 * Get the total number of bytes sent to the server.
148 * @return int
149 */
150 public function getBytesOut()
151 {
152 return $this->_out;
153 }
154
155 /**
156 * Get the total number of bytes received from the server.
157 * @return int
158 */
159 public function getBytesIn()
160 {
161 return $this->_in;
162 }
163
164 /**
165 * Reset the internal counters to zero.
166 */
167 public function reset()
168 {
169 $this->_out = 0;
170 $this->_in = 0;
171 }
172
173 }