Total Complexity | 70 |
Total Lines | 519 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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() { |
||
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() { |
||
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') { |
||
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') { |
||
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 | $result = []; |
||
203 | $uids = is_array($uids) ? $uids : [$uids]; |
||
204 | foreach ($uids as $uid) { |
||
205 | $result[$uid] = \imap_fetchbody($this->stream, $uid, "", IMAP::NIL); |
||
206 | } |
||
207 | return $result; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Fetch message headers |
||
212 | * @param array|int $uids |
||
213 | * @param string $rfc |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public function headers($uids, $rfc = "RFC822"){ |
||
218 | $result = []; |
||
219 | $uids = is_array($uids) ? $uids : [$uids]; |
||
220 | foreach ($uids as $uid) { |
||
221 | $result[$uid] = \imap_fetchheader($this->stream, $uid, IMAP::NIL); |
||
222 | } |
||
223 | return $result; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Fetch message flags |
||
228 | * @param array|int $uids |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function flags($uids){ |
||
233 | $result = []; |
||
234 | $uids = is_array($uids) ? $uids : [$uids]; |
||
235 | foreach ($uids as $uid) { |
||
236 | $raw_flags = \imap_fetch_overview($this->stream, $uid, IMAP::NIL); |
||
237 | $flags = []; |
||
238 | if (is_array($raw_flags) && isset($raw_flags[0])) { |
||
239 | $raw_flags = (array) $raw_flags[0]; |
||
240 | foreach($raw_flags as $flag => $value) { |
||
241 | if ($value === 1 && in_array($flag, ["size", "uid", "msgno", "update"]) === false){ |
||
242 | $flags[] = "\\".ucfirst($flag); |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | $result[$uid] = $flags; |
||
247 | } |
||
248 | |||
249 | return $result; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get uid for a given id |
||
254 | * @param int|null $id message number |
||
255 | * |
||
256 | * @return array|string message number for given message or all messages as array |
||
257 | */ |
||
258 | public function getUid($id = null) { |
||
259 | if ($id === null) { |
||
260 | $overview = $this->overview("1:*"); |
||
261 | $uids = []; |
||
262 | foreach($overview as $set){ |
||
263 | $uids[$set->msgno] = $set->uid; |
||
264 | } |
||
265 | return $uids; |
||
266 | } |
||
267 | return \imap_uid($this->stream, $id); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Get a message number for a uid |
||
272 | * @param string $id uid |
||
273 | * |
||
274 | * @return int message number |
||
275 | */ |
||
276 | public function getMessageNumber($id) { |
||
277 | return \imap_msgno($this->stream, $id); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get a message number for a uid |
||
282 | * @param string $sequence uid sequence |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | public function overview($sequence) { |
||
287 | return \imap_fetch_overview($this->stream, $sequence); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get a list of available folders |
||
292 | * @param string $reference mailbox reference for list |
||
293 | * @param string $folder mailbox name match with wildcards |
||
294 | * |
||
295 | * @return array folders that matched $folder as array(name => array('delimiter' => .., 'flags' => ..)) |
||
296 | * @throws RuntimeException |
||
297 | */ |
||
298 | public function folders($reference = '', $folder = '*') { |
||
299 | $result = []; |
||
300 | |||
301 | $items = \imap_getmailboxes($this->stream, $this->getAddress(), $reference.$folder); |
||
302 | if(is_array($items)){ |
||
303 | foreach ($items as $item) { |
||
304 | $name = $this->decodeFolderName($item->name); |
||
305 | $result[$name] = ['delimiter' => $item->delimiter, 'flags' => []]; |
||
306 | } |
||
307 | }else{ |
||
308 | throw new RuntimeException(\imap_last_error()); |
||
309 | } |
||
310 | |||
311 | return $result; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Manage flags |
||
316 | * @param array $flags flags to set, add or remove - see $mode |
||
317 | * @param int $from message for items or start message if $to !== null |
||
318 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
319 | * last message, INF means last message available |
||
320 | * @param string|null $mode '+' to add flags, '-' to remove flags, everything else sets the flags as given |
||
321 | * @param bool $silent if false the return values are the new flags for the wanted messages |
||
322 | * |
||
323 | * @return bool|array new flags if $silent is false, else true or false depending on success |
||
324 | */ |
||
325 | public function store(array $flags, $from, $to = null, $mode = null, $silent = true) { |
||
326 | $flag = trim(is_array($flags) ? implode(" ", $flags) : $flags); |
||
327 | |||
328 | if ($mode == "+"){ |
||
329 | $status = \imap_setflag_full($this->stream, $from, $flag, IMAP::NIL); |
||
330 | }else{ |
||
331 | $status = \imap_clearflag_full($this->stream, $from, $flag, IMAP::NIL); |
||
332 | } |
||
333 | |||
334 | if ($silent === true) { |
||
335 | return $status; |
||
336 | } |
||
337 | |||
338 | return $this->flags($from); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Append a new message to given folder |
||
343 | * @param string $folder name of target folder |
||
344 | * @param string $message full message content |
||
345 | * @param array $flags flags for new message |
||
346 | * @param string $date date for new message |
||
347 | * |
||
348 | * @return bool success |
||
349 | */ |
||
350 | public function appendMessage($folder, $message, $flags = null, $date = null) { |
||
351 | if ($date != null) { |
||
352 | if ($date instanceof \Carbon\Carbon){ |
||
353 | $date = $date->format('d-M-Y H:i:s O'); |
||
354 | } |
||
355 | return \imap_append($this->stream, $folder, $message, $flags, $date); |
||
356 | } |
||
357 | |||
358 | return \imap_append($this->stream, $folder, $message, $flags); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Copy message set from current folder to other folder |
||
363 | * @param string $folder destination folder |
||
364 | * @param $from |
||
365 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
366 | * last message, INF means last message available |
||
367 | * |
||
368 | * @return bool success |
||
369 | */ |
||
370 | public function copyMessage($folder, $from, $to = null) { |
||
371 | return \imap_mail_copy($this->stream, $from, $folder, IMAP::CP_UID); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Move a message set from current folder to an other folder |
||
376 | * @param string $folder destination folder |
||
377 | * @param $from |
||
378 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
379 | * last message, INF means last message available |
||
380 | * |
||
381 | * @return bool success |
||
382 | */ |
||
383 | public function moveMessage($folder, $from, $to = null) { |
||
384 | return \imap_mail_move($this->stream, $from, $folder, IMAP::CP_UID); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Create a new folder (and parent folders if needed) |
||
389 | * @param string $folder folder name |
||
390 | * |
||
391 | * @return bool success |
||
392 | */ |
||
393 | public function createFolder($folder) { |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Rename an existing folder |
||
399 | * @param string $old old name |
||
400 | * @param string $new new name |
||
401 | * |
||
402 | * @return bool success |
||
403 | */ |
||
404 | public function renameFolder($old, $new) { |
||
405 | return \imap_renamemailbox($this->stream, $old, $new); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Delete a folder |
||
410 | * @param string $folder folder name |
||
411 | * |
||
412 | * @return bool success |
||
413 | */ |
||
414 | public function deleteFolder($folder) { |
||
415 | return \imap_deletemailbox($this->stream, $folder); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Subscribe to a folder |
||
420 | * @param string $folder folder name |
||
421 | * |
||
422 | * @throws MethodNotSupportedException |
||
423 | */ |
||
424 | public function subscribeFolder($folder) { |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Unsubscribe from a folder |
||
430 | * @param string $folder folder name |
||
431 | * |
||
432 | * @throws MethodNotSupportedException |
||
433 | */ |
||
434 | public function unsubscribeFolder($folder) { |
||
435 | throw new MethodNotSupportedException(); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Apply session saved changes to the server |
||
440 | * |
||
441 | * @return bool success |
||
442 | */ |
||
443 | public function expunge() { |
||
444 | return \imap_expunge($this->stream); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Send noop command |
||
449 | * |
||
450 | * @throws MethodNotSupportedException |
||
451 | */ |
||
452 | public function noop() { |
||
453 | throw new MethodNotSupportedException(); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Send idle command |
||
458 | * |
||
459 | * @throws MethodNotSupportedException |
||
460 | */ |
||
461 | public function idle() { |
||
462 | throw new MethodNotSupportedException(); |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Send done command |
||
467 | * |
||
468 | * @throws MethodNotSupportedException |
||
469 | */ |
||
470 | public function done() { |
||
471 | throw new MethodNotSupportedException(); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Search for matching messages |
||
476 | * |
||
477 | * @param array $params |
||
478 | * @return array message ids |
||
479 | */ |
||
480 | public function search(array $params) { |
||
481 | return \imap_search($this->stream, $params[0], IMAP::NIL); |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Enable the debug mode |
||
486 | */ |
||
487 | public function enableDebug(){ |
||
488 | $this->debug = true; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Disable the debug mode |
||
493 | */ |
||
494 | public function disableDebug(){ |
||
495 | $this->debug = false; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Decode name. |
||
500 | * It converts UTF7-IMAP encoding to UTF-8. |
||
501 | * |
||
502 | * @param $name |
||
503 | * |
||
504 | * @return mixed|string |
||
505 | */ |
||
506 | protected function decodeFolderName($name) { |
||
507 | preg_match('#\{(.*)\}(.*)#', $name, $preg); |
||
508 | return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP"); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @return string |
||
513 | */ |
||
514 | public function getProtocol() { |
||
515 | return $this->protocol; |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Retrieve the quota level settings, and usage statics per mailbox |
||
520 | * @param $username |
||
521 | * |
||
522 | * @return array |
||
523 | */ |
||
524 | public function getQuota($username) { |
||
525 | return \imap_get_quota($this->stream, 'user.'.$username); |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Retrieve the quota settings per user |
||
530 | * @param string $quota_root |
||
531 | * |
||
532 | * @return array |
||
533 | */ |
||
534 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * @param string $protocol |
||
540 | * @return LegacyProtocol |
||
541 | */ |
||
542 | public function setProtocol($protocol) { |
||
543 | $this->protocol = $protocol; |
||
544 | return $this; |
||
545 | } |
||
546 | } |