<?php
namespace App\EventListener;
use App\Entity\PrivateUser;
use App\Entity\ProUser;
use App\Entity\User;
use App\Twig\Extension\CountryExtension;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class EmailConfirmationListener implements EventSubscriberInterface
{
public function __construct(private readonly MailerInterface $mailer, private readonly TokenGeneratorInterface $tokenGenerator, private readonly UrlGeneratorInterface $router, private readonly SessionInterface $session, private readonly TokenStorageInterface $tokenStorage)
{
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm',
];
}
public function onRegistrationConfirm($event): void
{
$user = null;
if ($event instanceof FormEvent) {
/** @var $user User */
$user = $event->getForm()->getData();
} elseif ($event instanceof UserEvent) {
$user = $event->getUser();
}
if ($user && CountryExtension::COUNTRY_PORTUGAL == strtolower((string) $user->getCountry())) {
$this->mailer->sendRegisterConfirmEmailMessage($user);
}
}
public function onRegistrationSuccess(FormEvent $event): void
{
/** @var $user User */
$user = $event->getForm()->getData();
$user->setEnabled(false);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
if (in_array(
strtolower((string) $user->getCountry()),
[CountryExtension::COUNTRY_PORTUGAL]
)) {
$this->mailer->sendRegisterRequestEmailMessage($user);
} else {
$this->mailer->sendConfirmationEmailMessage($user);
}
$cookie = false;
if ($user instanceof PrivateUser && in_array(
strtolower((string) $user->getCountry()),
[CountryExtension::COUNTRY_PORTUGAL]
)) {
$url = $this->router->generate('frontend_user_registration_waiting_verification');
$this->session->set('registration-datalayer', 'particulier');
} elseif ($user instanceof PrivateUser) {
$this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
$this->session->set('registration-datalayer', 'particulier');
$this->session->getFlashBag()->add('popup', $this->router->generate(
'registration_step',
['step' => 3],
UrlGeneratorInterface::ABSOLUTE_URL
));
$url = $this->router->generate('homepage');
} else {
$parameters = [];
// Set locale only if it's available
if (!empty($user->getLanguage())) {
$parameters['_locale'] = strtolower((string) $user->getLanguage());
}
/** @var ProUser $user */
if (ProUser::ORIGIN_SHOWVROOM === $user->getOrigin()) {
// Disconnect the user
$this->tokenStorage->setToken(null);
$this->session->invalidate();
// Don't redirect users with account which is disabled, let them see the website and the popup
$this->session->set('showvroom_account_disabled', true);
} else {
$this->session->getFlashBag()->add('popup', $this->router->generate(
'frontend_user_registration_waiting',
$parameters,
UrlGeneratorInterface::ABSOLUTE_URL
));
}
$this->session->set('registration-datalayer', 'pro');
// This will hide registration popup that is unnecessary after registration
$cookie = new Cookie('registration-pro-popup', 1, time() + 60);
$url = $this->router->generate('homepage_pro');
}
$response = new RedirectResponse($url);
if ($cookie) {
$response->headers->setCookie($cookie);
}
$event->setResponse($response);
}
}