Total Complexity | 91 |
Total Lines | 580 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | Features | 2 |
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 { |
||
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 | * @param mixed $encryption Connection encryption method |
||
37 | */ |
||
38 | public function __construct($cert_validation = true, $encryption = false) { |
||
39 | $this->setCertValidation($cert_validation); |
||
40 | $this->encryption = $encryption; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Public destructor |
||
45 | */ |
||
46 | public function __destruct() { |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Save the information for a nw connection |
||
52 | * @param string $host |
||
53 | * @param null $port |
||
|
|||
54 | */ |
||
55 | public function connect($host, $port = null) { |
||
56 | if ($this->encryption) { |
||
57 | $encryption = strtolower($this->encryption); |
||
58 | if ($encryption == "ssl") { |
||
59 | $port = $port === null ? 993 : $port; |
||
60 | } |
||
61 | } |
||
62 | $port = $port === null ? 143 : $port; |
||
63 | $this->host = $host; |
||
64 | $this->port = $port; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Login to a new session. |
||
69 | * @param string $user username |
||
70 | * @param string $password password |
||
71 | * |
||
72 | * @return bool |
||
73 | * @throws AuthFailedException |
||
74 | * @throws RuntimeException |
||
75 | */ |
||
76 | public function login($user, $password) { |
||
77 | try { |
||
78 | $this->stream = \imap_open( |
||
79 | $this->getAddress(), |
||
80 | $user, |
||
81 | $password, |
||
82 | 0, |
||
83 | $attempts = 3, |
||
84 | ClientManager::get('options.open') |
||
85 | ); |
||
86 | } catch (\ErrorException $e) { |
||
87 | $errors = \imap_errors(); |
||
88 | $message = $e->getMessage().'. '.implode("; ", (is_array($errors) ? $errors : array())); |
||
89 | throw new AuthFailedException($message); |
||
90 | } |
||
91 | |||
92 | if(!$this->stream) { |
||
93 | $errors = \imap_errors(); |
||
94 | $message = implode("; ", (is_array($errors) ? $errors : array())); |
||
95 | throw new AuthFailedException($message); |
||
96 | } |
||
97 | |||
98 | $errors = \imap_errors(); |
||
99 | if(is_array($errors)) { |
||
100 | $status = $this->examineFolder(); |
||
101 | if($status['exists'] !== 0) { |
||
102 | $message = implode("; ", (is_array($errors) ? $errors : array())); |
||
103 | throw new RuntimeException($message); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return $this->stream; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Authenticate your current session. |
||
112 | * @param string $user username |
||
113 | * @param string $token access token |
||
114 | * |
||
115 | * @return bool|resource |
||
116 | * @throws AuthFailedException|RuntimeException |
||
117 | */ |
||
118 | public function authenticate($user, $token) { |
||
119 | return $this->login($user, $token); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get full address of mailbox. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function getAddress() { |
||
128 | $address = "{".$this->host.":".$this->port."/".$this->protocol; |
||
129 | if (!$this->cert_validation) { |
||
130 | $address .= '/novalidate-cert'; |
||
131 | } |
||
132 | if (in_array($this->encryption,['tls', 'notls', 'ssl'])) { |
||
133 | $address .= '/'.$this->encryption; |
||
134 | } elseif ($this->encryption === "starttls") { |
||
135 | $address .= '/tls'; |
||
136 | } |
||
137 | |||
138 | $address .= '}'; |
||
139 | |||
140 | return $address; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Logout of the current session |
||
145 | * |
||
146 | * @return bool success |
||
147 | */ |
||
148 | public function logout() { |
||
149 | if ($this->stream) { |
||
150 | $result = \imap_close($this->stream, IMAP::CL_EXPUNGE); |
||
151 | $this->stream = false; |
||
152 | return $result; |
||
153 | } |
||
154 | return false; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Check if the current session is connected |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function connected(){ |
||
163 | return boolval($this->stream); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get an array of available capabilities |
||
168 | * |
||
169 | * @throws MethodNotSupportedException |
||
170 | */ |
||
171 | public function getCapabilities() { |
||
172 | throw new MethodNotSupportedException(); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Change the current folder |
||
177 | * @param string $folder change to this folder |
||
178 | * |
||
179 | * @return bool|array see examineOrselect() |
||
180 | * @throws RuntimeException |
||
181 | */ |
||
182 | public function selectFolder($folder = 'INBOX') { |
||
183 | \imap_reopen($this->stream, $folder, IMAP::OP_READONLY, 3); |
||
184 | return $this->examineFolder($folder); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Examine a given folder |
||
189 | * @param string $folder examine this folder |
||
190 | * |
||
191 | * @return bool|array |
||
192 | * @throws RuntimeException |
||
193 | */ |
||
194 | public function examineFolder($folder = 'INBOX') { |
||
195 | if (strpos($folder, ".") === 0) { |
||
196 | throw new RuntimeException("Segmentation fault prevented. Folders starts with an illegal char '.'."); |
||
197 | } |
||
198 | $folder = $this->getAddress().$folder; |
||
199 | $status = \imap_status($this->stream, $folder, IMAP::SA_ALL); |
||
200 | return [ |
||
201 | "flags" => [], |
||
202 | "exists" => $status->messages, |
||
203 | "recent" => $status->recent, |
||
204 | "unseen" => $status->unseen, |
||
205 | "uidnext" => $status->uidnext, |
||
206 | ]; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Fetch message content |
||
211 | * @param array|int $uids |
||
212 | * @param string $rfc |
||
213 | * @param bool $uid set to true if passing a unique id |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public function content($uids, $rfc = "RFC822", $uid = false) { |
||
218 | $result = []; |
||
219 | $uids = is_array($uids) ? $uids : [$uids]; |
||
220 | foreach ($uids as $id) { |
||
221 | $result[$id] = \imap_fetchbody($this->stream, $id, "", $uid ? IMAP::FT_UID : IMAP::NIL); |
||
222 | } |
||
223 | return $result; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Fetch message headers |
||
228 | * @param array|int $uids |
||
229 | * @param string $rfc |
||
230 | * @param bool $uid set to true if passing a unique id |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function headers($uids, $rfc = "RFC822", $uid = false){ |
||
235 | $result = []; |
||
236 | $uids = is_array($uids) ? $uids : [$uids]; |
||
237 | foreach ($uids as $id) { |
||
238 | $result[$id] = \imap_fetchheader($this->stream, $id, $uid ? IMAP::FT_UID : IMAP::NIL); |
||
239 | } |
||
240 | return $result; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Fetch message flags |
||
245 | * @param array|int $uids |
||
246 | * @param bool $uid set to true if passing a unique id |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | public function flags($uids, $uid = false){ |
||
251 | $result = []; |
||
252 | $uids = is_array($uids) ? $uids : [$uids]; |
||
253 | foreach ($uids as $id) { |
||
254 | $raw_flags = \imap_fetch_overview($this->stream, $id, $uid ? IMAP::FT_UID : IMAP::NIL); |
||
255 | $flags = []; |
||
256 | if (is_array($raw_flags) && isset($raw_flags[0])) { |
||
257 | $raw_flags = (array) $raw_flags[0]; |
||
258 | foreach($raw_flags as $flag => $value) { |
||
259 | if ($value === 1 && in_array($flag, ["size", "uid", "msgno", "update"]) === false){ |
||
260 | $flags[] = "\\".ucfirst($flag); |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | $result[$uid] = $flags; |
||
265 | } |
||
266 | |||
267 | return $result; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Get uid for a given id |
||
272 | * @param int|null $id message number |
||
273 | * |
||
274 | * @return array|string message number for given message or all messages as array |
||
275 | */ |
||
276 | public function getUid($id = null) { |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Get a message number for a uid |
||
290 | * @param string $id uid |
||
291 | * |
||
292 | * @return int message number |
||
293 | */ |
||
294 | public function getMessageNumber($id) { |
||
295 | return \imap_msgno($this->stream, $id); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get a message overview |
||
300 | * @param string $sequence uid sequence |
||
301 | * @param bool $uid set to true if passing a unique id |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | public function overview($sequence, $uid = false) { |
||
306 | return \imap_fetch_overview($this->stream, $sequence,$uid ? IMAP::FT_UID : IMAP::NIL); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Get a list of available folders |
||
311 | * @param string $reference mailbox reference for list |
||
312 | * @param string $folder mailbox name match with wildcards |
||
313 | * |
||
314 | * @return array folders that matched $folder as array(name => array('delimiter' => .., 'flags' => ..)) |
||
315 | * @throws RuntimeException |
||
316 | */ |
||
317 | public function folders($reference = '', $folder = '*') { |
||
318 | $result = []; |
||
319 | |||
320 | $items = \imap_getmailboxes($this->stream, $this->getAddress(), $reference.$folder); |
||
321 | if(is_array($items)){ |
||
322 | foreach ($items as $item) { |
||
323 | $name = $this->decodeFolderName($item->name); |
||
324 | $result[$name] = ['delimiter' => $item->delimiter, 'flags' => []]; |
||
325 | } |
||
326 | }else{ |
||
327 | throw new RuntimeException(\imap_last_error()); |
||
328 | } |
||
329 | |||
330 | return $result; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Manage flags |
||
335 | * @param array $flags flags to set, add or remove - see $mode |
||
336 | * @param int $from message for items or start message if $to !== null |
||
337 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
338 | * last message, INF means last message available |
||
339 | * @param string|null $mode '+' to add flags, '-' to remove flags, everything else sets the flags as given |
||
340 | * @param bool $silent if false the return values are the new flags for the wanted messages |
||
341 | * @param bool $uid set to true if passing a unique id |
||
342 | * |
||
343 | * @return bool|array new flags if $silent is false, else true or false depending on success |
||
344 | */ |
||
345 | public function store(array $flags, $from, $to = null, $mode = null, $silent = true, $uid = false) { |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Append a new message to given folder |
||
363 | * @param string $folder name of target folder |
||
364 | * @param string $message full message content |
||
365 | * @param array $flags flags for new message |
||
366 | * @param string $date date for new message |
||
367 | * |
||
368 | * @return bool success |
||
369 | */ |
||
370 | public function appendMessage($folder, $message, $flags = null, $date = null) { |
||
371 | if ($date != null) { |
||
372 | if ($date instanceof \Carbon\Carbon){ |
||
373 | $date = $date->format('d-M-Y H:i:s O'); |
||
374 | } |
||
375 | return \imap_append($this->stream, $folder, $message, $flags, $date); |
||
376 | } |
||
377 | |||
378 | return \imap_append($this->stream, $folder, $message, $flags); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Copy message set from current folder to other folder |
||
383 | * @param string $folder destination folder |
||
384 | * @param $from |
||
385 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
386 | * last message, INF means last message available |
||
387 | * @param bool $uid set to true if passing a unique id |
||
388 | * |
||
389 | * @return bool success |
||
390 | */ |
||
391 | public function copyMessage($folder, $from, $to = null, $uid = false) { |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Copy multiple messages to the target folder |
||
397 | * |
||
398 | * @param array<string> $messages List of message identifiers |
||
399 | * @param string $folder Destination folder |
||
400 | * @param bool $uid Set to true if you pass message unique identifiers instead of numbers |
||
401 | * @return array|bool Tokens if operation successful, false if an error occurred |
||
402 | */ |
||
403 | public function copyManyMessages($messages, $folder, $uid = false) { |
||
404 | foreach($messages as $msg) { |
||
405 | if ($this->copyMessage($folder, $msg, null, $uid) == false) { |
||
406 | return false; |
||
407 | } |
||
408 | } |
||
409 | |||
410 | return $messages; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Move a message set from current folder to an other folder |
||
415 | * @param string $folder destination folder |
||
416 | * @param $from |
||
417 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
418 | * last message, INF means last message available |
||
419 | * @param bool $uid set to true if passing a unique id |
||
420 | * |
||
421 | * @return bool success |
||
422 | */ |
||
423 | public function moveMessage($folder, $from, $to = null, $uid = false) { |
||
424 | return \imap_mail_move($this->stream, $from, $folder, $uid ? IMAP::FT_UID : IMAP::NIL); |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Move multiple messages to the target folder |
||
429 | * |
||
430 | * @param array<string> $messages List of message identifiers |
||
431 | * @param string $folder Destination folder |
||
432 | * @param bool $uid Set to true if you pass message unique identifiers instead of numbers |
||
433 | * @return array|bool Tokens if operation successful, false if an error occurred |
||
434 | */ |
||
435 | public function moveManyMessages($messages, $folder, $uid = false) { |
||
436 | foreach($messages as $msg) { |
||
437 | if ($this->moveMessage($folder, $msg, null, $uid) == false) { |
||
438 | return false; |
||
439 | } |
||
440 | } |
||
441 | |||
442 | return $messages; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Create a new folder (and parent folders if needed) |
||
447 | * @param string $folder folder name |
||
448 | * |
||
449 | * @return bool success |
||
450 | */ |
||
451 | public function createFolder($folder) { |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Rename an existing folder |
||
457 | * @param string $old old name |
||
458 | * @param string $new new name |
||
459 | * |
||
460 | * @return bool success |
||
461 | */ |
||
462 | public function renameFolder($old, $new) { |
||
463 | return \imap_renamemailbox($this->stream, $old, $new); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Delete a folder |
||
468 | * @param string $folder folder name |
||
469 | * |
||
470 | * @return bool success |
||
471 | */ |
||
472 | public function deleteFolder($folder) { |
||
473 | return \imap_deletemailbox($this->stream, $folder); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Subscribe to a folder |
||
478 | * @param string $folder folder name |
||
479 | * |
||
480 | * @throws MethodNotSupportedException |
||
481 | */ |
||
482 | public function subscribeFolder($folder) { |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Unsubscribe from a folder |
||
488 | * @param string $folder folder name |
||
489 | * |
||
490 | * @throws MethodNotSupportedException |
||
491 | */ |
||
492 | public function unsubscribeFolder($folder) { |
||
493 | throw new MethodNotSupportedException(); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Apply session saved changes to the server |
||
498 | * |
||
499 | * @return bool success |
||
500 | */ |
||
501 | public function expunge() { |
||
502 | return \imap_expunge($this->stream); |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Send noop command |
||
507 | * |
||
508 | * @throws MethodNotSupportedException |
||
509 | */ |
||
510 | public function noop() { |
||
511 | throw new MethodNotSupportedException(); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Send idle command |
||
516 | * |
||
517 | * @throws MethodNotSupportedException |
||
518 | */ |
||
519 | public function idle() { |
||
520 | throw new MethodNotSupportedException(); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * Send done command |
||
525 | * |
||
526 | * @throws MethodNotSupportedException |
||
527 | */ |
||
528 | public function done() { |
||
529 | throw new MethodNotSupportedException(); |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * Search for matching messages |
||
534 | * |
||
535 | * @param array $params |
||
536 | * @return array message ids |
||
537 | */ |
||
538 | public function search(array $params, $uid = false) { |
||
539 | return \imap_search($this->stream, $params[0], $uid ? IMAP::FT_UID : IMAP::NIL); |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Enable the debug mode |
||
544 | */ |
||
545 | public function enableDebug(){ |
||
546 | $this->debug = true; |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Disable the debug mode |
||
551 | */ |
||
552 | public function disableDebug(){ |
||
553 | $this->debug = false; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Decode name. |
||
558 | * It converts UTF7-IMAP encoding to UTF-8. |
||
559 | * |
||
560 | * @param $name |
||
561 | * |
||
562 | * @return mixed|string |
||
563 | */ |
||
564 | protected function decodeFolderName($name) { |
||
565 | preg_match('#\{(.*)\}(.*)#', $name, $preg); |
||
566 | return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP"); |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * @return string |
||
571 | */ |
||
572 | public function getProtocol() { |
||
573 | return $this->protocol; |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Retrieve the quota level settings, and usage statics per mailbox |
||
578 | * @param $username |
||
579 | * |
||
580 | * @return array |
||
581 | */ |
||
582 | public function getQuota($username) { |
||
583 | return \imap_get_quota($this->stream, 'user.'.$username); |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Retrieve the quota settings per user |
||
588 | * @param string $quota_root |
||
589 | * |
||
590 | * @return array |
||
591 | */ |
||
592 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @param string $protocol |
||
598 | * @return LegacyProtocol |
||
599 | */ |
||
600 | public function setProtocol($protocol) { |
||
606 | } |
||
607 | } |