| Total Complexity | 92 |
| Total Lines | 593 |
| 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) { |
||
| 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) { |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Exchange identification information |
||
| 447 | * Ref.: https://datatracker.ietf.org/doc/html/rfc2971 |
||
| 448 | * |
||
| 449 | * @param null $ids |
||
| 450 | * @return array|bool|void|null |
||
| 451 | * |
||
| 452 | * @throws MethodNotSupportedException |
||
| 453 | */ |
||
| 454 | public function ID($ids = null) { |
||
| 455 | throw new MethodNotSupportedException(); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Create a new folder (and parent folders if needed) |
||
| 460 | * @param string $folder folder name |
||
| 461 | * |
||
| 462 | * @return bool success |
||
| 463 | */ |
||
| 464 | public function createFolder($folder) { |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Rename an existing folder |
||
| 470 | * @param string $old old name |
||
| 471 | * @param string $new new name |
||
| 472 | * |
||
| 473 | * @return bool success |
||
| 474 | */ |
||
| 475 | public function renameFolder($old, $new) { |
||
| 476 | return \imap_renamemailbox($this->stream, $old, $new); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Delete a folder |
||
| 481 | * @param string $folder folder name |
||
| 482 | * |
||
| 483 | * @return bool success |
||
| 484 | */ |
||
| 485 | public function deleteFolder($folder) { |
||
| 486 | return \imap_deletemailbox($this->stream, $folder); |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Subscribe to a folder |
||
| 491 | * @param string $folder folder name |
||
| 492 | * |
||
| 493 | * @throws MethodNotSupportedException |
||
| 494 | */ |
||
| 495 | public function subscribeFolder($folder) { |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Unsubscribe from a folder |
||
| 501 | * @param string $folder folder name |
||
| 502 | * |
||
| 503 | * @throws MethodNotSupportedException |
||
| 504 | */ |
||
| 505 | public function unsubscribeFolder($folder) { |
||
| 506 | throw new MethodNotSupportedException(); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Apply session saved changes to the server |
||
| 511 | * |
||
| 512 | * @return bool success |
||
| 513 | */ |
||
| 514 | public function expunge() { |
||
| 515 | return \imap_expunge($this->stream); |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Send noop command |
||
| 520 | * |
||
| 521 | * @throws MethodNotSupportedException |
||
| 522 | */ |
||
| 523 | public function noop() { |
||
| 524 | throw new MethodNotSupportedException(); |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Send idle command |
||
| 529 | * |
||
| 530 | * @throws MethodNotSupportedException |
||
| 531 | */ |
||
| 532 | public function idle() { |
||
| 533 | throw new MethodNotSupportedException(); |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Send done command |
||
| 538 | * |
||
| 539 | * @throws MethodNotSupportedException |
||
| 540 | */ |
||
| 541 | public function done() { |
||
| 542 | throw new MethodNotSupportedException(); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Search for matching messages |
||
| 547 | * |
||
| 548 | * @param array $params |
||
| 549 | * @return array message ids |
||
| 550 | */ |
||
| 551 | public function search(array $params, $uid = false) { |
||
| 552 | return \imap_search($this->stream, $params[0], $uid ? IMAP::FT_UID : IMAP::NIL); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Enable the debug mode |
||
| 557 | */ |
||
| 558 | public function enableDebug(){ |
||
| 559 | $this->debug = true; |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Disable the debug mode |
||
| 564 | */ |
||
| 565 | public function disableDebug(){ |
||
| 566 | $this->debug = false; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Decode name. |
||
| 571 | * It converts UTF7-IMAP encoding to UTF-8. |
||
| 572 | * |
||
| 573 | * @param $name |
||
| 574 | * |
||
| 575 | * @return mixed|string |
||
| 576 | */ |
||
| 577 | protected function decodeFolderName($name) { |
||
| 578 | preg_match('#\{(.*)\}(.*)#', $name, $preg); |
||
| 579 | return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP"); |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function getProtocol() { |
||
| 586 | return $this->protocol; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Retrieve the quota level settings, and usage statics per mailbox |
||
| 591 | * @param $username |
||
| 592 | * |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | public function getQuota($username) { |
||
| 596 | return \imap_get_quota($this->stream, 'user.'.$username); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Retrieve the quota settings per user |
||
| 601 | * @param string $quota_root |
||
| 602 | * |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @param string $protocol |
||
| 611 | * @return LegacyProtocol |
||
| 612 | */ |
||
| 613 | public function setProtocol($protocol) { |
||
| 619 | } |
||
| 620 | } |