src/EventSubscriber/User/RegistrationSubscriber.php line 136

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\User;
  3. use App\Entity\Boutique\Pays;
  4. use App\Event\UserEvent;
  5. use App\Manager\Boutique\PanierManager;
  6. use App\Utils\Emails\UserEmail;
  7. use App\Entity\User\User;
  8. use App\Entity\User\UserGroup;
  9. use FOS\UserBundle\FOSUserEvents;
  10. use FOS\UserBundle\Event\FormEvent;
  11. use FOS\UserBundle\Event\GetResponseUserEvent;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Form\FormError;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. class RegistrationSubscriber implements EventSubscriberInterface
  22. {
  23.     private $router;
  24.     private $userEmail;
  25.     private $em;
  26.     private $panierManager;
  27.     private $session;
  28.     private $eventDispatcher;
  29.     public function __construct(
  30.         UrlGeneratorInterface $router,
  31.         UserEmail $userEmail,
  32.         EntityManagerInterface $entityManager,
  33.         PanierManager $panierManager,
  34.         SessionInterface $session,
  35.         EventDispatcherInterface $eventDispatcher
  36.     )
  37.     {
  38.         $this->router               $router;
  39.         $this->userEmail            $userEmail;
  40.         $this->em                   $entityManager;
  41.         $this->panierManager        $panierManager;
  42.         $this->session              $session;
  43.         $this->eventDispatcher      $eventDispatcher;
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return array(
  48.             FOSUserEvents::REGISTRATION_INITIALIZE  => array('onRegistrationInitialize', -10),
  49.             FOSUserEvents::REGISTRATION_SUCCESS     => array('onRegistrationSuccess', -10),
  50.             FOSUserEvents::REGISTRATION_FAILURE     => array('onRegistrationFailure', -10),
  51.             FOSUserEvents::REGISTRATION_CONFIRMED   => array('onRegistrationConfirmed', -10)
  52.         );
  53.     }
  54.     public function onRegistrationInitialize(GetResponseUserEvent $event)
  55.     {
  56.         $userGroup $this->em->getRepository(UserGroup::class)
  57.             ->findOneById(UserGroup::GROUPE_DEFAUT);
  58.         $pays $this->em->getRepository(Pays::class)
  59.             ->findOneById(Pays::PAYS_DEFAUT);
  60.         $user $event->getUser();
  61.         $user->addGroup($userGroup);
  62.         $user->addRole(User::ROLE_CLIENT);
  63.         $user->setStatut(User::STATUT_ATTENTE);
  64.         $user->setPays($pays);
  65.     }
  66.     public function onRegistrationSuccess(FormEvent $event)
  67.     {
  68.         $request $event->getRequest();
  69.         // Envoi mail de notification
  70.         $user $event->getForm()->getData();
  71.         // On MAJ le client du panier si on en a un et le pays de livraison
  72.         $panierId $this->session->get('panierId');
  73.         if (!is_null($panierId)) {
  74.             $this->panierManager->modifierUser($user);
  75.             if (!is_null($user->getPays())) {
  76.                 $this->panierManager->modifierLivraisonPays($user$user->getPays());
  77.             }
  78.         }
  79.         if (getenv('APP_SITE_SLUG') == 'tamtam') {
  80.             $form $event->getForm();
  81.             $userGroup $this->em->getRepository(UserGroup::class)
  82.                 ->findOneBy(['reference' => $form->get('reference')->getData()]);
  83.             if (!is_null($userGroup)) {
  84.                 $user->getGroups()->clear();
  85.                 $user->addGroup($userGroup);
  86.                 $user->setStatut(User::STATUT_VALIDE);
  87.             }
  88.         }
  89.         // On envoi le mail de notification
  90.         $event = new UserEvent($user$user$request);
  91.         $this->eventDispatcher->dispatch($eventUserEvent::USER_ENREGISTRER_STATUT);
  92. //        $this->userEmail->alerteCreationCompteModeration($user);
  93.     }
  94.     public function onRegistrationConfirmed(FormEvent $event)
  95.     {
  96.         $request $event->getRequest();
  97.         // Envoi mail de notification
  98.         $user $event->getForm()->getData();
  99.         // On MAJ le client du panier si on en a un et le pays de livraison
  100.         $panierId $this->session->get('panierId');
  101.         if (!is_null($panierId)) {
  102.             $this->panierManager->modifierUser($user);
  103.             if (!is_null($user->getPays())) {
  104.                 $this->panierManager->modifierLivraisonPays($user$user->getPays());
  105.             }
  106.         }
  107.         // On envoi le mail de notification
  108.         $event = new UserEvent($user$user$request);
  109.         $this->eventDispatcher->dispatch($eventUserEvent::USER_ENREGISTRER_STATUT);
  110. //        $this->userEmail->alerteCreationCompteConfirmation($user);
  111.     }
  112.     public function onRegistrationFailure(FormEvent $event)
  113.     {
  114.         $request  $event->getRequest();
  115.         $errors   str_replace('ERROR: '''$event->getForm()->getErrors(truetrue));
  116.         if ($request->isXmlHttpRequest()) {
  117.             $event->setResponse(new JsonResponse(array('registered' => false'errors' => (string) $errors)));
  118.         }
  119.     }
  120. }