|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Mail\Builder; |
| 4 | + |
| 5 | +use Stringable; |
| 6 | +use Tempest\Mail\Address; |
| 7 | +use Tempest\Mail\Attachments\Attachment; |
| 8 | +use Tempest\Mail\Attachments\FileAttachment; |
| 9 | +use Tempest\Mail\Attachments\StorageAttachment; |
| 10 | +use Tempest\Mail\Content; |
| 11 | +use Tempest\Mail\Email; |
| 12 | +use Tempest\Mail\Envelope; |
| 13 | +use Tempest\Mail\GenericEmail; |
| 14 | +use Tempest\Mail\Priority; |
| 15 | +use Tempest\Support\Arr; |
| 16 | +use Tempest\Support\Arr\ArrayInterface; |
| 17 | +use Tempest\View\View; |
| 18 | +use UnitEnum; |
| 19 | + |
| 20 | +final class EmailBuilder |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private(set) null|string|array|ArrayInterface|Address $to = null, |
| 24 | + private(set) null|string|array|ArrayInterface|Address $from = null, |
| 25 | + private(set) null|string|array|ArrayInterface|Address $replyTo = null, |
| 26 | + private(set) null|string|array|ArrayInterface|Address $cc = null, |
| 27 | + private(set) null|string|array|ArrayInterface|Address $bcc = null, |
| 28 | + private(set) ?string $subject = null, |
| 29 | + private(set) null|string|View $html = null, |
| 30 | + private(set) ?string $text = null, |
| 31 | + private(set) Priority|int $priority = Priority::NORMAL, |
| 32 | + private(set) array $headers = [], |
| 33 | + private(set) array $attachments = [], |
| 34 | + ) {} |
| 35 | + |
| 36 | + /** |
| 37 | + * Defines the recipients of the email. |
| 38 | + */ |
| 39 | + public function to(null|string|array|ArrayInterface|Address $to): self |
| 40 | + { |
| 41 | + $this->to = $to; |
| 42 | + |
| 43 | + return $this; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Defines the sender of the email. |
| 48 | + */ |
| 49 | + public function from(null|string|array|ArrayInterface|Address $from): self |
| 50 | + { |
| 51 | + $this->from = $from; |
| 52 | + |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Defines the reply-to address of the email. |
| 58 | + */ |
| 59 | + public function replyTo(null|string|array|ArrayInterface|Address $replyTo): self |
| 60 | + { |
| 61 | + $this->replyTo = $replyTo; |
| 62 | + |
| 63 | + return $this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Defines the carbon-copy recipients of the email. |
| 68 | + */ |
| 69 | + public function cc(null|string|array|ArrayInterface|Address $cc): self |
| 70 | + { |
| 71 | + $this->cc = $cc; |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Defines the blind carbon-copy recipients of the email. |
| 78 | + */ |
| 79 | + public function bcc(null|string|array|ArrayInterface|Address $bcc): self |
| 80 | + { |
| 81 | + $this->bcc = $bcc; |
| 82 | + |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Defines the subject of the email. |
| 88 | + */ |
| 89 | + public function withSubject(string|Stringable $subject): self |
| 90 | + { |
| 91 | + $this->subject = (string) $subject; |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Defines the HTML body of the email. |
| 98 | + */ |
| 99 | + public function withHtml(string|View $html): self |
| 100 | + { |
| 101 | + $this->html = $html; |
| 102 | + |
| 103 | + return $this; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Defines the text body of the email. |
| 108 | + */ |
| 109 | + public function withText(string $text): self |
| 110 | + { |
| 111 | + $this->text = $text; |
| 112 | + |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Defines the priority of the email. |
| 118 | + */ |
| 119 | + public function withPriority(Priority|int $priority): self |
| 120 | + { |
| 121 | + $this->priority = $priority; |
| 122 | + |
| 123 | + return $this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Defines the headers of the email. |
| 128 | + */ |
| 129 | + public function withHeaders(array $headers): self |
| 130 | + { |
| 131 | + $this->headers = $headers; |
| 132 | + |
| 133 | + return $this; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Defines the attachments of the email. |
| 138 | + * |
| 139 | + * @param Attachment[] $attachments |
| 140 | + */ |
| 141 | + public function withAttachments(array $attachments): self |
| 142 | + { |
| 143 | + foreach ($attachments as $attachment) { |
| 144 | + if (! ($attachment instanceof Attachment)) { |
| 145 | + throw new \InvalidArgumentException(sprintf('All attachments must be instances of `%s`.', Attachment::class)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + $this->attachments = $attachments; |
| 150 | + |
| 151 | + return $this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Adds an attachment to the email. |
| 156 | + */ |
| 157 | + public function withAttachment(Attachment $attachment): self |
| 158 | + { |
| 159 | + $this->attachments[] = $attachment; |
| 160 | + |
| 161 | + return $this; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Adds an attachment from the filesystem. |
| 166 | + */ |
| 167 | + public function withFileAttachment(string $path, ?string $name = null, ?string $contentType = null): self |
| 168 | + { |
| 169 | + $this->attachments[] = FileAttachment::fromPath($path, $name, $contentType); |
| 170 | + |
| 171 | + return $this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Adds an attachment from the storage. |
| 176 | + */ |
| 177 | + public function withStorageAttachment(string $path, ?string $name = null, ?string $contentType = null, null|string|UnitEnum $tag = null): self |
| 178 | + { |
| 179 | + $this->attachments[] = StorageAttachment::fromPath($path, $name, $contentType, $tag); |
| 180 | + |
| 181 | + return $this; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Builds the email. |
| 186 | + */ |
| 187 | + public function make(): Email |
| 188 | + { |
| 189 | + return new GenericEmail( |
| 190 | + envelope: new Envelope( |
| 191 | + subject: $this->subject, |
| 192 | + to: Arr\wrap($this->to), |
| 193 | + from: Arr\wrap($this->from), |
| 194 | + cc: Arr\wrap($this->cc), |
| 195 | + bcc: Arr\wrap($this->bcc), |
| 196 | + replyTo: Arr\wrap($this->replyTo), |
| 197 | + priority: is_int($this->priority) |
| 198 | + ? Priority::from($this->priority) |
| 199 | + : $this->priority, |
| 200 | + headers: $this->headers, |
| 201 | + ), |
| 202 | + content: new Content( |
| 203 | + html: $this->html, |
| 204 | + text: $this->text, |
| 205 | + attachments: $this->attachments, |
| 206 | + ), |
| 207 | + ); |
| 208 | + } |
| 209 | +} |
0 commit comments