Last active
August 21, 2022 19:33
-
-
Save mikemix/b077456cc1d72b359062808a4f83e740 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Notification\Implementation; | |
use App\Notification\EmailNotificationInterface; | |
use App\Notification\NotificationInterface; | |
use App\Notification\NotificationSendException; | |
use App\Notification\TransportInterface; | |
use Symfony\Component\Mailer\MailerInterface; | |
use Symfony\Component\Mime\Email; | |
/** | |
* {@inheritDoc} | |
* | |
* @template-implements TransportInterface<EmailNotificationInterface> | |
*/ | |
final class SymfonyMailerTransport implements TransportInterface | |
{ | |
private MailerInterface $mailer; | |
public function __construct(MailerInterface $mailer) | |
{ | |
$this->mailer = $mailer; | |
} | |
public function supports(NotificationInterface $notification): bool | |
{ | |
return $notification instanceof EmailNotificationInterface; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function deliver(NotificationInterface $notification): void | |
{ | |
$email = (new Email()) | |
->to($notification->getTo()->getAddress()) | |
->subject($notification->getSubject()) | |
->html($notification->getBody()); | |
try { | |
$this->mailer->send($email); | |
} catch (\Exception $exception) { | |
throw NotificationSendException::duringDelivery($exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment