<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
final class RoutingListener
{
/**
* @var Container
*/
private $container;
/**
* @var private $security;
*/
private $security;
/**
* @var private $parameters;
*/
private $parameters;
/**
* Constructor
*
* @param Container $container
*/
public function __construct(Container $container, Security $security, EntityManagerInterface $entityManager)
{
$this->container = $container;
$this->security = $security;
$this->em = $entityManager;
}
public function onKernelRequest(RequestEvent $event)
{
// Set UK timezone
date_default_timezone_set('Europe/London');
$user = $this->security->getUser();
if ($user) {
$this->setUserData($user);
$this->setTwigVariables();
}
}
/**
* This function is to get contact data from contact object
* @param string $key
* @return string
*/
public function get($key)
{
return $this->parameters[$key];
}
/**
* This function is to set contact details in contact object
* @param object $user
*/
public function setUserData($user)
{
$contact = $this->em->getRepository('App:FrContacts')->findOneBy(array('userId'=>$user->getId()));
if($contact){
$this->parameters['id'] = $contact->getId();
$this->parameters['userId'] = $contact->getUserId();
$this->parameters['firstName'] = $contact->getFirstName();
$this->parameters['surName'] = $contact->getSurName();
$this->parameters['fullName'] = $contact->getFirstName() .' '. $contact->getSurName();
$this->parameters['companyName'] = $contact->getCompanyName();
$this->parameters['roles'] = $user->getRoles();
}
$this->parameters['defaultDateFormat'] = 'd-m-Y h:i:s';
$this->parameters['defaultSystemLang'] = 'en';
$this->container->set('contact', $this);
}
/**
* This function is to set contact details in twig object
*/
private function setTwigVariables()
{
$this->container->get('twig')->addGlobal('fr_contact', $this->parameters);
}
}