feat(mail): add mailer interface with null and log drivers
This commit is contained in:
1 parent
21852bfc7f
commit
2897d0ba89
4 files changed
+80
No files matched your search
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Bazaar\Mail;
|
||||
|
||||
final class Email
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $to,
|
||||
public readonly string $subject,
|
||||
public readonly string $body,
|
||||
public readonly string $from = 'orders@bazaar.test'
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Bazaar\Mail;
|
||||
|
||||
final class LogMailer implements Mailer
|
||||
{
|
||||
private array $sent = [];
|
||||
|
||||
public function __construct(private readonly string $logPath)
|
||||
{
|
||||
}
|
||||
|
||||
public function send(Email $email): void
|
||||
{
|
||||
$this->sent[] = $email;
|
||||
|
||||
$directory = dirname($this->logPath);
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0775, true);
|
||||
}
|
||||
|
||||
$entry = sprintf(
|
||||
"[%s] To: %s | From: %s | Subject: %s\n%s\n%s\n",
|
||||
gmdate('c'),
|
||||
$email->to,
|
||||
$email->from,
|
||||
$email->subject,
|
||||
$email->body,
|
||||
str_repeat('-', 60)
|
||||
);
|
||||
file_put_contents($this->logPath, $entry, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
|
||||
public function sent(): array
|
||||
{
|
||||
return $this->sent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Bazaar\Mail;
|
||||
|
||||
interface Mailer
|
||||
{
|
||||
public function send(Email $email): void;
|
||||
|
||||
public function sent(): array;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Bazaar\Mail;
|
||||
|
||||
final class NullMailer implements Mailer
|
||||
{
|
||||
private array $sent = [];
|
||||
|
||||
public function send(Email $email): void
|
||||
{
|
||||
$this->sent[] = $email;
|
||||
}
|
||||
|
||||
public function sent(): array
|
||||
{
|
||||
return $this->sent;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user