Total Complexity | 79 |
Total Lines | 706 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Client { |
||
32 | |||
33 | /** |
||
34 | * @var boolean|resource |
||
35 | */ |
||
36 | public $connection = false; |
||
37 | |||
38 | /** |
||
39 | * Server hostname. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $host; |
||
44 | |||
45 | /** |
||
46 | * Server port. |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | public $port; |
||
51 | |||
52 | /** |
||
53 | * Service protocol. |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | public $protocol; |
||
58 | |||
59 | /** |
||
60 | * Server encryption. |
||
61 | * Supported: none, ssl or tls. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | public $encryption; |
||
66 | |||
67 | /** |
||
68 | * If server has to validate cert. |
||
69 | * |
||
70 | * @var mixed |
||
71 | */ |
||
72 | public $validate_cert; |
||
73 | |||
74 | /** |
||
75 | * Account username/ |
||
76 | * |
||
77 | * @var mixed |
||
78 | */ |
||
79 | public $username; |
||
80 | |||
81 | /** |
||
82 | * Account password. |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | public $password; |
||
87 | |||
88 | /** |
||
89 | * Read only parameter. |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | protected $read_only = false; |
||
94 | |||
95 | /** |
||
96 | * Active folder. |
||
97 | * |
||
98 | * @var Folder |
||
99 | */ |
||
100 | protected $active_folder = false; |
||
101 | |||
102 | /** |
||
103 | * Connected parameter |
||
104 | * |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected $connected = false; |
||
108 | |||
109 | /** |
||
110 | * IMAP errors that might have ben occurred |
||
111 | * |
||
112 | * @var array $errors |
||
113 | */ |
||
114 | protected $errors = []; |
||
115 | |||
116 | /** |
||
117 | * All valid and available account config parameters |
||
118 | * |
||
119 | * @var array $validConfigKeys |
||
120 | */ |
||
121 | protected $valid_config_keys = ['host', 'port', 'encryption', 'validate_cert', 'username', 'password', 'protocol']; |
||
122 | |||
123 | /** |
||
124 | * @var string $default_message_mask |
||
125 | */ |
||
126 | protected $default_message_mask = MessageMask::class; |
||
127 | |||
128 | /** |
||
129 | * @var string $default_attachment_mask |
||
130 | */ |
||
131 | protected $default_attachment_mask = AttachmentMask::class; |
||
132 | |||
133 | /** |
||
134 | * Client constructor. |
||
135 | * @param array $config |
||
136 | * |
||
137 | * @throws MaskNotFoundException |
||
138 | */ |
||
139 | public function __construct($config = []) { |
||
140 | $this->setConfig($config); |
||
141 | $this->setMaskFromConfig($config); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Client destructor |
||
146 | */ |
||
147 | public function __destruct() { |
||
148 | $this->disconnect(); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Set the Client configuration |
||
153 | * |
||
154 | * @param array $config |
||
155 | * |
||
156 | * @return self |
||
157 | */ |
||
158 | public function setConfig(array $config) { |
||
159 | $default_account = config('imap.default'); |
||
160 | $default_config = config("imap.accounts.$default_account"); |
||
161 | |||
162 | foreach ($this->valid_config_keys as $key) { |
||
163 | $this->$key = isset($config[$key]) ? $config[$key] : $default_config[$key]; |
||
164 | } |
||
165 | |||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Look for a possible mask in any available config |
||
171 | * @param $config |
||
172 | * |
||
173 | * @throws MaskNotFoundException |
||
174 | */ |
||
175 | protected function setMaskFromConfig($config) { |
||
176 | $default_config = config("imap.masks"); |
||
177 | |||
178 | if(isset($config['masks'])){ |
||
179 | if(isset($config['masks']['message'])) { |
||
180 | if(class_exists($config['masks']['message'])) { |
||
181 | $this->default_message_mask = $config['masks']['message']; |
||
182 | }else{ |
||
183 | throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['message']); |
||
184 | } |
||
185 | }else{ |
||
186 | if(class_exists($default_config['message'])) { |
||
187 | $this->default_message_mask = $default_config['message']; |
||
188 | }else{ |
||
189 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']); |
||
190 | } |
||
191 | } |
||
192 | if(isset($config['masks']['attachment'])) { |
||
193 | if(class_exists($config['masks']['attachment'])) { |
||
194 | $this->default_message_mask = $config['masks']['attachment']; |
||
195 | }else{ |
||
196 | throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['attachment']); |
||
197 | } |
||
198 | }else{ |
||
199 | if(class_exists($default_config['attachment'])) { |
||
200 | $this->default_message_mask = $default_config['attachment']; |
||
201 | }else{ |
||
202 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']); |
||
203 | } |
||
204 | } |
||
205 | }else{ |
||
206 | if(class_exists($default_config['message'])) { |
||
207 | $this->default_message_mask = $default_config['message']; |
||
208 | }else{ |
||
209 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']); |
||
210 | } |
||
211 | |||
212 | if(class_exists($default_config['attachment'])) { |
||
213 | $this->default_message_mask = $default_config['attachment']; |
||
214 | }else{ |
||
215 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Get the current imap resource |
||
223 | * |
||
224 | * @return bool|resource |
||
225 | * @throws ConnectionFailedException |
||
226 | */ |
||
227 | public function getConnection() { |
||
228 | $this->checkConnection(); |
||
229 | return $this->connection; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Set read only property and reconnect if it's necessary. |
||
234 | * |
||
235 | * @param bool $read_only |
||
236 | * |
||
237 | * @return self |
||
238 | */ |
||
239 | public function setReadOnly($read_only = true) { |
||
240 | $this->read_only = $read_only; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Determine if connection was established. |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function isConnected() { |
||
251 | return $this->connected; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Determine if connection is in read only mode. |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function isReadOnly() { |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Determine if connection was established and connect if not. |
||
265 | * |
||
266 | * @throws ConnectionFailedException |
||
267 | */ |
||
268 | public function checkConnection() { |
||
269 | if (!$this->isConnected() || $this->connection === false) { |
||
270 | $this->connect(); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Connect to server. |
||
276 | * |
||
277 | * @param int $attempts |
||
278 | * |
||
279 | * @return $this |
||
280 | * @throws ConnectionFailedException |
||
281 | */ |
||
282 | public function connect($attempts = 3) { |
||
283 | $this->disconnect(); |
||
284 | |||
285 | try { |
||
286 | $this->connection = imap_open( |
||
287 | $this->getAddress(), |
||
288 | $this->username, |
||
289 | $this->password, |
||
290 | $this->getOptions(), |
||
291 | $attempts, |
||
292 | config('imap.options.open') |
||
293 | ); |
||
294 | $this->connected = !!$this->connection; |
||
295 | } catch (\ErrorException $e) { |
||
296 | $errors = imap_errors(); |
||
297 | $message = $e->getMessage().'. '.implode("; ", (is_array($errors) ? $errors : array())); |
||
|
|||
298 | |||
299 | throw new ConnectionFailedException($message); |
||
300 | } |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Disconnect from server. |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function disconnect() { |
||
311 | if ($this->isConnected() && $this->connection !== false && is_integer($this->connection) === false) { |
||
312 | $this->errors = array_merge($this->errors, imap_errors() ?: []); |
||
313 | $this->connected = !imap_close($this->connection, IMAP::CL_EXPUNGE); |
||
314 | } |
||
315 | |||
316 | return $this; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Get a folder instance by a folder name |
||
321 | * --------------------------------------------- |
||
322 | * PLEASE NOTE: This is an experimental function |
||
323 | * --------------------------------------------- |
||
324 | * @param string $folder_name |
||
325 | * @param int $attributes |
||
326 | * @param null|string $delimiter |
||
327 | * |
||
328 | * @return Folder |
||
329 | */ |
||
330 | public function getFolder($folder_name, $attributes = 32, $delimiter = null) { |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get folders list. |
||
345 | * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
||
346 | * |
||
347 | * @param boolean $hierarchical |
||
348 | * @param string|null $parent_folder |
||
349 | * |
||
350 | * @return FolderCollection |
||
351 | * @throws ConnectionFailedException |
||
352 | * @throws MailboxFetchingException |
||
353 | */ |
||
354 | public function getFolders($hierarchical = true, $parent_folder = null) { |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Open folder. |
||
383 | * |
||
384 | * @param string|Folder $folder_path |
||
385 | * @param int $attempts |
||
386 | * |
||
387 | * @throws ConnectionFailedException |
||
388 | */ |
||
389 | public function openFolder($folder_path, $attempts = 3) { |
||
390 | $this->checkConnection(); |
||
391 | |||
392 | if(property_exists($folder_path, 'path')) { |
||
393 | $folder_path = $folder_path->path; |
||
394 | } |
||
395 | |||
396 | if ($this->active_folder !== $folder_path) { |
||
397 | $this->active_folder = $folder_path; |
||
398 | |||
399 | imap_reopen($this->getConnection(), $folder_path, $this->getOptions(), $attempts); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Create a new Folder |
||
405 | * @param string $name |
||
406 | * @param boolean $expunge |
||
407 | * |
||
408 | * @return bool |
||
409 | * @throws ConnectionFailedException |
||
410 | */ |
||
411 | public function createFolder($name, $expunge = true) { |
||
412 | $this->checkConnection(); |
||
413 | $status = imap_createmailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name)); |
||
414 | if($expunge) $this->expunge(); |
||
415 | |||
416 | return $status; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Rename Folder |
||
421 | * @param string $old_name |
||
422 | * @param string $new_name |
||
423 | * @param boolean $expunge |
||
424 | * |
||
425 | * @return bool |
||
426 | * @throws ConnectionFailedException |
||
427 | */ |
||
428 | public function renameFolder($old_name, $new_name, $expunge = true) { |
||
429 | $this->checkConnection(); |
||
430 | $status = imap_renamemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name)); |
||
431 | if($expunge) $this->expunge(); |
||
432 | |||
433 | return $status; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Delete Folder |
||
438 | * @param string $name |
||
439 | * @param boolean $expunge |
||
440 | * |
||
441 | * @return bool |
||
442 | * @throws ConnectionFailedException |
||
443 | */ |
||
444 | public function deleteFolder($name, $expunge = true) { |
||
445 | $this->checkConnection(); |
||
446 | $status = imap_deletemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name)); |
||
447 | if($expunge) $this->expunge(); |
||
448 | |||
449 | return $status; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Get messages from folder. |
||
454 | * |
||
455 | * @param Folder $folder |
||
456 | * @param string $criteria |
||
457 | * @param int|null $fetch_options |
||
458 | * @param boolean $fetch_body |
||
459 | * @param boolean $fetch_attachment |
||
460 | * @param boolean $fetch_flags |
||
461 | * |
||
462 | * @return MessageCollection |
||
463 | * @throws ConnectionFailedException |
||
464 | * @throws Exceptions\InvalidWhereQueryCriteriaException |
||
465 | * @throws GetMessagesFailedException |
||
466 | * |
||
467 | * @deprecated 1.0.5.2:2.0.0 No longer needed. Use Folder::getMessages() instead |
||
468 | * @see Folder::getMessages() |
||
469 | */ |
||
470 | public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) { |
||
471 | return $folder->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Get all unseen messages from folder |
||
476 | * |
||
477 | * @param Folder $folder |
||
478 | * @param string $criteria |
||
479 | * @param int|null $fetch_options |
||
480 | * @param boolean $fetch_body |
||
481 | * @param boolean $fetch_attachment |
||
482 | * @param boolean $fetch_flags |
||
483 | * |
||
484 | * @return MessageCollection |
||
485 | * @throws ConnectionFailedException |
||
486 | * @throws Exceptions\InvalidWhereQueryCriteriaException |
||
487 | * @throws GetMessagesFailedException |
||
488 | * @throws MessageSearchValidationException |
||
489 | * |
||
490 | * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead |
||
491 | * @see Folder::getMessages() |
||
492 | */ |
||
493 | public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) { |
||
494 | return $folder->getUnseenMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags); |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Search messages by a given search criteria |
||
499 | * |
||
500 | * @param array $where |
||
501 | * @param Folder $folder |
||
502 | * @param int|null $fetch_options |
||
503 | * @param boolean $fetch_body |
||
504 | * @param string $charset |
||
505 | * @param boolean $fetch_attachment |
||
506 | * @param boolean $fetch_flags |
||
507 | * |
||
508 | * @return MessageCollection |
||
509 | * @throws ConnectionFailedException |
||
510 | * @throws Exceptions\InvalidWhereQueryCriteriaException |
||
511 | * @throws GetMessagesFailedException |
||
512 | * |
||
513 | * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::searchMessages() instead |
||
514 | * @see Folder::searchMessages() |
||
515 | * |
||
516 | */ |
||
517 | public function searchMessages(array $where, Folder $folder, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true, $fetch_flags = false) { |
||
518 | return $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment, $fetch_flags); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Get option for imap_open and imap_reopen. |
||
523 | * It supports only isReadOnly feature. |
||
524 | * |
||
525 | * @return int |
||
526 | */ |
||
527 | protected function getOptions() { |
||
528 | return ($this->isReadOnly()) ? IMAP::OP_READONLY : 0; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Get full address of mailbox. |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | protected function getAddress() { |
||
537 | $address = "{".$this->host.":".$this->port."/".($this->protocol ? $this->protocol : 'imap'); |
||
538 | if (!$this->validate_cert) { |
||
539 | $address .= '/novalidate-cert'; |
||
540 | } |
||
541 | if (in_array($this->encryption,['tls','ssl'])) { |
||
542 | $address .= '/'.$this->encryption; |
||
543 | } |
||
544 | |||
545 | $address .= '}'; |
||
546 | |||
547 | return $address; |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Retrieve the quota level settings, and usage statics per mailbox |
||
552 | * |
||
553 | * @return array |
||
554 | * @throws ConnectionFailedException |
||
555 | */ |
||
556 | public function getQuota() { |
||
557 | $this->checkConnection(); |
||
558 | return imap_get_quota($this->getConnection(), 'user.'.$this->username); |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Retrieve the quota settings per user |
||
563 | * |
||
564 | * @param string $quota_root |
||
565 | * |
||
566 | * @return array |
||
567 | * @throws ConnectionFailedException |
||
568 | */ |
||
569 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
570 | $this->checkConnection(); |
||
571 | return imap_get_quotaroot($this->getConnection(), $quota_root); |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Gets the number of messages in the current mailbox |
||
576 | * |
||
577 | * @return int |
||
578 | * @throws ConnectionFailedException |
||
579 | */ |
||
580 | public function countMessages() { |
||
581 | $this->checkConnection(); |
||
582 | return imap_num_msg($this->connection); |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Gets the number of recent messages in current mailbox |
||
587 | * |
||
588 | * @return int |
||
589 | * @throws ConnectionFailedException |
||
590 | */ |
||
591 | public function countRecentMessages() { |
||
592 | $this->checkConnection(); |
||
593 | return imap_num_recent($this->connection); |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * Returns all IMAP alert messages that have occurred |
||
598 | * |
||
599 | * @return array |
||
600 | */ |
||
601 | public function getAlerts() { |
||
602 | return imap_alerts(); |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * Returns all of the IMAP errors that have occurred |
||
607 | * |
||
608 | * @return array |
||
609 | */ |
||
610 | public function getErrors() { |
||
611 | $this->errors = array_merge($this->errors, imap_errors() ?: []); |
||
612 | |||
613 | return $this->errors; |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Gets the last IMAP error that occurred during this page request |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | public function getLastError() { |
||
622 | return imap_last_error(); |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Delete all messages marked for deletion |
||
627 | * |
||
628 | * @return bool |
||
629 | * @throws ConnectionFailedException |
||
630 | */ |
||
631 | public function expunge() { |
||
632 | $this->checkConnection(); |
||
633 | return imap_expunge($this->connection); |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Check current mailbox |
||
638 | * |
||
639 | * @return object { |
||
640 | * Date [string(37) "Wed, 8 Mar 2017 22:17:54 +0100 (CET)"] current system time formatted according to » RFC2822 |
||
641 | * Driver [string(4) "imap"] protocol used to access this mailbox: POP3, IMAP, NNTP |
||
642 | * Mailbox ["{[email protected]:993/imap/user="[email protected]"}INBOX"] the mailbox name |
||
643 | * Nmsgs [int(1)] number of messages in the mailbox |
||
644 | * Recent [int(0)] number of recent messages in the mailbox |
||
645 | * } |
||
646 | * @throws ConnectionFailedException |
||
647 | */ |
||
648 | public function checkCurrentMailbox() { |
||
649 | $this->checkConnection(); |
||
650 | return imap_check($this->connection); |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * Set the imap timeout for a given operation type |
||
655 | * @param $type |
||
656 | * @param $timeout |
||
657 | * |
||
658 | * @return mixed |
||
659 | * @throws InvalidImapTimeoutTypeException |
||
660 | */ |
||
661 | public function setTimeout($type, $timeout) { |
||
662 | if(0 <= $type && $type <= 4) { |
||
663 | return imap_timeout($type, $timeout); |
||
664 | } |
||
665 | |||
666 | throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided."); |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Get the timeout for a certain operation |
||
671 | * @param $type |
||
672 | * |
||
673 | * @return mixed |
||
674 | * @throws InvalidImapTimeoutTypeException |
||
675 | */ |
||
676 | public function getTimeout($type){ |
||
677 | if(0 <= $type && $type <= 4) { |
||
678 | return imap_timeout($type); |
||
679 | } |
||
680 | |||
681 | throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided."); |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * @return string |
||
686 | */ |
||
687 | public function getDefaultMessageMask(){ |
||
688 | return $this->default_message_mask; |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * @param $mask |
||
693 | * |
||
694 | * @return $this |
||
695 | * @throws MaskNotFoundException |
||
696 | */ |
||
697 | public function setDefaultMessageMask($mask) { |
||
705 | } |
||
706 | |||
707 | /** |
||
708 | * @return string |
||
709 | */ |
||
710 | public function getDefaultAttachmentMask(){ |
||
711 | return $this->default_attachment_mask; |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * @param $mask |
||
716 | * |
||
717 | * @return $this |
||
718 | * @throws MaskNotFoundException |
||
719 | */ |
||
720 | public function setDefaultAttachmentMask($mask) { |
||
721 | if(class_exists($mask)) { |
||
722 | $this->default_attachment_mask = $mask; |
||
723 | |||
724 | return $this; |
||
725 | } |
||
726 | |||
727 | throw new MaskNotFoundException("Unknown mask provided: ".$mask); |
||
728 | } |
||
729 | |||
730 | /** |
||
731 | * Get the current active folder |
||
732 | * |
||
733 | * @return Folder |
||
734 | */ |
||
735 | public function getFolderPath(){ |
||
737 | } |
||
738 | } |
||
739 |