<?php
// src/EventSubscriber/UserLocaleSubscriber.php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\UserService;
use App\Entity\CartCoupon;
use App\Entity\User;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\UserEvent;
/**
* Stores the locale of the user in the session after the
* login. This can be used by the LocaleSubscriber afterwards.
*/
class UserLocaleSubscriber implements EventSubscriberInterface
{
private $session;
private $userServ;
private $containerInter;
private $em;
public function __construct(SessionInterface $session, UserService $userServ, ContainerInterface $container, EntityManagerInterface $em)
{
$this->session = $session;
$this->userServ = $userServ;
$this->containerInter = $container;
$this->em = $em;
}
/*
* Event when a login occur
*/
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if (null !== $user->getLocale()) {
$this->session->set('_locale', $user->getLocale());
}
$this->userServ->transferSession($user);
}
/*
* Event when the registration is completed but yet not confirmed
*/
public function registrationCompleted(FilterUserResponseEvent $event)
{
$user = $event->getUser();
$referredBy = $this->session->get('referredBy');
if(!empty($referredBy)){
$userReferredBy = $this->userServ->getUser($referredBy);
if($userReferredBy){
$userReferredBy->addUsersReferred($user);
$this->em->persist($userReferredBy);
$this->session->remove('referredBy');
//Let's give him the first 10%
//as he his referenced
$coupon = new CartCoupon();
$coupon->setOneTimeOnly(true);
$coupon->setType(0);
$coupon->setAmount(10);
$coupon->setCode('C-'.uniqid());
$coupon->setForUser($user);
$coupon->setDateFrom(new \DateTime());
$coupon->setDateTo(new \DateTime("+1 year"));
$this->em->persist($coupon);
//Apply coupon to cart right away
$user->getCart()->setUsedCoupon($coupon);
$this->em->flush();
}
}
// link association if necessary
if (($association = $this->userServ->getAssociationUserIsBrowsing()) !== false){
$this->userServ->setAssociationUserIsBrowsing($association->getId(), $user);
}
}
/*
* Event when a new account has been registered
*/
public function userAccountConfirmed(FilterUserResponseEvent $event)
{
$user= $event->getUser();
$this->userServ->transferSession($user);
$view = $this->containerInter->get('templating')->render('emails/welcomeNewAccount.html.twig', array('user' => $user));
$this->userServ->sendEmail('Bienvenue chez Maturin', $view, false, $user->getEmail());
}
/*
Happen when the user is logged by the system instead of the form
*/
public function implicitLogin(UserEvent $event){
$user = $event->getUser();
$this->userServ->transferSession($user);
}
/*
* Manage login fail
*/
public function loginFail($failureEvent)
{
$email = $failureEvent->getAuthenticationToken()->getUser();
$user = $this->em->getRepository(User::class)->findOneByEmail($email);
if($user && !$user->IsEnabled()){
$mailer = $this->containerInter->get('fos_user.mailer');
$mailer->sendConfirmationEmailMessage($user);
}
}
public static function getSubscribedEvents()
{
return array(
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
AuthenticationEvents::AUTHENTICATION_FAILURE => 'loginFail',
FOSUserEvents::REGISTRATION_CONFIRMED => 'userAccountConfirmed',
FOSUserEvents::REGISTRATION_COMPLETED => 'registrationCompleted',
FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'implicitLogin'
);
}
}
?>