| Total Complexity | 64 |
| Total Lines | 622 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | class Message implements MessageInterface |
||
| 58 | { |
||
| 59 | /** |
||
| 60 | * End of line char |
||
| 61 | */ |
||
| 62 | public const CRLF = PHP_EOL; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected string $from = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected string $replyTo = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The send mail receiver(s) |
||
| 78 | * @var array<int, string> |
||
| 79 | */ |
||
| 80 | protected array $to = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The send mail receiver(s) copy |
||
| 84 | * @var array<int|string, string> $cc |
||
| 85 | */ |
||
| 86 | protected array $cc = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The send mail receiver(s) hidden copy |
||
| 90 | * @var array<int|string, string> $bcc |
||
| 91 | */ |
||
| 92 | protected array $bcc = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The mail subject |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected string $subject = ''; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The mail body |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected string $body = ''; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The mail attachments |
||
| 108 | * @var array<int, array<string, string>> |
||
| 109 | */ |
||
| 110 | protected array $attachments = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The mail headers |
||
| 114 | * @var array<string, mixed> |
||
| 115 | */ |
||
| 116 | protected array $headers = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The mail boundary value |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | protected string $uid = ''; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Maximum characters for each message line |
||
| 126 | * @var int |
||
| 127 | */ |
||
| 128 | protected int $wrap = 70; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set mail priority |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | protected int $priority = 3; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Create new instance |
||
| 138 | */ |
||
| 139 | public function __construct() |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritedoc} |
||
| 146 | */ |
||
| 147 | public function reset(): self |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritedoc} |
||
| 167 | */ |
||
| 168 | public function addAttachment(string $path, ?string $filename = null): self |
||
| 169 | { |
||
| 170 | if (file_exists($path) === false) { |
||
| 171 | throw new InvalidArgumentException(sprintf( |
||
| 172 | 'The email attachment file [%s] does not exists.', |
||
| 173 | $path |
||
| 174 | )); |
||
| 175 | } |
||
| 176 | |||
| 177 | if (empty($filename)) { |
||
| 178 | $filename = basename($path); |
||
| 179 | } |
||
| 180 | |||
| 181 | $data = $this->getAttachmentData($path); |
||
| 182 | |||
| 183 | if ($data !== null) { |
||
| 184 | $this->attachments[] = [ |
||
| 185 | 'file' => $this->encodeUtf8($this->filterString($filename)), |
||
| 186 | 'path' => $path, |
||
| 187 | 'data' => chunk_split(base64_encode($data)) |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritedoc} |
||
| 196 | */ |
||
| 197 | public function setBcc(array $pairs): self |
||
| 198 | { |
||
| 199 | $this->bcc = $pairs; |
||
| 200 | |||
| 201 | return $this->addMailHeaders('Bcc', $pairs); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritedoc} |
||
| 206 | */ |
||
| 207 | public function getBcc(): array |
||
| 208 | { |
||
| 209 | return $this->bcc; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritedoc} |
||
| 214 | */ |
||
| 215 | public function setCc(array $pairs): self |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritedoc} |
||
| 224 | */ |
||
| 225 | public function getCc(): array |
||
| 226 | { |
||
| 227 | return $this->cc; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritedoc} |
||
| 232 | */ |
||
| 233 | public function setBody(string $body): self |
||
| 234 | { |
||
| 235 | $this->body = str_replace("\n.", "\n..", $body); |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritedoc} |
||
| 242 | */ |
||
| 243 | public function getEncodedBody(): string |
||
| 244 | { |
||
| 245 | $body = wordwrap($this->body, $this->wrap); |
||
| 246 | if ($this->hasAttachments()) { |
||
| 247 | $body = $this->getBodyWithAttachments(); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $body; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritedoc} |
||
| 255 | */ |
||
| 256 | public function getEncodedHeaders(): string |
||
| 257 | { |
||
| 258 | $this->prepareHeaders(); |
||
| 259 | |||
| 260 | $content = ''; |
||
| 261 | foreach ($this->headers as $name => $value) { |
||
| 262 | $content .= $name . ': ' . $value . self::CRLF; |
||
| 263 | } |
||
| 264 | |||
| 265 | return $content; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritedoc} |
||
| 270 | */ |
||
| 271 | public function setFrom(string $email, ?string $name = null): self |
||
| 272 | { |
||
| 273 | $this->from = $this->formatHeader($email, $name); |
||
| 274 | |||
| 275 | return $this->addMailHeader('From', $email, $name); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritedoc} |
||
| 280 | */ |
||
| 281 | public function getFrom(): string |
||
| 282 | { |
||
| 283 | return $this->from; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritedoc} |
||
| 288 | */ |
||
| 289 | public function setReplyTo(string $email, ?string $name = null): self |
||
| 290 | { |
||
| 291 | $this->replyTo = $this->formatHeader($email, $name); |
||
| 292 | |||
| 293 | return $this->addMailHeader('Reply-To', $email, $name); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritedoc} |
||
| 298 | */ |
||
| 299 | public function getSubject(): string |
||
| 300 | { |
||
| 301 | return $this->subject; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritedoc} |
||
| 306 | */ |
||
| 307 | public function setSubject(string $subject): self |
||
| 308 | { |
||
| 309 | $this->subject = $this->encodeUtf8($this->filterString($subject)); |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritedoc} |
||
| 316 | */ |
||
| 317 | public function setTo(string $email, ?string $name = null): self |
||
| 318 | { |
||
| 319 | $this->to[] = $this->formatHeader($email, $name); |
||
| 320 | |||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritedoc} |
||
| 326 | */ |
||
| 327 | public function getEncodedTo(): string |
||
| 328 | { |
||
| 329 | return join(', ', $this->to); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritedoc} |
||
| 334 | */ |
||
| 335 | public function getTo(): array |
||
| 336 | { |
||
| 337 | return $this->to; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritedoc} |
||
| 342 | */ |
||
| 343 | public function setWrap(int $wrap = 70): self |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritedoc} |
||
| 356 | */ |
||
| 357 | public function setPriority(int $priority = 3): self |
||
| 358 | { |
||
| 359 | if ($priority < 1 || $priority > 5) { |
||
| 360 | $priority = 3; |
||
| 361 | } |
||
| 362 | |||
| 363 | $this->priority = $priority; |
||
| 364 | |||
| 365 | return $this; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritedoc} |
||
| 370 | */ |
||
| 371 | public function getHeader(string $name, mixed $default = null): mixed |
||
| 372 | { |
||
| 373 | $this->prepareHeaders(); |
||
| 374 | |||
| 375 | return array_key_exists($name, $this->headers) |
||
| 376 | ? $this->headers[$name] |
||
| 377 | : $default; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritedoc} |
||
| 382 | */ |
||
| 383 | public function addHeader(string $name, mixed $value): self |
||
| 384 | { |
||
| 385 | $this->headers[$name] = $value; |
||
| 386 | |||
| 387 | return $this; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritedoc} |
||
| 392 | */ |
||
| 393 | public function addMailHeader(string $header, string $email, ?string $name = null): self |
||
| 394 | { |
||
| 395 | $address = $this->formatHeader($email, $name); |
||
| 396 | $this->headers[$header] = $address; |
||
| 397 | |||
| 398 | return $this; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritedoc} |
||
| 403 | */ |
||
| 404 | public function addMailHeaders(string $header, array $pairs): self |
||
| 405 | { |
||
| 406 | if (count($pairs) === 0) { |
||
| 407 | throw new InvalidArgumentException('The mail headers is empty'); |
||
| 408 | } |
||
| 409 | |||
| 410 | $addresses = []; |
||
| 411 | foreach ($pairs as $name => $email) { |
||
| 412 | if (is_numeric($name)) { |
||
| 413 | $name = null; |
||
| 414 | } |
||
| 415 | $addresses[] = $this->formatHeader($email, $name); |
||
| 416 | } |
||
| 417 | |||
| 418 | $this->addHeader($header, implode(', ', $addresses)); |
||
| 419 | |||
| 420 | return $this; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * {@inheritedoc} |
||
| 425 | */ |
||
| 426 | public function hasAttachments(): bool |
||
| 427 | { |
||
| 428 | return !empty($this->attachments); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritedoc} |
||
| 433 | */ |
||
| 434 | public function setHtml(): self |
||
| 435 | { |
||
| 436 | return $this->addHeader('Content-Type', 'text/html; charset="UTF-8"'); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * {@inheritedoc} |
||
| 441 | */ |
||
| 442 | public function __toString(): string |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Prepare mail headers |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | protected function prepareHeaders(): self |
||
| 456 | { |
||
| 457 | if (!array_key_exists('Return-Path', $this->headers)) { |
||
| 458 | $this->addHeader('Return-Path', $this->from); |
||
| 459 | } |
||
| 460 | |||
| 461 | if (!array_key_exists('Reply-To', $this->headers)) { |
||
| 462 | $this->addHeader('Reply-To', $this->from); |
||
| 463 | } |
||
| 464 | |||
| 465 | $this->addHeader('X-Priority', $this->priority) |
||
| 466 | ->addHeader('X-Mailer', 'Platine PHP Mail') |
||
| 467 | ->addHeader('Subject', $this->subject) |
||
| 468 | ->addHeader('To', join(', ', $this->to)) |
||
| 469 | ->addHeader('Date', date('r')); |
||
| 470 | |||
| 471 | if ($this->hasAttachments()) { |
||
| 472 | $this->addHeader('MIME-Version', '1.0') |
||
| 473 | ->addHeader( |
||
| 474 | 'Content-Type', |
||
| 475 | sprintf('multipart/mixed; boundary="%s"', $this->uid) |
||
| 476 | ); |
||
| 477 | } |
||
| 478 | |||
| 479 | return $this; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get mail attachment data |
||
| 484 | * @param string $path |
||
| 485 | * |
||
| 486 | * @return string|null |
||
| 487 | */ |
||
| 488 | protected function getAttachmentData(string $path): ?string |
||
| 489 | { |
||
| 490 | $filesize = filesize($path); |
||
| 491 | if ($filesize === false || $filesize < 1) { |
||
| 492 | return null; |
||
| 493 | } |
||
| 494 | |||
| 495 | $handle = fopen($path, 'r'); |
||
| 496 | $content = null; |
||
| 497 | if (is_resource($handle)) { |
||
| 498 | $result = fread($handle, $filesize); |
||
| 499 | if ($result !== false) { |
||
| 500 | $content = $result; |
||
| 501 | } |
||
| 502 | fclose($handle); |
||
| 503 | } |
||
| 504 | |||
| 505 | return $content; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Return the attachment with body |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | protected function getBodyWithAttachments(): string |
||
| 513 | { |
||
| 514 | $body = []; |
||
| 515 | $body[] = 'This is a multi-part message in MIME format.'; |
||
| 516 | $body[] = sprintf('--%s', $this->uid); |
||
| 517 | $body[] = 'Content-Type: text/html; charset="UTF-8"'; |
||
| 518 | $body[] = 'Content-Transfer-Encoding: base64'; |
||
| 519 | $body[] = self::CRLF; |
||
| 520 | $body[] = chunk_split(base64_encode($this->body)); |
||
| 521 | $body[] = self::CRLF; |
||
| 522 | $body[] = sprintf('--%s', $this->uid); |
||
| 523 | |||
| 524 | foreach ($this->attachments as $attachment) { |
||
| 525 | $body[] = $this->getAttachmentMimeTemplate($attachment); |
||
| 526 | } |
||
| 527 | |||
| 528 | return implode(self::CRLF, $body) . '--'; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get attachment mime template |
||
| 533 | * @param array<string, string> $attachment |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | protected function getAttachmentMimeTemplate(array $attachment): string |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Format mail header |
||
| 555 | * @param string $email |
||
| 556 | * @param string|null $name |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | protected function formatHeader(string $email, ?string $name = null): string |
||
| 569 | ); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Filter email address |
||
| 574 | * @param string $email |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | protected function filterEmail(string $email): string |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Filter name address |
||
| 599 | * @param string $name |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | protected function filterName(string $name): string |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Filter the string other than email and name |
||
| 628 | * @param string $value |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | protected function filterString(string $value): string |
||
| 632 | { |
||
| 633 | $filtered = filter_var( |
||
| 634 | $value, |
||
| 635 | FILTER_UNSAFE_RAW, |
||
| 636 | FILTER_FLAG_STRIP_LOW |
||
| 637 | ); |
||
| 638 | return $filtered === false ? '' : $filtered; |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Encode the UTF-8 value for the given string |
||
| 643 | * @param string $value |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | protected function encodeUtf8(?string $value): string |
||
| 647 | { |
||
| 648 | $valueClean = trim((string)$value); |
||
| 649 | if (preg_match('/(\s)/', $valueClean)) { |
||
| 650 | return $this->encodeUtf8Words($valueClean); |
||
| 651 | } |
||
| 652 | |||
| 653 | return $this->encodeUtf8Word($valueClean); |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Encode the UTF-8 value for on word |
||
| 658 | * @param string $value |
||
| 659 | * @return string |
||
| 660 | */ |
||
| 661 | protected function encodeUtf8Word(string $value): string |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Encode the UTF-8 for multiple word |
||
| 668 | * @param string $value |
||
| 669 | * @return string |
||
| 670 | */ |
||
| 671 | protected function encodeUtf8Words(string $value): string |
||
| 672 | { |
||
| 673 | $words = explode(' ', $value); |
||
| 679 | } |
||
| 680 | } |
||
| 681 |