<?php
/*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential.
*
* @author Bilel AZRI <azri.bilel@gmail.com>
* @author Assma BEN SASSI <bensassiasma.bws@gmail.com>
*
* Bicking man (c) 2019-present.
*/
declare(strict_types=1);
namespace App\EventSubscriber\Mailer;
use App\Event\UserRegisterEvent;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Twig\Environment;
final class UserRegisterSubscriber implements EventSubscriberInterface
{
private Environment $twig;
private string $senderEmail;
private string $senderName;
private MailerInterface $mailer;
public function __construct(Environment $twig, MailerInterface $mailer, string $senderEmail, string $senderName)
{
$this->twig = $twig;
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->mailer = $mailer;
}
public static function getSubscribedEvents()
{
return [UserRegisterEvent::NAME => 'sendMailToUser'];
}
public function sendMailToUser(UserRegisterEvent $event): void
{
$user = $event->getUser();
$emailContent = $this->twig->render('emails/user/user_register.html.twig', [
'user' => $user,
]);
$email = (new TemplatedEmail())
->from(new Address($this->senderEmail, $this->senderName))
->to((string) $user->getEmail())
->subject('Welcome to BikingMan')
->html($emailContent)
;
$this->mailer->send($email);
}
}