<?php
/**
* This file is part of an Innatial Developers development or collaboration.
*
* @author Innatial Developers, S.L.
* @link https://innatial.com
* @copyright Copyright (c) 2020. Innatial Developers, S.L.
*
* All rights are reserved.
* Copyright (c) 2020. Innatial Developers, S.L.
*/
namespace App\EventSubscriber;
use App\Event\UserForgotPasswordEvent;
use App\Event\UserResetPasswordEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class PasswordResetSubscriber implements EventSubscriberInterface{
protected $mailer;
protected $twig;
protected $translator;
public function __construct(\Swift_Mailer $mailer, EngineInterface $twig, TranslatorInterface $translator)
{
$this->mailer = $mailer;
$this->twig = $twig;
$this->translator = $translator;
}
public static function getSubscribedEvents()
{
return [
UserForgotPasswordEvent::NAME => 'onUserPasswordForgotEvent',
UserResetPasswordEvent::NAME => 'onUserPasswordResetEvent',
];
}
public function onUserPasswordForgotEvent(UserForgotPasswordEvent $event){
$user = $event->getUser();
$message = (new \Swift_Message($this->translator->trans('auth.mailer.templates.user_forgot_password.subject')))
->setTo($user->getEmail())
->setBody(
$this->twig->render(
'mailer/user_forgot_password.html.twig',
['user' => $user]
),
'text/html'
);
$this->mailer->send($message);
}
public function onUserPasswordResetEvent(UserResetPasswordEvent $event){
$user = $event->getUser();
$message = (new \Swift_Message($this->translator->trans('auth.mailer.templates.user_resetted_password_successfully.subject')))
->setTo($user->getEmail())
->setBody(
$this->twig->render(
'mailer/user_reset_password.html.twig',
['user' => $user]
),
'text/html'
);
$this->mailer->send($message);
}
}