add Emailer and EmailFactory interfaces with laravel + fake impls
This commit is contained in:
parent
e16cb45387
commit
2890781a56
6 changed files with 104 additions and 0 deletions
8
backend/app/Email/EmailFactory.php
Normal file
8
backend/app/Email/EmailFactory.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Email;
|
||||||
|
|
||||||
|
interface EmailFactory
|
||||||
|
{
|
||||||
|
public function makeConfirmationEmail(string $token): string;
|
||||||
|
}
|
||||||
8
backend/app/Email/Emailer.php
Normal file
8
backend/app/Email/Emailer.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Email;
|
||||||
|
|
||||||
|
interface Emailer
|
||||||
|
{
|
||||||
|
public function send(string $from, string $to, string $body): void;
|
||||||
|
}
|
||||||
15
backend/app/Email/LaravelEmailFactory.php
Normal file
15
backend/app/Email/LaravelEmailFactory.php
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Email;
|
||||||
|
|
||||||
|
class LaravelEmailFactory implements EmailFactory
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private string $confirmationUrlPrefix,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function makeConfirmationEmail(string $token): string
|
||||||
|
{
|
||||||
|
return "Confirm your email: {$this->confirmationUrlPrefix}{$token}";
|
||||||
|
}
|
||||||
|
}
|
||||||
25
backend/app/Email/LaravelMailer.php
Normal file
25
backend/app/Email/LaravelMailer.php
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Email;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Mail\Mailer;
|
||||||
|
use Illuminate\Mail\Message;
|
||||||
|
|
||||||
|
class LaravelMailer implements Emailer
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private Mailer $mailer,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function send(string $from, string $to, string $body): void
|
||||||
|
{
|
||||||
|
$this->mailer->raw(
|
||||||
|
$body,
|
||||||
|
function (Message $message) use ($from, $to) {
|
||||||
|
$message->from($from)
|
||||||
|
->to($to)
|
||||||
|
->subject('TIDE');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
backend/tests/Fakes/FakeEmailFactory.php
Normal file
13
backend/tests/Fakes/FakeEmailFactory.php
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fakes;
|
||||||
|
|
||||||
|
use App\Email\EmailFactory;
|
||||||
|
|
||||||
|
class FakeEmailFactory implements EmailFactory
|
||||||
|
{
|
||||||
|
public function makeConfirmationEmail(string $token): string
|
||||||
|
{
|
||||||
|
return "confirm:{$token}";
|
||||||
|
}
|
||||||
|
}
|
||||||
35
backend/tests/Fakes/FakeEmailer.php
Normal file
35
backend/tests/Fakes/FakeEmailer.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fakes;
|
||||||
|
|
||||||
|
use App\Email\Emailer;
|
||||||
|
|
||||||
|
class FakeEmailer implements Emailer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array<int, array{from: string, to: string, body: string}>
|
||||||
|
*/
|
||||||
|
private array $sentEmails = [];
|
||||||
|
|
||||||
|
public function send(string $from, string $to, string $body): void
|
||||||
|
{
|
||||||
|
$this->sentEmails[] = [
|
||||||
|
'from' => $from,
|
||||||
|
'to' => $to,
|
||||||
|
'body' => $body,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNumberOfEmailsSent(): int
|
||||||
|
{
|
||||||
|
return count($this->sentEmails);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{from: string, to: string, body: string}>
|
||||||
|
*/
|
||||||
|
public function getSentEmails(): array
|
||||||
|
{
|
||||||
|
return $this->sentEmails;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue