| Total Complexity | 47 |
| Total Lines | 469 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Mail 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 Mail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Mail |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Additional parameters for sending the mail. |
||
| 23 | * |
||
| 24 | * @since 1.9.0 |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $addparams = ''; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Collection of all attachments. |
||
| 32 | * |
||
| 33 | * @since 1.9.0 |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $attachments = []; |
||
| 38 | /** |
||
| 39 | * Collection of all BCC (Blind Copy Carbon) mail-addresses. |
||
| 40 | * |
||
| 41 | * @since 1.9.0 |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $bcc = []; |
||
| 46 | /** |
||
| 47 | * Collection of all CC (Copy Carbon) mail-addresses. |
||
| 48 | * |
||
| 49 | * @since 1.9.0 |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $cc = []; |
||
| 54 | /** |
||
| 55 | * The formatted content (HTML) of the mail. |
||
| 56 | * |
||
| 57 | * @since 1.9.0 |
||
| 58 | * |
||
| 59 | * @var string|HTML |
||
|
|
|||
| 60 | */ |
||
| 61 | private $contentHTML = ''; |
||
| 62 | /** |
||
| 63 | * The plain content (non HTML) content of the mail. |
||
| 64 | * |
||
| 65 | * @since 1.9.0 |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $contentPlain = ''; |
||
| 70 | /** |
||
| 71 | * Collection of all receivers. |
||
| 72 | * |
||
| 73 | * @since 1.9.0 |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $receivers = []; |
||
| 78 | /** |
||
| 79 | * The mail-address on which should be answered. |
||
| 80 | * |
||
| 81 | * @since 1.9.0 |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $replyTo = ''; |
||
| 86 | /** |
||
| 87 | * The sender of the mail. |
||
| 88 | * |
||
| 89 | * @since 1.9.0 |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private $sender = ''; |
||
| 94 | /** |
||
| 95 | * The subject of the mail. |
||
| 96 | * |
||
| 97 | * @since 1.9.0 |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $subject = ''; |
||
| 102 | /** |
||
| 103 | * Configuration for smtp. |
||
| 104 | * |
||
| 105 | * @since 1.9.0 |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | private $smtp = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * For add attachment. |
||
| 113 | * |
||
| 114 | * @param resource $file |
||
| 115 | * |
||
| 116 | * @since 1.9.0 |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function addAttachment($file) |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * For add a new BCC. |
||
| 127 | * |
||
| 128 | * @param string $bcc |
||
| 129 | * |
||
| 130 | * @since 1.9.0 |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function addBcc($bcc) |
||
| 135 | { |
||
| 136 | if ($this->isValidEmail($bcc) === true) { |
||
| 137 | if (is_array($bcc, $this->bcc) === false) { |
||
| 138 | $this->bcc[] = $bcc; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * For adding a new cc. |
||
| 147 | * |
||
| 148 | * @param string $cc |
||
| 149 | * |
||
| 150 | * @since 1.9.0 |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function addCc($cc) |
||
| 155 | { |
||
| 156 | if ($this->isValidEmail($cc) === true) { |
||
| 157 | if (is_array($cc, $this->cc) === false) { |
||
| 158 | $this->cc[] = $cc; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return false; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * For adding a receiver. |
||
| 167 | * |
||
| 168 | * @param string $email |
||
| 169 | * |
||
| 170 | * @since 1.9.0 |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function addReceiver($email) |
||
| 175 | { |
||
| 176 | if ($this->isValidEmail($email) === true) { |
||
| 177 | if (@is_array($email, $this->receivers) === false) { |
||
| 178 | $this->receivers[] = $email; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * For preparing an attachment to send with mail. |
||
| 187 | * |
||
| 188 | * @since 1.9.0 |
||
| 189 | * |
||
| 190 | * @return mix-data |
||
| 191 | */ |
||
| 192 | public function prepareAttachment($attachment) |
||
| 193 | { |
||
| 194 | if ($this->isFile($attachment) !== true) { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | //http://php.net/manual/en/class.finfo.php |
||
| 198 | $fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
||
| 199 | $fileType = $fileInfo->file($attachment); |
||
| 200 | $file = fopen($attachment, 'r'); |
||
| 201 | $fileContent = fread($file, filesize($attachment)); |
||
| 202 | $fileContent = chunk_split(base64_encode($fileContent)); |
||
| 203 | fclose($file); |
||
| 204 | $msgContent = 'Content-Type: '.$fileType.'; name='.basename($attachment)."\r\n"; |
||
| 205 | $msgContent .= 'Content-Transfer-Encoding: base64'."\r\n"; |
||
| 206 | $msgContent .= 'Content-ID: <'.basename($attachment).'>'."\r\n"; |
||
| 207 | $msgContent .= "\r\n".$fileContent."\r\n\r\n"; |
||
| 208 | |||
| 209 | return $msgContent; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * For send the mail. |
||
| 214 | * |
||
| 215 | * @since 1.9.0 |
||
| 216 | * |
||
| 217 | * @return bool. |
||
| 218 | */ |
||
| 219 | public function send() |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Check is email is valid. |
||
| 288 | * |
||
| 289 | * @param string $email |
||
| 290 | * |
||
| 291 | * @since 1.9.0 |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function isValidEmail($email) |
||
| 296 | { |
||
| 297 | if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) { |
||
| 298 | return true; |
||
| 299 | } |
||
| 300 | if (filter_var($email, FILTER_SANITIZE_EMAIL) !== false) { |
||
| 301 | return true; |
||
| 302 | } else { |
||
| 303 | return false; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set SMPT status. |
||
| 309 | * |
||
| 310 | * @param $status |
||
| 311 | * |
||
| 312 | * @since 1.9.0 |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function isSMTP(bool $status = false) |
||
| 317 | { |
||
| 318 | if ($status === true) { |
||
| 319 | $this->smtp['status'] = $status; |
||
| 320 | } else { |
||
| 321 | $this->smtp['status'] = $status; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Send mail over SMTP. |
||
| 327 | * |
||
| 328 | * @param $to sender email |
||
| 329 | * $from from email |
||
| 330 | * $message message to be send |
||
| 331 | * |
||
| 332 | * @since 1.9.0 |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function sendSMTP($to, $from, $message) |
||
| 337 | { |
||
| 338 | $host = __config()->email->smtp_host; |
||
| 339 | $user = __config()->email->smtp_user; |
||
| 340 | $pass = __config()->email->smtp_pass; |
||
| 341 | $port = __config()->email->smtp_port; |
||
| 342 | if ($h = fsockopen($host, $port)) { |
||
| 343 | $data = [ |
||
| 344 | 0, |
||
| 345 | "EHLO $host", |
||
| 346 | 'AUTH LOGIN', |
||
| 347 | base64_encode($user), |
||
| 348 | base64_encode($pass), |
||
| 349 | "MAIL FROM: <$from>", |
||
| 350 | "RCPT TO: <$to>", |
||
| 351 | 'DATA', |
||
| 352 | $message, |
||
| 353 | ]; |
||
| 354 | foreach ($data as $c) { |
||
| 355 | $c && fwrite($h, "$c\r\n"); |
||
| 356 | while (substr(fgets($h, 256), 3, 1) != ' ') { |
||
| 357 | } |
||
| 358 | } |
||
| 359 | fwrite($h, "QUIT\r\n"); |
||
| 360 | |||
| 361 | return fclose($h); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * For set additional parameter for the mail function (4th parameter). |
||
| 367 | * |
||
| 368 | * @param string $parameter |
||
| 369 | * |
||
| 370 | * @since 1.9.0 |
||
| 371 | * |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function setaddParams($params) |
||
| 375 | { |
||
| 376 | $this->addparams = $params; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * For the formatted content (HTML) of the mail. |
||
| 381 | * |
||
| 382 | * @param string $content |
||
| 383 | * |
||
| 384 | * @since 1.9.0 |
||
| 385 | * |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | public function setContentHTML($content) |
||
| 389 | { |
||
| 390 | $content = wordwrap($content, 60, "\n"); |
||
| 391 | $this->contentHTML = $content; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * For set the plain content of the mail. |
||
| 396 | * |
||
| 397 | * @param string $content |
||
| 398 | * |
||
| 399 | * @since 1.9.0 |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function setContentPlain($content) |
||
| 404 | { |
||
| 405 | $content = wordwrap($content, 60, "\n"); |
||
| 406 | $this->contentPlain = $content; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * For set reply_to in mail. |
||
| 411 | * |
||
| 412 | * @param string $subject The subject of the mail. |
||
| 413 | * |
||
| 414 | * @since 1.9.0 |
||
| 415 | * |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | public function setReplyTo($email) |
||
| 419 | { |
||
| 420 | if ($this->isValidEmail($email) === true) { |
||
| 421 | $this->reply_to = $email; |
||
| 422 | } else { |
||
| 423 | return false; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * For set sender in mail. |
||
| 429 | * |
||
| 430 | * @param string $subject The subject of the mail. |
||
| 431 | * |
||
| 432 | * @since 1.9.0 |
||
| 433 | * |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function setSender($email) |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * For set subject in mail. |
||
| 447 | * |
||
| 448 | * @param string $subject The subject of the mail. |
||
| 449 | * |
||
| 450 | * @since 1.9.0 |
||
| 451 | * |
||
| 452 | * @return bool |
||
| 453 | */ |
||
| 454 | public function setSubject($subject) |
||
| 455 | { |
||
| 456 | $this->subject = $subject; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Check file exists or not. |
||
| 461 | * |
||
| 462 | * @param resource $file |
||
| 463 | * |
||
| 464 | * @since 1.9.0 |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | public function isFile() |
||
| 469 | { |
||
| 470 | return file_exists($this->file); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Clear all the information. |
||
| 475 | * |
||
| 476 | * @since 1.9.0 |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | public function clear() |
||
| 488 | } |
||
| 489 | } |
||
| 490 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths