src/EventListener/RoutingListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\DependencyInjection\Container;
  5. use Symfony\Component\Security\Core\Security;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. final class RoutingListener
  8. {
  9.     /**
  10.      * @var Container
  11.      */
  12.      private $container;
  13.     /**
  14.      * @var private $security;
  15.      */
  16.      private $security;
  17.     /**
  18.      * @var private $parameters;
  19.      */
  20.      private $parameters;
  21.     /**
  22.      * Constructor
  23.      *
  24.      * @param Container $container
  25.      */
  26.    public function __construct(Container $containerSecurity $securityEntityManagerInterface $entityManager)
  27.     {
  28.         $this->container $container;
  29.         $this->security $security;
  30.         $this->em $entityManager;
  31.     }
  32.     public function onKernelRequest(RequestEvent $event)
  33.     {
  34.     // Set UK timezone
  35.     date_default_timezone_set('Europe/London');
  36.     $user $this->security->getUser();
  37.     if ($user) {
  38.         $this->setUserData($user);
  39.         $this->setTwigVariables();
  40.     }
  41.     }
  42.     /**
  43.      * This function is to get contact data from contact object
  44.      * @param string $key
  45.      * @return string
  46.      */
  47.     public function get($key)
  48.     {
  49.     return $this->parameters[$key];
  50.     }
  51.     /**
  52.      * This function is to set contact details in contact object
  53.      * @param object $user
  54.      */
  55.     public function setUserData($user)
  56.     {
  57.     $contact $this->em->getRepository('App:FrContacts')->findOneBy(array('userId'=>$user->getId()));
  58.     if($contact){
  59.         $this->parameters['id'] = $contact->getId();
  60.         $this->parameters['userId'] = $contact->getUserId();
  61.         $this->parameters['firstName'] = $contact->getFirstName();
  62.         $this->parameters['surName'] = $contact->getSurName();
  63.         $this->parameters['fullName'] = $contact->getFirstName() .' '$contact->getSurName();
  64.         $this->parameters['companyName'] = $contact->getCompanyName();
  65.         $this->parameters['roles'] = $user->getRoles();
  66.     }
  67.     $this->parameters['defaultDateFormat'] = 'd-m-Y h:i:s';
  68.     $this->parameters['defaultSystemLang'] = 'en';
  69.     $this->container->set('contact'$this);
  70.     }
  71.     /**
  72.      * This function is to set contact details in twig object
  73.      */
  74.     private function setTwigVariables()
  75.     {
  76.     $this->container->get('twig')->addGlobal('fr_contact'$this->parameters);
  77.     }
  78. }