| Total Complexity | 60 |
| Total Lines | 596 |
| 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 |
||
| 27 | class Client { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var boolean|resource |
||
| 31 | */ |
||
| 32 | public $connection = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Server hostname. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $host; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Server port. |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | public $port; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Service protocol. |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | public $protocol; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Server encryption. |
||
| 57 | * Supported: none, ssl or tls. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | public $encryption; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * If server has to validate cert. |
||
| 65 | * |
||
| 66 | * @var mixed |
||
| 67 | */ |
||
| 68 | public $validate_cert; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Account username/ |
||
| 72 | * |
||
| 73 | * @var mixed |
||
| 74 | */ |
||
| 75 | public $username; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Account password. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $password; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Read only parameter. |
||
| 86 | * |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $read_only = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Active folder. |
||
| 93 | * |
||
| 94 | * @var Folder |
||
| 95 | */ |
||
| 96 | protected $activeFolder = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Connected parameter |
||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | protected $connected = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * IMAP errors that might have ben occurred |
||
| 107 | * |
||
| 108 | * @var array $errors |
||
| 109 | */ |
||
| 110 | protected $errors = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * All valid and available account config parameters |
||
| 114 | * |
||
| 115 | * @var array $validConfigKeys |
||
| 116 | */ |
||
| 117 | protected $validConfigKeys = ['host', 'port', 'encryption', 'validate_cert', 'username', 'password','protocol']; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * All available timeout types |
||
| 121 | * |
||
| 122 | * @var array $timeout_type |
||
| 123 | */ |
||
| 124 | protected $timeout_type = [ |
||
| 125 | 'IMAP_OPENTIMEOUT' => 1, |
||
| 126 | 'IMAP_READTIMEOUT' => 2, |
||
| 127 | 'IMAP_WRITETIMEOUT' => 3, |
||
| 128 | 'IMAP_CLOSETIMEOUT' => 4 |
||
| 129 | ]; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Client constructor. |
||
| 133 | * |
||
| 134 | * @param array $config |
||
| 135 | */ |
||
| 136 | public function __construct($config = []) { |
||
| 137 | $this->setConfig($config); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Client destructor |
||
| 142 | */ |
||
| 143 | public function __destruct() { |
||
| 144 | $this->disconnect(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set the Client configuration |
||
| 149 | * |
||
| 150 | * @param array $config |
||
| 151 | * |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function setConfig(array $config) { |
||
| 155 | $defaultAccount = config('imap.default'); |
||
| 156 | $defaultConfig = config("imap.accounts.$defaultAccount"); |
||
| 157 | |||
| 158 | foreach ($this->validConfigKeys as $key) { |
||
| 159 | $this->$key = isset($config[$key]) ? $config[$key] : $defaultConfig[$key]; |
||
| 160 | } |
||
| 161 | |||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the current imap resource |
||
| 167 | * |
||
| 168 | * @return bool|resource |
||
| 169 | * @throws ConnectionFailedException |
||
| 170 | */ |
||
| 171 | public function getConnection() { |
||
| 172 | $this->checkConnection(); |
||
| 173 | return $this->connection; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set read only property and reconnect if it's necessary. |
||
| 178 | * |
||
| 179 | * @param bool $readOnly |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | */ |
||
| 183 | public function setReadOnly($readOnly = true) { |
||
| 184 | $this->read_only = $readOnly; |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Determine if connection was established. |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | public function isConnected() { |
||
| 195 | return $this->connected; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Determine if connection is in read only mode. |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function isReadOnly() { |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Determine if connection was established and connect if not. |
||
| 209 | * |
||
| 210 | * @throws ConnectionFailedException |
||
| 211 | */ |
||
| 212 | public function checkConnection() { |
||
| 213 | if (!$this->isConnected() || $this->connection === false) { |
||
| 214 | $this->connect(); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Connect to server. |
||
| 220 | * |
||
| 221 | * @param int $attempts |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | * @throws ConnectionFailedException |
||
| 225 | */ |
||
| 226 | public function connect($attempts = 3) { |
||
| 227 | $this->disconnect(); |
||
| 228 | |||
| 229 | try { |
||
| 230 | $this->connection = imap_open( |
||
| 231 | $this->getAddress(), |
||
| 232 | $this->username, |
||
| 233 | $this->password, |
||
| 234 | $this->getOptions(), |
||
| 235 | $attempts, |
||
| 236 | config('imap.options.open') |
||
| 237 | ); |
||
| 238 | $this->connected = !!$this->connection; |
||
| 239 | } catch (\ErrorException $e) { |
||
| 240 | $errors = imap_errors(); |
||
| 241 | $message = $e->getMessage().'. '.implode("; ", (is_array($errors) ? $errors : array())); |
||
|
|
|||
| 242 | |||
| 243 | throw new ConnectionFailedException($message); |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Disconnect from server. |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function disconnect() { |
||
| 255 | if ($this->isConnected() && $this->connection !== false && is_integer($this->connection) === false) { |
||
| 256 | $this->errors = array_merge($this->errors, imap_errors() ?: []); |
||
| 257 | $this->connected = !imap_close($this->connection, CL_EXPUNGE); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $this; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get a folder instance by a folder name |
||
| 265 | * --------------------------------------------- |
||
| 266 | * PLEASE NOTE: This is an experimental function |
||
| 267 | * --------------------------------------------- |
||
| 268 | * @param string $folder_name |
||
| 269 | * @param int $attributes |
||
| 270 | * @param null|string $delimiter |
||
| 271 | * |
||
| 272 | * @return Folder |
||
| 273 | */ |
||
| 274 | public function getFolder($folder_name, $attributes = 32, $delimiter = null) { |
||
| 275 | |||
| 276 | $delimiter = $delimiter === null ? config('imap.options.delimiter', '/') : $delimiter; |
||
| 277 | |||
| 278 | $oFolder = new Folder($this, (object) [ |
||
| 279 | 'name' => $this->getAddress().$folder_name, |
||
| 280 | 'attributes' => $attributes, |
||
| 281 | 'delimiter' => $delimiter |
||
| 282 | ]); |
||
| 283 | |||
| 284 | return $oFolder; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get folders list. |
||
| 289 | * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
||
| 290 | * |
||
| 291 | * @param boolean $hierarchical |
||
| 292 | * @param string|null $parent_folder |
||
| 293 | * |
||
| 294 | * @return FolderCollection |
||
| 295 | * @throws ConnectionFailedException |
||
| 296 | */ |
||
| 297 | public function getFolders($hierarchical = true, $parent_folder = null) { |
||
| 298 | $this->checkConnection(); |
||
| 299 | $folders = FolderCollection::make([]); |
||
| 300 | |||
| 301 | $pattern = $parent_folder.($hierarchical ? '%' : '*'); |
||
| 302 | |||
| 303 | $items = imap_getmailboxes($this->connection, $this->getAddress(), $pattern); |
||
| 304 | foreach ($items as $item) { |
||
| 305 | $folder = new Folder($this, $item); |
||
| 306 | |||
| 307 | if ($hierarchical && $folder->hasChildren()) { |
||
| 308 | $pattern = $folder->fullName.$folder->delimiter.'%'; |
||
| 309 | |||
| 310 | $children = $this->getFolders(true, $pattern); |
||
| 311 | $folder->setChildren($children); |
||
| 312 | } |
||
| 313 | |||
| 314 | $folders->push($folder); |
||
| 315 | } |
||
| 316 | |||
| 317 | return $folders; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Open folder. |
||
| 322 | * |
||
| 323 | * @param Folder $folder |
||
| 324 | * @param int $attempts |
||
| 325 | * |
||
| 326 | * @throws ConnectionFailedException |
||
| 327 | */ |
||
| 328 | public function openFolder(Folder $folder, $attempts = 3) { |
||
| 329 | $this->checkConnection(); |
||
| 330 | |||
| 331 | if ($this->activeFolder !== $folder) { |
||
| 332 | $this->activeFolder = $folder; |
||
| 333 | |||
| 334 | imap_reopen($this->getConnection(), $folder->path, $this->getOptions(), $attempts); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Create a new Folder |
||
| 340 | * @param string $name |
||
| 341 | * @param boolean $expunge |
||
| 342 | * |
||
| 343 | * @return bool |
||
| 344 | * @throws ConnectionFailedException |
||
| 345 | */ |
||
| 346 | public function createFolder($name, $expunge = true) { |
||
| 347 | $this->checkConnection(); |
||
| 348 | $status = imap_createmailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name)); |
||
| 349 | if($expunge) $this->expunge(); |
||
| 350 | |||
| 351 | return $status; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Rename Folder |
||
| 356 | * @param string $old_name |
||
| 357 | * @param string $new_name |
||
| 358 | * @param boolean $expunge |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | * @throws ConnectionFailedException |
||
| 362 | */ |
||
| 363 | public function renameFolder($old_name, $new_name, $expunge = true) { |
||
| 364 | $this->checkConnection(); |
||
| 365 | $status = imap_renamemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name)); |
||
| 366 | if($expunge) $this->expunge(); |
||
| 367 | |||
| 368 | return $status; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Delete Folder |
||
| 373 | * @param string $name |
||
| 374 | * @param boolean $expunge |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | * @throws ConnectionFailedException |
||
| 378 | */ |
||
| 379 | public function deleteFolder($name, $expunge = true) { |
||
| 380 | $this->checkConnection(); |
||
| 381 | $status = imap_deletemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name)); |
||
| 382 | if($expunge) $this->expunge(); |
||
| 383 | |||
| 384 | return $status; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get messages from folder. |
||
| 389 | * |
||
| 390 | * @param Folder $folder |
||
| 391 | * @param string $criteria |
||
| 392 | * @param int|null $fetch_options |
||
| 393 | * @param boolean $fetch_body |
||
| 394 | * @param boolean $fetch_attachment |
||
| 395 | * |
||
| 396 | * @return MessageCollection |
||
| 397 | * @throws ConnectionFailedException |
||
| 398 | * @throws Exceptions\InvalidWhereQueryCriteriaException |
||
| 399 | * @throws GetMessagesFailedException |
||
| 400 | * @throws MessageSearchValidationException |
||
| 401 | * |
||
| 402 | * @deprecated 1.0.5.2:2.0.0 No longer needed. Use Folder::getMessages() instead |
||
| 403 | * @see Folder::getMessages() |
||
| 404 | */ |
||
| 405 | public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) { |
||
| 406 | return $folder->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get all unseen messages from folder |
||
| 411 | * |
||
| 412 | * @param Folder $folder |
||
| 413 | * @param string $criteria |
||
| 414 | * @param int|null $fetch_options |
||
| 415 | * @param boolean $fetch_body |
||
| 416 | * @param boolean $fetch_attachment |
||
| 417 | * |
||
| 418 | * @return MessageCollection |
||
| 419 | * @throws ConnectionFailedException |
||
| 420 | * @throws Exceptions\InvalidWhereQueryCriteriaException |
||
| 421 | * @throws GetMessagesFailedException |
||
| 422 | * @throws MessageSearchValidationException |
||
| 423 | * |
||
| 424 | * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead |
||
| 425 | * @see Folder::getMessages() |
||
| 426 | */ |
||
| 427 | public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) { |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Search messages by a given search criteria |
||
| 433 | * |
||
| 434 | * @param array $where |
||
| 435 | * @param Folder $folder |
||
| 436 | * @param int|null $fetch_options |
||
| 437 | * @param boolean $fetch_body |
||
| 438 | * @param string $charset |
||
| 439 | * @param boolean $fetch_attachment |
||
| 440 | * |
||
| 441 | * @return MessageCollection |
||
| 442 | * @throws ConnectionFailedException |
||
| 443 | * @throws Exceptions\InvalidWhereQueryCriteriaException |
||
| 444 | * @throws GetMessagesFailedException |
||
| 445 | * @throws MessageSearchValidationException |
||
| 446 | * |
||
| 447 | * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::searchMessages() instead |
||
| 448 | * @see Folder::searchMessages() |
||
| 449 | * |
||
| 450 | */ |
||
| 451 | public function searchMessages(array $where, Folder $folder, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true, $fetch_flags = false) { |
||
| 452 | return $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment, $fetch_flags); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get option for imap_open and imap_reopen. |
||
| 457 | * It supports only isReadOnly feature. |
||
| 458 | * |
||
| 459 | * @return int |
||
| 460 | */ |
||
| 461 | protected function getOptions() { |
||
| 462 | return ($this->isReadOnly()) ? OP_READONLY : 0; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get full address of mailbox. |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | protected function getAddress() { |
||
| 471 | $address = "{".$this->host.":".$this->port."/".($this->protocol ? $this->protocol : 'imap'); |
||
| 472 | if (!$this->validate_cert) { |
||
| 473 | $address .= '/novalidate-cert'; |
||
| 474 | } |
||
| 475 | if (in_array($this->encryption,['tls','ssl'])) { |
||
| 476 | $address .= '/'.$this->encryption; |
||
| 477 | } |
||
| 478 | $address .= '}'; |
||
| 479 | |||
| 480 | return $address; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Retrieve the quota level settings, and usage statics per mailbox |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | * @throws ConnectionFailedException |
||
| 488 | */ |
||
| 489 | public function getQuota() { |
||
| 490 | $this->checkConnection(); |
||
| 491 | return imap_get_quota($this->getConnection(), 'user.'.$this->username); |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Retrieve the quota settings per user |
||
| 496 | * |
||
| 497 | * @param string $quota_root |
||
| 498 | * |
||
| 499 | * @return array |
||
| 500 | * @throws ConnectionFailedException |
||
| 501 | */ |
||
| 502 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
| 503 | $this->checkConnection(); |
||
| 504 | return imap_get_quotaroot($this->getConnection(), $quota_root); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Gets the number of messages in the current mailbox |
||
| 509 | * |
||
| 510 | * @return int |
||
| 511 | * @throws ConnectionFailedException |
||
| 512 | */ |
||
| 513 | public function countMessages() { |
||
| 514 | $this->checkConnection(); |
||
| 515 | return imap_num_msg($this->connection); |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Gets the number of recent messages in current mailbox |
||
| 520 | * |
||
| 521 | * @return int |
||
| 522 | * @throws ConnectionFailedException |
||
| 523 | */ |
||
| 524 | public function countRecentMessages() { |
||
| 525 | $this->checkConnection(); |
||
| 526 | return imap_num_recent($this->connection); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns all IMAP alert messages that have occurred |
||
| 531 | * |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | public function getAlerts() { |
||
| 535 | return imap_alerts(); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns all of the IMAP errors that have occurred |
||
| 540 | * |
||
| 541 | * @return array |
||
| 542 | */ |
||
| 543 | public function getErrors() { |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Gets the last IMAP error that occurred during this page request |
||
| 551 | * |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | public function getLastError() { |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Delete all messages marked for deletion |
||
| 560 | * |
||
| 561 | * @return bool |
||
| 562 | * @throws ConnectionFailedException |
||
| 563 | */ |
||
| 564 | public function expunge() { |
||
| 565 | $this->checkConnection(); |
||
| 566 | return imap_expunge($this->connection); |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Check current mailbox |
||
| 571 | * |
||
| 572 | * @return object { |
||
| 573 | * Date [string(37) "Wed, 8 Mar 2017 22:17:54 +0100 (CET)"] current system time formatted according to » RFC2822 |
||
| 574 | * Driver [string(4) "imap"] protocol used to access this mailbox: POP3, IMAP, NNTP |
||
| 575 | * Mailbox ["{[email protected]:993/imap/user="[email protected]"}INBOX"] the mailbox name |
||
| 576 | * Nmsgs [int(1)] number of messages in the mailbox |
||
| 577 | * Recent [int(0)] number of recent messages in the mailbox |
||
| 578 | * } |
||
| 579 | * @throws ConnectionFailedException |
||
| 580 | */ |
||
| 581 | public function checkCurrentMailbox() { |
||
| 582 | $this->checkConnection(); |
||
| 583 | return imap_check($this->connection); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Set the imap timeout for a given operation type |
||
| 588 | * @param $type |
||
| 589 | * @param $timeout |
||
| 590 | * |
||
| 591 | * @return mixed |
||
| 592 | * @throws InvalidImapTimeoutTypeException |
||
| 593 | */ |
||
| 594 | public function setTimeout($type, $timeout) { |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get the timeout for a certain operation |
||
| 608 | * @param $type |
||
| 609 | * |
||
| 610 | * @return mixed |
||
| 611 | * @throws InvalidImapTimeoutTypeException |
||
| 612 | */ |
||
| 613 | public function getTimeout($type){ |
||
| 614 | if(is_numeric($type)) { |
||
| 615 | $type = (int) $type; |
||
| 623 | } |
||
| 624 | } |
||
| 625 |