src/EventListener/RequestListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use App\Repository\CompanyRepository;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class  RequestListener
  11. {
  12.   private $tokenStorage;
  13.   private $entityManagerInterface;
  14.   private $companyRepository;
  15.   private ParameterBagInterface $parameter
  16.   public function __construct(TokenStorageInterface $tokenStorageInterfaceEntityManagerInterface $entityManagerInterfaceParameterBagInterface $parameterBag,CompanyRepository $companyRepository)
  17.   {
  18.     $this->tokenStorage $tokenStorageInterface;
  19.     $this->entityManagerInterface $entityManagerInterface;
  20.     $this->parameter $parameterBag;
  21.     $this->companyRepository $companyRepository;
  22.   }
  23.   public function onKernelRequest(RequestEvent $requestEvent)
  24.   {
  25.      
  26.     //get domain_name
  27.     $request $requestEvent->getRequest();
  28.     $host $request->getHost(); 
  29.     
  30.     $company $this->companyRepository->findByDomaineNameHost($host)
  31.      ;
  32.      if (!empty($company)) {
  33.         // set company domain name
  34.         $request->attributes->set('agency_connect', ['id'=>$company->getId(),'logo'=>$company->getLogoName()]);    
  35.      }
  36.     //end
  37.     
  38.     if ($this->tokenStorage->getToken()) {
  39.       $user $this->tokenStorage->getToken()->getUser();
  40.       if ($user instanceof UserInterface) {
  41.           if (!$this->entityManagerInterface->isOpen()) {
  42.                 $this->entityManagerInterface $this->entityManagerInterface->create(
  43.                 $this->entityManagerInterface->getConnection(),
  44.                 $this->entityManagerInterface->getConfiguration()
  45.             );
  46.          }
  47.           $user->setLastActivityAt(new \DateTimeImmutable());
  48.           $user->setAutorizationDateAt(new \DateTimeImmutable());
  49.           $this->entityManagerInterface->flush();
  50.       
  51.       }
  52.       if(in_array('ROLE_AUTHOR'$user->getRoles()) ||  in_array('ROLE_EDITOR'$user->getRoles())){
  53.              $response = new RedirectResponse("{$this->parameter->get('front_website_url')}/wp-admin");
  54.              $requestEvent->setResponse($response);
  55.       }
  56.     }
  57.   }
  58. }