Skip to content

Commit 9197462

Browse files
committed
refactor: rename email builder
1 parent 9b11f79 commit 9197462

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
<?php
22

3-
namespace Tempest\Mail\Builder;
3+
namespace Tempest\Mail;
44

55
use Stringable;
66
use Tempest\Mail\Address;
7-
use Tempest\Mail\Attachments\Attachment;
8-
use Tempest\Mail\Attachments\FileAttachment;
9-
use Tempest\Mail\Attachments\StorageAttachment;
7+
use Tempest\Mail\Attachment;
108
use Tempest\Mail\Content;
11-
use Tempest\Mail\Email as EmailInterface;
9+
use Tempest\Mail\Email;
1210
use Tempest\Mail\EmailPriority;
1311
use Tempest\Mail\Envelope;
1412
use Tempest\Mail\GenericEmail;
13+
use Tempest\Storage\Storage;
1514
use Tempest\Support\Arr;
1615
use Tempest\Support\Arr\ArrayInterface;
1716
use Tempest\View\View;
18-
use UnitEnum;
1917

2018
/**
2119
* A builder class for creating email objects.
2220
*/
23-
final class Email
21+
final class EmailBuilder
2422
{
2523
public function __construct(
2624
private(set) null|string|array|ArrayInterface|Address $to = null,
@@ -31,9 +29,9 @@ public function __construct(
3129
private(set) ?string $subject = null,
3230
private(set) null|string|View $html = null,
3331
private(set) ?string $text = null,
32+
private(set) array $attachments = [],
3433
private(set) EmailPriority|int $priority = EmailPriority::NORMAL,
3534
private(set) array $headers = [],
36-
private(set) array $attachments = [],
3735
) {}
3836

3937
/**
@@ -157,25 +155,25 @@ public function attach(Attachment ...$attachments): self
157155
*/
158156
public function attachFromFileystem(string $path, ?string $name = null, ?string $contentType = null): self
159157
{
160-
$this->attachments[] = FileAttachment::fromPath($path, $name, $contentType);
158+
$this->attachments[] = Attachment::fromFilesystem($path, $name, $contentType);
161159

162160
return $this;
163161
}
164162

165163
/**
166164
* Adds an attachment from the storage.
167165
*/
168-
public function attachFromStorage(string $path, ?string $name = null, ?string $contentType = null, null|string|UnitEnum $tag = null): self
166+
public function attachFromStorage(Storage $storage, string $path, ?string $name = null, ?string $contentType = null): self
169167
{
170-
$this->attachments[] = StorageAttachment::fromPath($path, $name, $contentType, $tag);
168+
$this->attachments[] = Attachment::fromStorage($storage, $path, $name, $contentType);
171169

172170
return $this;
173171
}
174172

175173
/**
176174
* Builds the email.
177175
*/
178-
public function make(): EmailInterface
176+
public function make(): Email
179177
{
180178
return new GenericEmail(
181179
envelope: new Envelope(

packages/mailer/src/GenericEmail.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public function __construct(
1111
public Envelope $envelope,
1212
public Content $content,
1313
) {}
14+
15+
/**
16+
* Builds a new generic email.
17+
*/
18+
public static function build(): EmailBuilder
19+
{
20+
return new EmailBuilder();
21+
}
1422
}

packages/mailer/tests/EmailBuilderTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
namespace Tempest\Mail\Tests;
44

55
use PHPUnit\Framework\TestCase;
6-
use Tempest\Mail\Attachments\FileAttachment;
7-
use Tempest\Mail\Builder\Email;
8-
use Tempest\Mail\Email as EmailInterface;
6+
use Tempest\Mail\Attachment;
7+
use Tempest\Mail\Email;
8+
use Tempest\Mail\EmailBuilder;
9+
use Tempest\Mail\GenericEmail;
910

1011
final class EmailBuilderTest extends TestCase
1112
{
13+
public function test_generic_email_builds_emails(): void
14+
{
15+
$this->assertInstanceOf(EmailBuilder::class, GenericEmail::build());
16+
}
17+
1218
public function test_builder(): void
1319
{
14-
$email = new Email()
20+
$email = new EmailBuilder()
1521
1622
1723
@@ -20,7 +26,7 @@ public function test_builder(): void
2026
->text('Gotcha!')
2127
->make();
2228

23-
$this->assertInstanceOf(EmailInterface::class, $email);
29+
$this->assertInstanceOf(Email::class, $email);
2430
$this->assertContains('[email protected]', $email->envelope->to);
2531
$this->assertContains('[email protected]', $email->envelope->cc);
2632
$this->assertContains('[email protected]', $email->envelope->cc);
@@ -31,7 +37,7 @@ public function test_builder(): void
3137

3238
$attachment = $email->content->attachments[0];
3339

34-
$this->assertInstanceOf(FileAttachment::class, $attachment);
40+
$this->assertInstanceOf(Attachment::class, $attachment);
3541
$this->assertSame('attachment.txt', $attachment->name);
3642
$this->assertSame('text/plain', $attachment->contentType);
3743
}

0 commit comments

Comments
 (0)