Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active August 21, 2022 19:33
Show Gist options
  • Save mikemix/b077456cc1d72b359062808a4f83e740 to your computer and use it in GitHub Desktop.
Save mikemix/b077456cc1d72b359062808a4f83e740 to your computer and use it in GitHub Desktop.
<?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