Total Complexity | 64 |
Total Lines | 505 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like LegacyProtocol often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LegacyProtocol, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class LegacyProtocol extends Protocol implements ProtocolInterface { |
||
27 | |||
28 | protected $protocol = "imap"; |
||
29 | protected $host = null; |
||
30 | protected $port = null; |
||
31 | protected $encryption = null; |
||
32 | |||
33 | /** |
||
34 | * Imap constructor. |
||
35 | * @param bool $cert_validation set to false to skip SSL certificate validation |
||
36 | */ |
||
37 | public function __construct($cert_validation = true) { |
||
38 | $this->setCertValidation($cert_validation); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Public destructor |
||
43 | */ |
||
44 | public function __destruct() { |
||
45 | $this->logout(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Save the information for a nw connection |
||
50 | * @param string $host |
||
51 | * @param null $port |
||
|
|||
52 | * @param bool $encryption |
||
53 | */ |
||
54 | public function connect($host, $port = null, $encryption = false) { |
||
55 | if ($port === null) { |
||
56 | $port = $encryption == "ssl" ? 995 : 110; |
||
57 | } |
||
58 | $this->host = $host; |
||
59 | $this->port = $port; |
||
60 | $this->encryption = $encryption; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Login to a new session. |
||
65 | * @param string $user username |
||
66 | * @param string $password password |
||
67 | * |
||
68 | * @return bool |
||
69 | * @throws AuthFailedException |
||
70 | */ |
||
71 | public function login($user, $password) { |
||
72 | try { |
||
73 | $this->stream = \imap_open( |
||
74 | $this->getAddress(), |
||
75 | $user, |
||
76 | $password, |
||
77 | IMAP::OP_READONLY, |
||
78 | $attempts = 3, |
||
79 | ClientManager::get('options.open') |
||
80 | ); |
||
81 | } catch (\ErrorException $e) { |
||
82 | $errors = \imap_errors(); |
||
83 | $message = $e->getMessage().'. '.implode("; ", (is_array($errors) ? $errors : array())); |
||
84 | |||
85 | throw new AuthFailedException($message); |
||
86 | } |
||
87 | |||
88 | if(!$this->stream) { |
||
89 | $errors = \imap_errors(); |
||
90 | $message = implode("; ", (is_array($errors) ? $errors : array())); |
||
91 | throw new AuthFailedException($message); |
||
92 | } |
||
93 | |||
94 | return !$this->stream; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Authenticate your current session. |
||
99 | * @param string $user username |
||
100 | * @param string $token access token |
||
101 | * |
||
102 | * @return bool |
||
103 | * @throws AuthFailedException |
||
104 | */ |
||
105 | public function authenticate($user, $token) { |
||
106 | return $this->login($user, $token); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get full address of mailbox. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | protected function getAddress() { |
||
115 | $address = "{".$this->host.":".$this->port."/".$this->protocol; |
||
116 | if (!$this->cert_validation) { |
||
117 | $address .= '/novalidate-cert'; |
||
118 | } |
||
119 | if (in_array($this->encryption,['tls', 'notls', 'ssl'])) { |
||
120 | $address .= '/'.$this->encryption; |
||
121 | } elseif ($this->encryption === "starttls") { |
||
122 | $address .= '/tls'; |
||
123 | } |
||
124 | |||
125 | $address .= '}'; |
||
126 | |||
127 | return $address; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Logout of the current session |
||
132 | * |
||
133 | * @return bool success |
||
134 | */ |
||
135 | public function logout() { |
||
136 | if ($this->stream) { |
||
137 | return \imap_close($this->stream, IMAP::CL_EXPUNGE); |
||
138 | } |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Check if the current session is connected |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function connected(){ |
||
148 | return !$this->stream; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get an array of available capabilities |
||
153 | * |
||
154 | * @throws MethodNotSupportedException |
||
155 | */ |
||
156 | public function getCapabilities() { |
||
157 | throw new MethodNotSupportedException(); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Change the current folder |
||
162 | * @param string $folder change to this folder |
||
163 | * |
||
164 | * @return bool|array see examineOrselect() |
||
165 | * @throws RuntimeException |
||
166 | */ |
||
167 | public function selectFolder($folder = 'INBOX') { |
||
168 | \imap_reopen($this->stream, $folder, IMAP::OP_READONLY, 3); |
||
169 | return $this->examineFolder($folder); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Examine a given folder |
||
174 | * @param string $folder examine this folder |
||
175 | * |
||
176 | * @return bool|array see examineOrselect() |
||
177 | * @throws RuntimeException |
||
178 | */ |
||
179 | public function examineFolder($folder = 'INBOX') { |
||
180 | if (strpos($folder, ".") === 0) { |
||
181 | throw new RuntimeException("Segmentation fault prevented. Folders starts with an illegal char '.'."); |
||
182 | } |
||
183 | $folder = $this->getAddress().$folder; |
||
184 | $status = \imap_status($this->stream, $folder, IMAP::SA_ALL); |
||
185 | return [ |
||
186 | "flags" => [], |
||
187 | "exists" => $status->messages, |
||
188 | "recent" => $status->recent, |
||
189 | "unseen" => $status->unseen, |
||
190 | "uidnext" => $status->uidnext, |
||
191 | ]; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Fetch message content |
||
196 | * @param array|int $uids |
||
197 | * @param string $rfc |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function content($uids, $rfc = "RFC822") { |
||
202 | $content = \imap_fetchbody($this->stream, $uids[0], "", IMAP::FT_UID); |
||
203 | return [$uids[0] => $content]; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Fetch message headers |
||
208 | * @param array|int $uids |
||
209 | * @param string $rfc |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function headers($uids, $rfc = "RFC822"){ |
||
214 | $headers = \imap_fetchheader($this->stream, $uids[0], IMAP::FT_UID); |
||
215 | return [$uids[0] => $headers]; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Fetch message flags |
||
220 | * @param array|int $uids |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function flags($uids){ |
||
225 | $flags = \imap_fetch_overview($this->stream, $uids[0], IMAP::FT_UID); |
||
226 | $result = []; |
||
227 | if (is_array($flags) && isset($flags[0])) { |
||
228 | $flags = (array) $flags[0]; |
||
229 | foreach($flags as $flag => $value) { |
||
230 | if ($value === 1 && in_array($flag, ["size", "uid", "msgno", "update"]) === false){ |
||
231 | $result[] = "\\".ucfirst($flag); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | return [$uids[0] => $result]; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get uid for a given id |
||
240 | * @param int|null $id message number |
||
241 | * |
||
242 | * @return array|string message number for given message or all messages as array |
||
243 | */ |
||
244 | public function getUid($id = null) { |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Get a message number for a uid |
||
258 | * @param string $id uid |
||
259 | * |
||
260 | * @return int message number |
||
261 | */ |
||
262 | public function getMessageNumber($id) { |
||
263 | return \imap_msgno($this->stream, $id); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Get a message number for a uid |
||
268 | * @param string $sequence uid sequence |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | public function overview($sequence) { |
||
273 | return \imap_fetch_overview($this->stream, $sequence); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Get a list of available folders |
||
278 | * @param string $reference mailbox reference for list |
||
279 | * @param string $folder mailbox name match with wildcards |
||
280 | * |
||
281 | * @return array folders that matched $folder as array(name => array('delimiter' => .., 'flags' => ..)) |
||
282 | * @throws RuntimeException |
||
283 | */ |
||
284 | public function folders($reference = '', $folder = '*') { |
||
285 | $result = []; |
||
286 | |||
287 | $items = \imap_getmailboxes($this->stream, $this->getAddress(), $reference.$folder); |
||
288 | if(is_array($items)){ |
||
289 | foreach ($items as $item) { |
||
290 | $name = $this->decodeFolderName($item->name); |
||
291 | $result[$name] = ['delimiter' => $item->delimiter, 'flags' => []]; |
||
292 | } |
||
293 | }else{ |
||
294 | throw new RuntimeException(\imap_last_error()); |
||
295 | } |
||
296 | |||
297 | return $result; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Manage flags |
||
302 | * @param array $flags flags to set, add or remove - see $mode |
||
303 | * @param int $from message for items or start message if $to !== null |
||
304 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
305 | * last message, INF means last message available |
||
306 | * @param string|null $mode '+' to add flags, '-' to remove flags, everything else sets the flags as given |
||
307 | * @param bool $silent if false the return values are the new flags for the wanted messages |
||
308 | * |
||
309 | * @return bool|array new flags if $silent is false, else true or false depending on success |
||
310 | */ |
||
311 | public function store(array $flags, $from, $to = null, $mode = null, $silent = true) { |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Append a new message to given folder |
||
329 | * @param string $folder name of target folder |
||
330 | * @param string $message full message content |
||
331 | * @param array $flags flags for new message |
||
332 | * @param string $date date for new message |
||
333 | * |
||
334 | * @return bool success |
||
335 | */ |
||
336 | public function appendMessage($folder, $message, $flags = null, $date = null) { |
||
337 | if ($date != null) { |
||
338 | if ($date instanceof \Carbon\Carbon){ |
||
339 | $date = $date->format('d-M-Y H:i:s O'); |
||
340 | } |
||
341 | return \imap_append($this->stream, $folder, $message, $flags, $date); |
||
342 | } |
||
343 | |||
344 | return \imap_append($this->stream, $folder, $message, $flags); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Copy message set from current folder to other folder |
||
349 | * @param string $folder destination folder |
||
350 | * @param $from |
||
351 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
352 | * last message, INF means last message available |
||
353 | * |
||
354 | * @return bool success |
||
355 | */ |
||
356 | public function copyMessage($folder, $from, $to = null) { |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Move a message set from current folder to an other folder |
||
362 | * @param string $folder destination folder |
||
363 | * @param $from |
||
364 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
365 | * last message, INF means last message available |
||
366 | * |
||
367 | * @return bool success |
||
368 | */ |
||
369 | public function moveMessage($folder, $from, $to = null) { |
||
370 | return \imap_mail_move($this->stream, $from, $folder, IMAP::CP_UID); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Create a new folder (and parent folders if needed) |
||
375 | * @param string $folder folder name |
||
376 | * |
||
377 | * @return bool success |
||
378 | */ |
||
379 | public function createFolder($folder) { |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Rename an existing folder |
||
385 | * @param string $old old name |
||
386 | * @param string $new new name |
||
387 | * |
||
388 | * @return bool success |
||
389 | */ |
||
390 | public function renameFolder($old, $new) { |
||
391 | return \imap_renamemailbox($this->stream, $old, $new); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Delete a folder |
||
396 | * @param string $folder folder name |
||
397 | * |
||
398 | * @return bool success |
||
399 | */ |
||
400 | public function deleteFolder($folder) { |
||
401 | return \imap_deletemailbox($this->stream, $folder); |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Subscribe to a folder |
||
406 | * @param string $folder folder name |
||
407 | * |
||
408 | * @throws MethodNotSupportedException |
||
409 | */ |
||
410 | public function subscribeFolder($folder) { |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Unsubscribe from a folder |
||
416 | * @param string $folder folder name |
||
417 | * |
||
418 | * @throws MethodNotSupportedException |
||
419 | */ |
||
420 | public function unsubscribeFolder($folder) { |
||
421 | throw new MethodNotSupportedException(); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Apply session saved changes to the server |
||
426 | * |
||
427 | * @return bool success |
||
428 | */ |
||
429 | public function expunge() { |
||
430 | return \imap_expunge($this->stream); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Send noop command |
||
435 | * |
||
436 | * @throws MethodNotSupportedException |
||
437 | */ |
||
438 | public function noop() { |
||
439 | throw new MethodNotSupportedException(); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Send idle command |
||
444 | * |
||
445 | * @throws MethodNotSupportedException |
||
446 | */ |
||
447 | public function idle() { |
||
448 | throw new MethodNotSupportedException(); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Send done command |
||
453 | * |
||
454 | * @throws MethodNotSupportedException |
||
455 | */ |
||
456 | public function done() { |
||
457 | throw new MethodNotSupportedException(); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Search for matching messages |
||
462 | * |
||
463 | * @param array $params |
||
464 | * @return array message ids |
||
465 | */ |
||
466 | public function search(array $params) { |
||
467 | return \imap_search($this->stream, $params[0], IMAP::SE_UID); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Enable the debug mode |
||
472 | */ |
||
473 | public function enableDebug(){ |
||
474 | $this->debug = true; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Disable the debug mode |
||
479 | */ |
||
480 | public function disableDebug(){ |
||
481 | $this->debug = false; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Decode name. |
||
486 | * It converts UTF7-IMAP encoding to UTF-8. |
||
487 | * |
||
488 | * @param $name |
||
489 | * |
||
490 | * @return mixed|string |
||
491 | */ |
||
492 | protected function decodeFolderName($name) { |
||
493 | preg_match('#\{(.*)\}(.*)#', $name, $preg); |
||
494 | return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP"); |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * @return string |
||
499 | */ |
||
500 | public function getProtocol() { |
||
501 | return $this->protocol; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Retrieve the quota level settings, and usage statics per mailbox |
||
506 | * @param $username |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | public function getQuota($username) { |
||
511 | return \imap_get_quota($this->stream, 'user.'.$username); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Retrieve the quota settings per user |
||
516 | * @param string $quota_root |
||
517 | * |
||
518 | * @return array |
||
519 | */ |
||
520 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * @param string $protocol |
||
526 | * @return LegacyProtocol |
||
527 | */ |
||
528 | public function setProtocol($protocol) { |
||
531 | } |
||
532 | } |