Skip to content

Commit 50bbd49

Browse files
committed
wip
1 parent e1c3c48 commit 50bbd49

File tree

14 files changed

+90
-117
lines changed

14 files changed

+90
-117
lines changed

packages/mailer/src/Attachment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @param null|string $contentType Content type of the attachment.
1919
*/
2020
public function __construct(
21-
public \Closure $resolve,
21+
public Closure $resolve,
2222
public ?string $name = null,
2323
public ?string $contentType = null,
2424
) {}

packages/mailer/src/Content.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/mailer/src/Email.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tempest\Mail;
44

5+
use Tempest\View\View;
6+
57
/**
68
* Represents an email.
79
*/
@@ -15,9 +17,16 @@ interface Email
1517
}
1618

1719
/**
18-
* The content of the email.
20+
* The content of the email can be a path to a view file, raw HTML, or a View object
21+
*/
22+
public string|View $content {
23+
get;
24+
}
25+
26+
/**
27+
* @var \Tempest\Mail\Attachment[] $attachments
1928
*/
20-
public Content $content {
29+
public array $attachments {
2130
get;
2231
}
2332
}

packages/mailer/src/EmailBuilder.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public function __construct(
2525
private(set) null|string|array|ArrayInterface|Address $cc = null,
2626
private(set) null|string|array|ArrayInterface|Address $bcc = null,
2727
private(set) ?string $subject = null,
28-
private(set) null|string|View $html = null,
29-
private(set) ?string $text = null,
28+
private(set) ?string $content = null,
3029
private(set) array $attachments = [],
3130
private(set) EmailPriority|int $priority = EmailPriority::NORMAL,
3231
private(set) array $headers = [],
@@ -93,21 +92,11 @@ public function subject(string|Stringable $subject): self
9392
}
9493

9594
/**
96-
* Defines the HTML body of the email.
95+
* Defines the content of the email.
9796
*/
98-
public function html(string|View $html): self
97+
public function content(string|View $content): self
9998
{
100-
$this->html = $html;
101-
102-
return $this;
103-
}
104-
105-
/**
106-
* Defines the text body of the email.
107-
*/
108-
public function text(string $text): self
109-
{
110-
$this->text = $text;
99+
$this->content = $content;
111100

112101
return $this;
113102
}
@@ -137,13 +126,7 @@ public function headers(array $headers): self
137126
*/
138127
public function attach(Attachment ...$attachments): self
139128
{
140-
foreach ($attachments as $attachment) {
141-
if (! ($attachment instanceof Attachment)) {
142-
throw new \InvalidArgumentException(sprintf('All attachments must be instances of `%s`.', Attachment::class));
143-
}
144-
145-
$this->attachments[] = $attachment;
146-
}
129+
$this->attachments = [...$this->attachments, ...$attachments];
147130

148131
return $this;
149132
}
@@ -176,16 +159,15 @@ public function make(): Email
176159
return new GenericEmail(
177160
subject: $this->subject,
178161
to: Arr\wrap($this->to),
162+
content: $this->content,
179163
from: Arr\wrap($this->from),
180164
cc: Arr\wrap($this->cc),
181165
bcc: Arr\wrap($this->bcc),
182166
replyTo: Arr\wrap($this->replyTo),
167+
headers: $this->headers,
183168
priority: is_int($this->priority)
184169
? EmailPriority::from($this->priority)
185170
: $this->priority,
186-
headers: $this->headers,
187-
html: $this->html,
188-
text: $this->text,
189171
attachments: $this->attachments,
190172
);
191173
}

packages/mailer/src/EmailToSymfonyEmailMapper.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
final readonly class EmailToSymfonyEmailMapper implements Mapper
2020
{
2121
public function __construct(
22-
private readonly MailerConfig $mailerConfig,
23-
private readonly ViewRenderer $viewRenderer,
22+
private MailerConfig $mailerConfig,
23+
private ViewRenderer $viewRenderer,
2424
) {}
2525

2626
public function canMap(mixed $from, mixed $to): bool
@@ -80,19 +80,20 @@ public function map(mixed $from, mixed $to): SymfonyEmail
8080

8181
$symfonyEmail->priority($email->envelope->priority->value);
8282

83-
if ($email->content->text) {
84-
$symfonyEmail->text($email->content->text);
83+
if ($email->content instanceof View) {
84+
$symfonyEmail->html($this->viewRenderer->render($email->content));
85+
} elseif (is_file($email->content)) {
86+
$symfonyEmail->html($this->viewRenderer->render($email->content));
87+
} else {
88+
$symfonyEmail->text($email->content);
8589
}
8690

87-
if ($email->content->html instanceof View) {
88-
$symfonyEmail->html($this->viewRenderer->render($email->content->html));
89-
} elseif ($email->content->html) {
90-
$symfonyEmail->html($email->content->html);
91-
}
92-
93-
/** @var Attachment $attachment */
94-
foreach (Arr\wrap($email->content->attachments) as $attachment) {
95-
$symfonyEmail->attach(($attachment->resolve)(), $attachment->name, $attachment->contentType);
91+
foreach ($email->attachments as $attachment) {
92+
$symfonyEmail->attach(
93+
body: ($attachment->resolve)(),
94+
name: $attachment->name,
95+
contentType: $attachment->contentType
96+
);
9697
}
9798

9899
return $symfonyEmail;

packages/mailer/src/GenericEmail.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,16 @@ final class GenericEmail implements Email
2222
);
2323
}
2424

25-
public Content $content {
26-
get => new Content(
27-
html: $this->html,
28-
text: $this->text,
29-
attachments: $this->attachments,
30-
);
31-
}
32-
3325
public function __construct(
3426
public ?string $subject,
3527
public null|string|array|Address $to,
28+
public string|View $content,
3629
public null|string|array|Address $from = null,
3730
public null|string|array|Address $cc = null,
3831
public null|string|array|Address $bcc = null,
3932
public null|string|array|Address $replyTo = null,
4033
public array $headers = [],
4134
public EmailPriority $priority = EmailPriority::NORMAL,
42-
public null|string|View $html = null,
43-
public ?string $text = null,
4435
public array $attachments = [],
4536
) {}
4637

packages/mailer/src/GenericMailer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Symfony\Component\Mailer\Transport\TransportInterface;
66
use Tempest\EventBus\EventBus;
77
use Tempest\Mail\Exceptions\MailerTransportWasMissing;
8-
use Tempest\Mail\MailerConfig;
98
use Tempest\View\ViewRenderer;
109

1110
use function Tempest\map;
@@ -16,9 +15,9 @@
1615
final class GenericMailer implements Mailer
1716
{
1817
public function __construct(
19-
private MailerConfig $mailerConfig,
20-
private ViewRenderer $viewRenderer,
21-
private ?EventBus $eventBus = null,
18+
private readonly MailerConfig $mailerConfig,
19+
private readonly ViewRenderer $viewRenderer,
20+
private readonly ?EventBus $eventBus = null,
2221
private ?TransportInterface $transport = null,
2322
) {
2423
$this->transport ??= $this->createTransport();

packages/mailer/src/Testing/SentTestingEmail.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ public function assertNotSee(string $expect): self
228228
*/
229229
public function assertSeeInHtml(string $expect): self
230230
{
231+
Assert::assertNotNull(
232+
actual: $this->symfonyEmail->getHtmlBody(),
233+
message: 'The email does not contain an HTML body.',
234+
);
235+
231236
Assert::assertStringContainsString(
232237
needle: $expect,
233238
haystack: $this->symfonyEmail->getHtmlBody(),
@@ -256,6 +261,11 @@ public function assertNotSeeInHtml(string $expect): self
256261
*/
257262
public function assertSeeInText(string $expect): self
258263
{
264+
Assert::assertNotNull(
265+
actual: $this->symfonyEmail->getTextBody(),
266+
message: 'The email does not contain a text body.',
267+
);
268+
259269
Assert::assertStringContainsString(
260270
needle: $expect,
261271
haystack: $this->symfonyEmail->getTextBody(),

packages/mailer/tests/EmailBuilderTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ public function test_builder(): void
2323
2424
->subject('Important: Please come to my office right away')
2525
->attachFromFileystem(__DIR__ . '/Fixtures/attachment.txt')
26-
->text('Gotcha!')
26+
->content('Gotcha!')
2727
->make();
2828

29-
$this->assertInstanceOf(Email::class, $email);
3029
$this->assertContains('[email protected]', $email->envelope->to);
3130
$this->assertContains('[email protected]', $email->envelope->cc);
3231
$this->assertContains('[email protected]', $email->envelope->cc);
3332
$this->assertContains('[email protected]', $email->envelope->bcc);
3433
$this->assertSame('Important: Please come to my office right away', $email->envelope->subject);
35-
$this->assertSame('Gotcha!', $email->content->text);
36-
$this->assertCount(1, $email->content->attachments);
34+
$this->assertSame('Gotcha!', $email->content);
35+
$this->assertCount(1, $email->attachments);
3736

3837
$attachment = $email->content->attachments[0];
3938

tests/Integration/Mailer/Fixtures/SendWelcomeEmail.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Tests\Tempest\Integration\Mailer\Fixtures;
44

5-
use Tempest\Mail\Content;
65
use Tempest\Mail\Email;
76
use Tempest\Mail\Envelope;
8-
7+
use Tempest\View\View;
98
use function Tempest\view;
109

1110
final class SendWelcomeEmail implements Email
@@ -18,12 +17,14 @@ final class SendWelcomeEmail implements Email
1817
);
1918
}
2019

21-
public Content $content {
22-
get => new Content(
23-
html: view(__DIR__ . '/welcome.view.php', fullName: $this->fullName),
24-
);
20+
public string|View $content {
21+
get {
22+
return view(__DIR__ . '/welcome.view.php', fullName: $this->fullName);
23+
}
2524
}
2625

26+
public array $attachments = [];
27+
2728
public function __construct(
2829
private readonly string $address,
2930
private readonly string $fullName,

0 commit comments

Comments
 (0)