<?php
declare(strict_types = 1);
namespace App\Subscriber;
use App\Infra\Events\SessionMessageEvent;
use App\Infra\Events\UserEvent;
use App\Infra\Services\MailerService;
use App\Security\EmailVerifier;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
UserEvent::USER_REGISTER => 'userRegister',
UserEvent::USER_RETRIEVE_PASSWORD => 'userRetrievePassword',
UserEvent::USER_RETRIEVE_PASSWORD_VALIDATION => 'userRetrievePasswordValidation'
];
}
public function __construct(
MailerService $mailerService,
EmailVerifier $emailVerifier,
EventDispatcherInterface $eventDispatcher
) {}
public function userRegister(UserEvent $event)
{
$this->eventDispatcher->dispatch(
new SessionMessageEvent(
'success',
'Votre compte a bien été crée, merci de valider votre adresse mail pour vous connecter'),
SessionMessageEvent::SESSION_MESSAGE
);
$this->emailVerifier->sendEmailConfirmation($event->getUser());
}
public function userRetrievePassword(UserEvent $event)
{
$this->eventDispatcher->dispatch(
new SessionMessageEvent(
'success',
'Un email de réinitialisation a bien été envoyé'),
SessionMessageEvent::SESSION_MESSAGE
);
$this->mailerService->sendMail(
$_ENV['ADMIN_MAIL'],
$event->getUser()->getEmail(),
'Mot de passe oublié',
'password/retrieve_password_email.html.twig', ['user' => $event->getUser()]
);
}
public function userRetrievePasswordValidation()
{
$this->eventDispatcher->dispatch(
new SessionMessageEvent(
'success',
'Votre mot de passe est changé'),
SessionMessageEvent::SESSION_MESSAGE
);
}
}