<?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\UserResetPasswordSendTokenEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
final class UserResetPasswordSendTokenSubscriber implements EventSubscriberInterface
{
private string $senderEmail;
private MailerInterface $mailer;
private EntityManagerInterface $entityManager;
private string $senderName;
private Environment $twig;
private UrlGeneratorInterface $urlGenerator;
public function __construct(EntityManagerInterface $entityManager, MailerInterface $mailer, Environment $twig,
string $senderEmail, string $senderName, UrlGeneratorInterface $urlGenerator)
{
$this->entityManager = $entityManager;
$this->mailer = $mailer;
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
}
public static function getSubscribedEvents()
{
return [UserResetPasswordSendTokenEvent::NAME => 'sendMailToUser'];
}
public function sendMailToUser(UserResetPasswordSendTokenEvent $event): void
{
$context = [
'resetToken' => $event->getResetToken(),
'tokenLifetime' => $event->getTokenLifeTime(),
];
$user = $event->getUser();
// $context['site'] = 'url app front';
$emailContent = $this->twig->render('emails/user/reset_password_send_token.html.twig',
$context);
$email = (new TemplatedEmail())
->from(new Address($this->senderEmail, $this->senderName))
->to((string) $user->getEmail())
->subject('BikingMan - modification mot de passe')
->html($emailContent)
;
$this->mailer->send($email);
}
}