Total Complexity | 77 |
Total Lines | 623 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 1 |
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 |
||
34 | class Client { |
||
35 | use HasEvents; |
||
36 | |||
37 | /** |
||
38 | * Connection resource |
||
39 | * |
||
40 | * @var boolean|Protocol|ProtocolInterface |
||
41 | */ |
||
42 | public $connection = false; |
||
43 | |||
44 | /** |
||
45 | * Server hostname. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | public $host; |
||
50 | |||
51 | /** |
||
52 | * Server port. |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | public $port; |
||
57 | |||
58 | /** |
||
59 | * Service protocol. |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | public $protocol; |
||
64 | |||
65 | /** |
||
66 | * Server encryption. |
||
67 | * Supported: none, ssl, tls, or notls. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public $encryption; |
||
72 | |||
73 | /** |
||
74 | * If server has to validate cert. |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | public $validate_cert = true; |
||
79 | |||
80 | /** |
||
81 | * Proxy settings |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $proxy = [ |
||
85 | 'socket' => null, |
||
86 | 'request_fulluri' => false, |
||
87 | 'username' => null, |
||
88 | 'password' => null, |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * Account username/ |
||
93 | * |
||
94 | * @var mixed |
||
95 | */ |
||
96 | public $username; |
||
97 | |||
98 | /** |
||
99 | * Account password. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | public $password; |
||
104 | |||
105 | /** |
||
106 | * Account authentication method. |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | public $authentication; |
||
111 | |||
112 | /** |
||
113 | * Active folder. |
||
114 | * |
||
115 | * @var Folder |
||
116 | */ |
||
117 | protected $active_folder = false; |
||
118 | |||
119 | /** |
||
120 | * Default message mask |
||
121 | * |
||
122 | * @var string $default_message_mask |
||
123 | */ |
||
124 | protected $default_message_mask = MessageMask::class; |
||
125 | |||
126 | /** |
||
127 | * Default attachment mask |
||
128 | * |
||
129 | * @var string $default_attachment_mask |
||
130 | */ |
||
131 | protected $default_attachment_mask = AttachmentMask::class; |
||
132 | |||
133 | /** |
||
134 | * Used default account values |
||
135 | * |
||
136 | * @var array $default_account_config |
||
137 | */ |
||
138 | protected $default_account_config = [ |
||
139 | 'host' => 'localhost', |
||
140 | 'port' => 993, |
||
141 | 'protocol' => 'imap', |
||
142 | 'encryption' => 'ssl', |
||
143 | 'validate_cert' => true, |
||
144 | 'username' => '', |
||
145 | 'password' => '', |
||
146 | 'authentication' => null, |
||
147 | 'proxy' => [ |
||
148 | 'socket' => null, |
||
149 | 'request_fulluri' => false, |
||
150 | 'username' => null, |
||
151 | 'password' => null, |
||
152 | ] |
||
153 | ]; |
||
154 | |||
155 | /** |
||
156 | * Client constructor. |
||
157 | * @param array $config |
||
158 | * |
||
159 | * @throws MaskNotFoundException |
||
160 | */ |
||
161 | public function __construct($config = []) { |
||
162 | $this->setConfig($config); |
||
163 | $this->setMaskFromConfig($config); |
||
164 | $this->setEventsFromConfig($config); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Client destructor |
||
169 | */ |
||
170 | public function __destruct() { |
||
171 | $this->disconnect(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Set the Client configuration |
||
176 | * @param array $config |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | public function setConfig(array $config) { |
||
181 | $default_account = ClientManager::get('default'); |
||
182 | $default_config = ClientManager::get("accounts.$default_account"); |
||
183 | |||
184 | foreach ($this->default_account_config as $key => $value) { |
||
185 | $this->setAccountConfig($key, $config, $default_config); |
||
186 | } |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set a specific account config |
||
193 | * @param string $key |
||
194 | * @param array $config |
||
195 | * @param array $default_config |
||
196 | */ |
||
197 | private function setAccountConfig($key, $config, $default_config){ |
||
198 | $value = $this->default_account_config[$key]; |
||
199 | if(isset($config[$key])) { |
||
200 | $value = $config[$key]; |
||
201 | }elseif(isset($default_config[$key])) { |
||
202 | $value = $default_config[$key]; |
||
203 | } |
||
204 | $this->$key = $value; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Look for a possible events in any available config |
||
209 | * @param $config |
||
210 | */ |
||
211 | protected function setEventsFromConfig($config) { |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Look for a possible mask in any available config |
||
224 | * @param $config |
||
225 | * |
||
226 | * @throws MaskNotFoundException |
||
227 | */ |
||
228 | protected function setMaskFromConfig($config) { |
||
229 | $default_config = ClientManager::get("masks"); |
||
230 | |||
231 | if(isset($config['masks'])){ |
||
232 | if(isset($config['masks']['message'])) { |
||
233 | if(class_exists($config['masks']['message'])) { |
||
234 | $this->default_message_mask = $config['masks']['message']; |
||
235 | }else{ |
||
236 | throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['message']); |
||
237 | } |
||
238 | }else{ |
||
239 | if(class_exists($default_config['message'])) { |
||
240 | $this->default_message_mask = $default_config['message']; |
||
241 | }else{ |
||
242 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']); |
||
243 | } |
||
244 | } |
||
245 | if(isset($config['masks']['attachment'])) { |
||
246 | if(class_exists($config['masks']['attachment'])) { |
||
247 | $this->default_message_mask = $config['masks']['attachment']; |
||
248 | }else{ |
||
249 | throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['attachment']); |
||
250 | } |
||
251 | }else{ |
||
252 | if(class_exists($default_config['attachment'])) { |
||
253 | $this->default_message_mask = $default_config['attachment']; |
||
254 | }else{ |
||
255 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']); |
||
256 | } |
||
257 | } |
||
258 | }else{ |
||
259 | if(class_exists($default_config['message'])) { |
||
260 | $this->default_message_mask = $default_config['message']; |
||
261 | }else{ |
||
262 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']); |
||
263 | } |
||
264 | |||
265 | if(class_exists($default_config['attachment'])) { |
||
266 | $this->default_message_mask = $default_config['attachment']; |
||
267 | }else{ |
||
268 | throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get the current imap resource |
||
276 | * |
||
277 | * @return bool|Protocol|ProtocolInterface |
||
278 | * @throws ConnectionFailedException |
||
279 | */ |
||
280 | public function getConnection() { |
||
281 | $this->checkConnection(); |
||
282 | return $this->connection; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Determine if connection was established. |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | public function isConnected() { |
||
291 | return $this->connection ? $this->connection->connected() : false; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Determine if connection was established and connect if not. |
||
296 | * |
||
297 | * @throws ConnectionFailedException |
||
298 | */ |
||
299 | public function checkConnection() { |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Force a reconnect |
||
307 | * |
||
308 | * @throws ConnectionFailedException |
||
309 | */ |
||
310 | public function reconnect() { |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Connect to server. |
||
319 | * |
||
320 | * @return $this |
||
321 | * @throws ConnectionFailedException |
||
322 | */ |
||
323 | public function connect() { |
||
324 | $this->disconnect(); |
||
325 | $protocol = strtolower($this->protocol); |
||
326 | |||
327 | if ($protocol == "imap") { |
||
328 | $timeout = $this->connection !== false ? $this->connection->getConnectionTimeout() : null; |
||
329 | $this->connection = new ImapProtocol($this->validate_cert, $this->encryption); |
||
330 | $this->connection->setConnectionTimeout($timeout); |
||
331 | $this->connection->setProxy($this->proxy); |
||
332 | }else{ |
||
333 | if (extension_loaded('imap') === false) { |
||
334 | throw new ConnectionFailedException("connection setup failed", 0, new ProtocolNotSupportedException($protocol." is an unsupported protocol")); |
||
335 | } |
||
336 | $this->connection = new LegacyProtocol($this->validate_cert, $this->encryption); |
||
337 | if (strpos($protocol, "legacy-") === 0) { |
||
338 | $protocol = substr($protocol, 7); |
||
339 | } |
||
340 | $this->connection->setProtocol($protocol); |
||
341 | } |
||
342 | |||
343 | try { |
||
344 | $this->connection->connect($this->host, $this->port); |
||
345 | } catch (\ErrorException $e) { |
||
346 | throw new ConnectionFailedException("connection setup failed", 0, $e); |
||
347 | } catch (Exceptions\RuntimeException $e) { |
||
348 | throw new ConnectionFailedException("connection setup failed", 0, $e); |
||
349 | } |
||
350 | $this->authenticate(); |
||
351 | |||
352 | return $this; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Authenticate the current session |
||
357 | * |
||
358 | * @throws ConnectionFailedException |
||
359 | */ |
||
360 | protected function authenticate() { |
||
361 | try { |
||
362 | if ($this->authentication == "oauth") { |
||
363 | if (!$this->connection->authenticate($this->username, $this->password)) { |
||
364 | throw new AuthFailedException(); |
||
365 | } |
||
366 | } elseif (!$this->connection->login($this->username, $this->password)) { |
||
367 | throw new AuthFailedException(); |
||
368 | } |
||
369 | } catch (\Exception $e) { |
||
370 | throw new ConnectionFailedException("connection setup failed", 0, $e); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Disconnect from server. |
||
376 | * |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function disconnect() { |
||
380 | if ($this->isConnected() && $this->connection !== false) { |
||
381 | $this->connection->logout(); |
||
382 | } |
||
383 | $this->active_folder = false; |
||
384 | |||
385 | return $this; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get a folder instance by a folder name |
||
390 | * @param string $folder_name |
||
391 | * @param string|bool $delimiter |
||
392 | * |
||
393 | * @return mixed |
||
394 | * @throws ConnectionFailedException |
||
395 | * @throws FolderFetchingException |
||
396 | * @throws Exceptions\RuntimeException |
||
397 | */ |
||
398 | public function getFolder($folder_name, $delimiter = null) { |
||
399 | // Set delimiter to false to force selection via getFolderByName (maybe useful for uncommon folder names) |
||
400 | if ($delimiter !== false) { |
||
401 | $delimiter = (is_null($delimiter)) ? ClientManager::get('options.delimiter', "/") : $delimiter; |
||
402 | if (strpos($folder_name, $delimiter) !== false) { |
||
403 | return $this->getFolderByPath($folder_name); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | return $this->getFolderByName($folder_name); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Get a folder instance by a folder name |
||
412 | * @param $folder_name |
||
413 | * |
||
414 | * @return mixed |
||
415 | * @throws ConnectionFailedException |
||
416 | * @throws FolderFetchingException |
||
417 | * @throws Exceptions\RuntimeException |
||
418 | */ |
||
419 | public function getFolderByName($folder_name) { |
||
420 | return $this->getFolders(false)->where("name", $folder_name)->first(); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get a folder instance by a folder path |
||
425 | * @param $folder_path |
||
426 | * |
||
427 | * @return mixed |
||
428 | * @throws ConnectionFailedException |
||
429 | * @throws FolderFetchingException |
||
430 | * @throws Exceptions\RuntimeException |
||
431 | */ |
||
432 | public function getFolderByPath($folder_path) { |
||
433 | return $this->getFolders(false)->where("path", $folder_path)->first(); |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Get folders list. |
||
438 | * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
||
439 | * |
||
440 | * @param boolean $hierarchical |
||
441 | * @param string|null $parent_folder |
||
442 | * |
||
443 | * @return FolderCollection |
||
444 | * @throws ConnectionFailedException |
||
445 | * @throws FolderFetchingException |
||
446 | * @throws Exceptions\RuntimeException |
||
447 | */ |
||
448 | public function getFolders($hierarchical = true, $parent_folder = null) { |
||
449 | $this->checkConnection(); |
||
450 | $folders = FolderCollection::make([]); |
||
451 | |||
452 | $pattern = $parent_folder.($hierarchical ? '%' : '*'); |
||
453 | $items = $this->connection->folders('', $pattern); |
||
454 | |||
455 | if(is_array($items)){ |
||
456 | foreach ($items as $folder_name => $item) { |
||
457 | $folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]); |
||
458 | |||
459 | if ($hierarchical && $folder->hasChildren()) { |
||
460 | $pattern = $folder->full_name.$folder->delimiter.'%'; |
||
461 | |||
462 | $children = $this->getFolders(true, $pattern); |
||
463 | $folder->setChildren($children); |
||
464 | } |
||
465 | |||
466 | $folders->push($folder); |
||
467 | } |
||
468 | |||
469 | return $folders; |
||
470 | }else{ |
||
471 | throw new FolderFetchingException("failed to fetch any folders"); |
||
472 | } |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Open folder. |
||
477 | * @param string $folder |
||
478 | * @param boolean $force_select |
||
479 | * |
||
480 | * @return mixed |
||
481 | * @throws ConnectionFailedException |
||
482 | * @throws Exceptions\RuntimeException |
||
483 | */ |
||
484 | public function openFolder($folder, $force_select = false) { |
||
485 | if ($this->active_folder == $folder && $this->isConnected() && $force_select === false) { |
||
486 | return true; |
||
487 | } |
||
488 | $this->checkConnection(); |
||
489 | $this->active_folder = $folder; |
||
490 | return $this->connection->selectFolder($folder); |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Create a new Folder |
||
495 | * @param string $folder |
||
496 | * @param boolean $expunge |
||
497 | * |
||
498 | * @return bool |
||
499 | * @throws ConnectionFailedException |
||
500 | * @throws FolderFetchingException |
||
501 | * @throws Exceptions\EventNotFoundException |
||
502 | * @throws Exceptions\RuntimeException |
||
503 | */ |
||
504 | public function createFolder($folder, $expunge = true) { |
||
505 | $this->checkConnection(); |
||
506 | $status = $this->connection->createFolder($folder); |
||
507 | |||
508 | if($expunge) $this->expunge(); |
||
509 | |||
510 | $folder = $this->getFolder($folder); |
||
511 | if($status && $folder) { |
||
512 | $event = $this->getEvent("folder", "new"); |
||
513 | $event::dispatch($folder); |
||
514 | } |
||
515 | |||
516 | return $folder; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Check a given folder |
||
521 | * @param $folder |
||
522 | * |
||
523 | * @return false|object |
||
524 | * @throws ConnectionFailedException |
||
525 | * @throws Exceptions\RuntimeException |
||
526 | */ |
||
527 | public function checkFolder($folder) { |
||
528 | $this->checkConnection(); |
||
529 | return $this->connection->examineFolder($folder); |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * Get the current active folder |
||
534 | * |
||
535 | * @return Folder |
||
536 | */ |
||
537 | public function getFolderPath(){ |
||
538 | return $this->active_folder; |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Retrieve the quota level settings, and usage statics per mailbox |
||
543 | * |
||
544 | * @return array |
||
545 | * @throws ConnectionFailedException |
||
546 | * @throws Exceptions\RuntimeException |
||
547 | */ |
||
548 | public function getQuota() { |
||
549 | $this->checkConnection(); |
||
550 | return $this->connection->getQuota($this->username); |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Retrieve the quota settings per user |
||
555 | * @param string $quota_root |
||
556 | * |
||
557 | * @return array |
||
558 | * @throws ConnectionFailedException |
||
559 | */ |
||
560 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Delete all messages marked for deletion |
||
567 | * |
||
568 | * @return bool |
||
569 | * @throws ConnectionFailedException |
||
570 | * @throws Exceptions\RuntimeException |
||
571 | */ |
||
572 | public function expunge() { |
||
573 | $this->checkConnection(); |
||
574 | return $this->connection->expunge(); |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Set the imap timeout for a given operation type |
||
579 | * @param $timeout |
||
580 | * |
||
581 | * @return Protocol |
||
582 | */ |
||
583 | public function setTimeout($timeout) { |
||
584 | return $this->connection->setConnectionTimeout($timeout); |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Get the timeout for a certain operation |
||
589 | * @param $type |
||
590 | * |
||
591 | * @return int |
||
592 | */ |
||
593 | public function getTimeout($type){ |
||
594 | return $this->connection->getConnectionTimeout(); |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * Get the default message mask |
||
599 | * |
||
600 | * @return string |
||
601 | */ |
||
602 | public function getDefaultMessageMask(){ |
||
604 | } |
||
605 | |||
606 | /** |
||
607 | * Get the default events for a given section |
||
608 | * @param $section |
||
609 | * |
||
610 | * @return array |
||
611 | */ |
||
612 | public function getDefaultEvents($section){ |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Set the default message mask |
||
618 | * @param $mask |
||
619 | * |
||
620 | * @return $this |
||
621 | * @throws MaskNotFoundException |
||
622 | */ |
||
623 | public function setDefaultMessageMask($mask) { |
||
624 | if(class_exists($mask)) { |
||
625 | $this->default_message_mask = $mask; |
||
626 | |||
627 | return $this; |
||
628 | } |
||
629 | |||
630 | throw new MaskNotFoundException("Unknown mask provided: ".$mask); |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * Get the default attachment mask |
||
635 | * |
||
636 | * @return string |
||
637 | */ |
||
638 | public function getDefaultAttachmentMask(){ |
||
639 | return $this->default_attachment_mask; |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * Set the default attachment mask |
||
644 | * @param $mask |
||
645 | * |
||
646 | * @return $this |
||
647 | * @throws MaskNotFoundException |
||
648 | */ |
||
649 | public function setDefaultAttachmentMask($mask) { |
||
657 | } |
||
658 | } |
||
659 |