|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Mail; |
| 6 | + |
| 7 | +use Symfony\Component\Mime\Email as SymfonyEmail; |
| 8 | +use Tempest\Mail\Exceptions\MissingExpeditorAddressException; |
| 9 | +use Tempest\Mail\Exceptions\MissingRecipientAddressException; |
| 10 | +use Tempest\Mapper\Mapper; |
| 11 | +use Tempest\Support\Arr; |
| 12 | +use Tempest\View\View; |
| 13 | +use Tempest\View\ViewRenderer; |
| 14 | + |
| 15 | +final readonly class EmailToSymfonyEmailMapper implements Mapper |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + private readonly MailerConfig $mailerConfig, |
| 19 | + private readonly ViewRenderer $viewRenderer, |
| 20 | + ) {} |
| 21 | + |
| 22 | + public function canMap(mixed $from, mixed $to): bool |
| 23 | + { |
| 24 | + if ($from instanceof Email) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + public function map(mixed $from, mixed $to): SymfonyEmail |
| 32 | + { |
| 33 | + /** @var Email $email */ |
| 34 | + $email = $from; |
| 35 | + $symfonyEmail = new SymfonyEmail(); |
| 36 | + |
| 37 | + if ($email->envelope->from) { |
| 38 | + $symfonyEmail->from(...Arr\wrap($email->envelope->from)); |
| 39 | + } elseif ($this->mailerConfig->from) { |
| 40 | + $symfonyEmail->from($this->mailerConfig->from); |
| 41 | + } else { |
| 42 | + throw new MissingExpeditorAddressException(); |
| 43 | + } |
| 44 | + |
| 45 | + if ($email->envelope->to) { |
| 46 | + $symfonyEmail->to(...Arr\wrap($email->envelope->to)); |
| 47 | + } else { |
| 48 | + throw new MissingRecipientAddressException(); |
| 49 | + } |
| 50 | + |
| 51 | + if ($email->envelope->cc) { |
| 52 | + $symfonyEmail->cc(...Arr\wrap($email->envelope->cc)); |
| 53 | + } |
| 54 | + |
| 55 | + if ($email->envelope->bcc) { |
| 56 | + $symfonyEmail->bcc(...Arr\wrap($email->envelope->bcc)); |
| 57 | + } |
| 58 | + |
| 59 | + if ($email->envelope->replyTo) { |
| 60 | + $symfonyEmail->replyTo(...Arr\wrap($email->envelope->replyTo)); |
| 61 | + } |
| 62 | + |
| 63 | + if ($email->envelope->subject) { |
| 64 | + $symfonyEmail->subject($email->envelope->subject); |
| 65 | + } |
| 66 | + |
| 67 | + if ($email->envelope->priority) { |
| 68 | + $symfonyEmail->priority($email->envelope->priority->value); |
| 69 | + } |
| 70 | + |
| 71 | + if ($email->content->text) { |
| 72 | + $symfonyEmail->text($email->content->text); |
| 73 | + } |
| 74 | + |
| 75 | + if ($email->content->html instanceof View) { |
| 76 | + $symfonyEmail->html($this->viewRenderer->render($email->content->html)); |
| 77 | + } elseif ($email->content->html) { |
| 78 | + $symfonyEmail->html($email->content->html); |
| 79 | + } |
| 80 | + |
| 81 | + /** @var Attachment $attachment */ |
| 82 | + foreach (Arr\wrap($email->content->attachments) as $attachment) { |
| 83 | + $symfonyEmail->attach(($attachment->resolve)(), $attachment->name, $attachment->contentType); |
| 84 | + } |
| 85 | + |
| 86 | + return $symfonyEmail; |
| 87 | + } |
| 88 | +} |
0 commit comments