<?php
namespace App\EventSubscriber\User;
use App\Entity\Boutique\Pays;
use App\Event\UserEvent;
use App\Manager\Boutique\PanierManager;
use App\Utils\Emails\UserEmail;
use App\Entity\User\User;
use App\Entity\User\UserGroup;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RegistrationSubscriber implements EventSubscriberInterface
{
private $router;
private $userEmail;
private $em;
private $panierManager;
private $session;
private $eventDispatcher;
public function __construct(
UrlGeneratorInterface $router,
UserEmail $userEmail,
EntityManagerInterface $entityManager,
PanierManager $panierManager,
SessionInterface $session,
EventDispatcherInterface $eventDispatcher
)
{
$this->router = $router;
$this->userEmail = $userEmail;
$this->em = $entityManager;
$this->panierManager = $panierManager;
$this->session = $session;
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_INITIALIZE => array('onRegistrationInitialize', -10),
FOSUserEvents::REGISTRATION_SUCCESS => array('onRegistrationSuccess', -10),
FOSUserEvents::REGISTRATION_FAILURE => array('onRegistrationFailure', -10),
FOSUserEvents::REGISTRATION_CONFIRMED => array('onRegistrationConfirmed', -10)
);
}
public function onRegistrationInitialize(GetResponseUserEvent $event)
{
$userGroup = $this->em->getRepository(UserGroup::class)
->findOneById(UserGroup::GROUPE_DEFAUT);
$pays = $this->em->getRepository(Pays::class)
->findOneById(Pays::PAYS_DEFAUT);
$user = $event->getUser();
$user->addGroup($userGroup);
$user->addRole(User::ROLE_CLIENT);
$user->setStatut(User::STATUT_ATTENTE);
$user->setPays($pays);
}
public function onRegistrationSuccess(FormEvent $event)
{
$request = $event->getRequest();
// Envoi mail de notification
$user = $event->getForm()->getData();
// On MAJ le client du panier si on en a un et le pays de livraison
$panierId = $this->session->get('panierId');
if (!is_null($panierId)) {
$this->panierManager->modifierUser($user);
if (!is_null($user->getPays())) {
$this->panierManager->modifierLivraisonPays($user, $user->getPays());
}
}
if (getenv('APP_SITE_SLUG') == 'tamtam') {
$form = $event->getForm();
$userGroup = $this->em->getRepository(UserGroup::class)
->findOneBy(['reference' => $form->get('reference')->getData()]);
if (!is_null($userGroup)) {
$user->getGroups()->clear();
$user->addGroup($userGroup);
$user->setStatut(User::STATUT_VALIDE);
}
}
// On envoi le mail de notification
$event = new UserEvent($user, $user, $request);
$this->eventDispatcher->dispatch($event, UserEvent::USER_ENREGISTRER_STATUT);
// $this->userEmail->alerteCreationCompteModeration($user);
}
public function onRegistrationConfirmed(FormEvent $event)
{
$request = $event->getRequest();
// Envoi mail de notification
$user = $event->getForm()->getData();
// On MAJ le client du panier si on en a un et le pays de livraison
$panierId = $this->session->get('panierId');
if (!is_null($panierId)) {
$this->panierManager->modifierUser($user);
if (!is_null($user->getPays())) {
$this->panierManager->modifierLivraisonPays($user, $user->getPays());
}
}
// On envoi le mail de notification
$event = new UserEvent($user, $user, $request);
$this->eventDispatcher->dispatch($event, UserEvent::USER_ENREGISTRER_STATUT);
// $this->userEmail->alerteCreationCompteConfirmation($user);
}
public function onRegistrationFailure(FormEvent $event)
{
$request = $event->getRequest();
$errors = str_replace('ERROR: ', '', $event->getForm()->getErrors(true, true));
if ($request->isXmlHttpRequest()) {
$event->setResponse(new JsonResponse(array('registered' => false, 'errors' => (string) $errors)));
}
}
}