src/EventSubscriber/UserLocaleSubscriber.php line 104

Open in your IDE?
  1. <?php 
  2. // src/EventSubscriber/UserLocaleSubscriber.php
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\AuthenticationEvents;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use App\Service\UserService;
  14. use App\Entity\CartCoupon;
  15. use App\Entity\User;
  16. use FOS\UserBundle\FOSUserEvents;
  17. use FOS\UserBundle\Mailer\MailerInterface;
  18. use FOS\UserBundle\Event\FilterUserResponseEvent;
  19. use FOS\UserBundle\Event\UserEvent;
  20. /**
  21.  * Stores the locale of the user in the session after the
  22.  * login. This can be used by the LocaleSubscriber afterwards.
  23.  */
  24. class UserLocaleSubscriber implements EventSubscriberInterface
  25. {
  26.     private $session;
  27.     private $userServ;
  28.     private $containerInter;
  29.     private $em;
  30.     public function __construct(SessionInterface $sessionUserService $userServContainerInterface $containerEntityManagerInterface $em)
  31.     {
  32.         $this->session $session;
  33.         $this->userServ $userServ;
  34.         $this->containerInter $container;
  35.         $this->em $em;
  36.     }
  37.     /*
  38.      * Event when a login occur
  39.      */
  40.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  41.     {
  42.         $user $event->getAuthenticationToken()->getUser();
  43.         if (null !== $user->getLocale()) {
  44.             $this->session->set('_locale'$user->getLocale());
  45.         }
  46.         $this->userServ->transferSession($user);
  47.     }
  48.     /*
  49.      * Event when the registration is completed but yet not confirmed
  50.      */
  51.     public function registrationCompleted(FilterUserResponseEvent $event)
  52.     {
  53.         $user $event->getUser();
  54.         $referredBy $this->session->get('referredBy');
  55.         if(!empty($referredBy)){
  56.             $userReferredBy $this->userServ->getUser($referredBy);
  57.             if($userReferredBy){
  58.                 $userReferredBy->addUsersReferred($user);
  59.                 $this->em->persist($userReferredBy);
  60.                 $this->session->remove('referredBy');
  61.                 //Let's give him the first 10%
  62.                 //as he his referenced
  63.                 $coupon = new CartCoupon();
  64.                 $coupon->setOneTimeOnly(true);
  65.                 $coupon->setType(0);
  66.                 $coupon->setAmount(10);
  67.                 $coupon->setCode('C-'.uniqid());
  68.                 $coupon->setForUser($user);
  69.                 $coupon->setDateFrom(new \DateTime());
  70.                 $coupon->setDateTo(new \DateTime("+1 year"));
  71.                 $this->em->persist($coupon);
  72.                 //Apply coupon to cart right away
  73.                 $user->getCart()->setUsedCoupon($coupon);
  74.                 
  75.                 $this->em->flush();
  76.             }
  77.         }
  78.         // link association if necessary
  79.         if (($association $this->userServ->getAssociationUserIsBrowsing()) !== false){
  80.             $this->userServ->setAssociationUserIsBrowsing($association->getId(), $user);
  81.         }
  82.     }
  83.     /*
  84.      * Event when a new account has been registered
  85.      */
  86.     public function userAccountConfirmed(FilterUserResponseEvent $event)
  87.     {
  88.         $user$event->getUser();
  89.         $this->userServ->transferSession($user);
  90.         $view $this->containerInter->get('templating')->render('emails/welcomeNewAccount.html.twig', array('user' => $user));
  91.         $this->userServ->sendEmail('Bienvenue chez Maturin'$viewfalse$user->getEmail());
  92.                
  93.     }
  94.     /*
  95.     Happen when the user is logged by the system instead of the form
  96.     */
  97.     public function implicitLogin(UserEvent $event){
  98.         $user $event->getUser();
  99.         $this->userServ->transferSession($user);
  100.     }
  101.     /*
  102.      * Manage login fail
  103.      */
  104.     public function loginFail($failureEvent)
  105.     {
  106.         $email $failureEvent->getAuthenticationToken()->getUser();
  107.         $user $this->em->getRepository(User::class)->findOneByEmail($email);
  108.         if($user && !$user->IsEnabled()){
  109.             $mailer $this->containerInter->get('fos_user.mailer');
  110.             $mailer->sendConfirmationEmailMessage($user);
  111.         }
  112.     }
  113.     public static function getSubscribedEvents()
  114.     {
  115.         return array(
  116.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  117.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'loginFail',
  118.             FOSUserEvents::REGISTRATION_CONFIRMED => 'userAccountConfirmed',
  119.             FOSUserEvents::REGISTRATION_COMPLETED => 'registrationCompleted',
  120.             FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'implicitLogin'
  121.         );
  122.     }
  123. }
  124. ?>