<?php
namespace App\EventSubscriber\User;
use App\Constant\EmailConfiguration;
use App\Entity\User\Partenaire;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
use App\Service\User\ExpertUserManager;
use App\Events\User\UserEvent;
use App\AppEvents;
//use App\Security\Rights\ModuleRights;
class UserCreatePostActionSubscriber implements EventSubscriberInterface
{
const PLATFORM = 'MyFidelia';
private $em;
private $mailer;
private $token;
private $template;
private $router;
private $usermanager;
private $dispatcher;
protected $request;
public function __construct(
EntityManagerInterface $em,
MailerInterface $mailer,
Environment $template,
TokenStorageInterface $token,
RouterInterface $router,
ExpertUserManager $usermanager,
EventDispatcherInterface $dispatcher,
RequestStack $requestStack
)
{
$this->em = $em;
$this->mailer = $mailer;
$this->token = $token;
$this->template = $template;
$this->router = $router;
$this->usermanager = $usermanager;
$this->dispatcher = $dispatcher;
$this->request = $requestStack->getCurrentRequest();;
}
public function onUserCreated(UserEvent $event)
{
$user = $event->getUser();
$email = (new TemplatedEmail())
->from(new Address(EmailConfiguration::SENDER_EMAIL, EmailConfiguration::SENDER_NAME))
->to($user->getEmail())
->subject(EmailConfiguration::SUBJECT_ACCOUNT_ACTIVATION)
->context([
'user' => $user
])
->htmlTemplate(EmailConfiguration::ACCOUNT_ACTIVATION_EMAIL);
$this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email) {
$this->mailer->send($email);
});
}
public function onPasswordForgotten(UserEvent $event){
$user = $event->getUser();
$email = (new TemplatedEmail())
->from(new Address(EmailConfiguration::SENDER_EMAIL, EmailConfiguration::SENDER_NAME))
->to($user->getEmail())
->subject(EmailConfiguration::SUBJECT_PASSWORD_FORGOTTEN)
->context([
'user' => $user,
'code' => $user->getActivationCode()
])
->htmlTemplate(EmailConfiguration::PASSWORD_FORGOTTEN_EMAIL);
$this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email) {
$this->mailer->send($email);
});
}
public function onPasswordReset(UserEvent $event){
$user = $event->getUser();
$email = (new TemplatedEmail())
->from(new Address(EmailConfiguration::SENDER_EMAIL, EmailConfiguration::SENDER_NAME))
->to($user->getEmail())
->subject(EmailConfiguration::SUBJECT_PASSWORD_RESET)
->context([
'user' => $user
])
->htmlTemplate(EmailConfiguration::PASSWORD_RESET_EMAIL);
$this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email) {
$this->mailer->send($email);
});
}
public static function getSubscribedEvents()
{
return [
AppEvents::USER_CREATE_SUCCESS => 'onUserCreated',
AppEvents::USER_PASSWORD_FORGOTTEN => 'onPasswordForgotten',
AppEvents::USER_PASSWORD_RESET => 'onPasswordReset'
];
}
}