1 | <?php |
||
53 | abstract class AbstractConnection implements ConnectionInterface |
||
54 | { |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | * @var XMLStream |
||
59 | */ |
||
60 | protected $outputStream; |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @var XMLStream |
||
65 | */ |
||
66 | protected $inputStream; |
||
67 | |||
68 | /** |
||
69 | * Options. |
||
70 | * |
||
71 | * @var Options |
||
72 | */ |
||
73 | protected $options; |
||
74 | |||
75 | /** |
||
76 | * Eventmanager. |
||
77 | * |
||
78 | * @var EventManagerInterface |
||
79 | */ |
||
80 | protected $events; |
||
81 | |||
82 | /** |
||
83 | * Event listeners. |
||
84 | * |
||
85 | * @var EventListenerInterface[] |
||
86 | */ |
||
87 | protected $listeners = []; |
||
88 | |||
89 | /** |
||
90 | * Connected. |
||
91 | * |
||
92 | * @var boolean |
||
93 | */ |
||
94 | protected $connected = false; |
||
95 | |||
96 | /** |
||
97 | * |
||
98 | * @var boolean |
||
99 | */ |
||
100 | protected $ready = false; |
||
101 | |||
102 | /** |
||
103 | * Timestamp of last response data received. |
||
104 | * |
||
105 | * @var integer |
||
106 | */ |
||
107 | private $lastResponse; |
||
108 | |||
109 | /** |
||
110 | * Last blocking event listener. |
||
111 | * |
||
112 | * Cached to reduce debug output a bit. |
||
113 | * |
||
114 | * @var BlockingEventListenerInterface |
||
115 | */ |
||
116 | private $lastBlockingListener; |
||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | 3 | public function getOutputStream() |
|
122 | { |
||
123 | 3 | if (null === $this->outputStream) { |
|
124 | 3 | $this->outputStream = new XMLStream(); |
|
125 | 3 | } |
|
126 | |||
127 | 3 | return $this->outputStream; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritDoc} |
||
132 | */ |
||
133 | 3 | public function getInputStream() |
|
134 | { |
||
135 | 3 | if (null === $this->inputStream) { |
|
136 | 3 | $this->inputStream = new XMLStream(); |
|
137 | 3 | } |
|
138 | |||
139 | 3 | return $this->inputStream; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritDoc} |
||
144 | */ |
||
145 | 3 | public function setOutputStream(XMLStream $outputStream) |
|
146 | { |
||
147 | 3 | $this->outputStream = $outputStream; |
|
148 | 3 | return $this; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * {@inheritDoc} |
||
153 | */ |
||
154 | 3 | public function setInputStream(XMLStream $inputStream) |
|
155 | { |
||
156 | 3 | $this->inputStream = $inputStream; |
|
157 | 3 | return $this; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * {@inheritDoc} |
||
162 | */ |
||
163 | 3 | public function addListener(EventListenerInterface $eventListener) |
|
164 | { |
||
165 | 3 | $this->listeners[] = $eventListener; |
|
166 | 3 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritDoc} |
||
171 | */ |
||
172 | 6 | public function isConnected() |
|
173 | { |
||
174 | 6 | return $this->connected; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritDoc} |
||
179 | */ |
||
180 | public function isReady() |
||
181 | { |
||
182 | return $this->ready; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * {@inheritDoc} |
||
187 | */ |
||
188 | public function setReady($flag) |
||
189 | { |
||
190 | $this->ready = (bool) $flag; |
||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Reset streams. |
||
196 | * |
||
197 | * @return void |
||
198 | */ |
||
199 | 3 | public function resetStreams() |
|
200 | { |
||
201 | 3 | $this->getInputStream()->reset(); |
|
202 | 3 | $this->getOutputStream()->reset(); |
|
203 | 3 | } |
|
204 | |||
205 | /** |
||
206 | * {@inheritDoc} |
||
207 | */ |
||
208 | 3 | public function getEventManager() |
|
209 | { |
||
210 | 3 | if (null === $this->events) { |
|
211 | 3 | $this->setEventManager(new EventManager()); |
|
212 | 3 | } |
|
213 | |||
214 | 3 | return $this->events; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritDoc} |
||
219 | */ |
||
220 | 3 | public function setEventManager(EventManagerInterface $events) |
|
221 | { |
||
222 | 3 | $this->events = $events; |
|
223 | 3 | return $this; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Get listeners. |
||
228 | * |
||
229 | * @return EventListenerInterface |
||
230 | */ |
||
231 | 3 | public function getListeners() |
|
232 | { |
||
233 | 3 | return $this->listeners; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * {@inheritDoc} |
||
238 | */ |
||
239 | public function getOptions() |
||
240 | { |
||
241 | return $this->options; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritDoc} |
||
246 | */ |
||
247 | public function setOptions(Options $options) |
||
248 | { |
||
249 | $this->options = $options; |
||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Call logging event. |
||
255 | * |
||
256 | * @param string $message Log message |
||
257 | * @param integer $level Log level |
||
258 | * @return void |
||
259 | */ |
||
260 | 3 | protected function log($message, $level = LogLevel::DEBUG) |
|
261 | { |
||
262 | 3 | $this->getEventManager()->trigger('logger', $this, [$message, $level]); |
|
263 | 3 | } |
|
264 | |||
265 | /** |
||
266 | * Check blocking event listeners. |
||
267 | * |
||
268 | * @return boolean |
||
269 | */ |
||
270 | protected function checkBlockingListeners() |
||
287 | |||
288 | /** |
||
289 | * Check for timeout. |
||
290 | * |
||
291 | * @param string $buffer Function required current received buffer |
||
292 | * @throws TimeoutException |
||
293 | */ |
||
294 | 3 | protected function checkTimeout($buffer) |
|
311 | } |
||
312 |