src/Aqarmap/Bundle/MainBundle/Listener/RequestEventSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\Listener;
  3. use Aqarmap\Bundle\UserBundle\Constant\UserTypes;
  4. use Aqarmap\Bundle\UserBundle\Entity\PersonalInfo;
  5. use Aqarmap\Bundle\UserBundle\Entity\User;
  6. use Aqarmap\Bundle\UserBundle\Services\UserManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Ikimea\Browser\Browser;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Twig\Environment;
  16. class RequestEventSubscriber implements EventSubscriberInterface
  17. {
  18.     private Environment $engine;
  19.     private UserManager $userManager;
  20.     private \Twig_Environment $twig_Environment;
  21.     private EntityManagerInterface $entityManager;
  22.     private Security $security;
  23.     public function __construct(
  24.         ContainerInterface $container,
  25.         Environment $engine,
  26.         UserManager $userManager,
  27.         AuthorizationCheckerInterface $authorizationChecker,
  28.         \Twig_Environment $twig_Environment,
  29.         EntityManagerInterface $entityManager,
  30.         Security $security
  31.     ) {
  32.         $this->engine $engine;
  33.         $this->userManager $userManager;
  34.         $this->twig_Environment $twig_Environment;
  35.         $this->entityManager $entityManager;
  36.         $this->security $security;
  37.     }
  38.     public function onKernelRequest(RequestEvent $event): void
  39.     {
  40.         $securityToken $this->security->getToken();
  41.         if ($event->getRequest()->query->get('_locale')
  42.             && \in_array($event->getRequest()->get('_route'), ['listing_slug''listing_details''listing_view_notification''listing_view'])) {
  43.             $event->getRequest()->query->remove('_locale');
  44.         }
  45.         try {
  46.             $this->userManager->authenticateWithToken(
  47.                 $event->getRequest()->get('access_token'$event->getRequest()->get('oauth_access_token'))
  48.             );
  49.         } catch (\Exception $exception) {
  50.         }
  51.         // Only display the logo alert if user is logged in and he is a broker
  52.         $notifyLogo false;
  53.         $notifyPersonalPhoto false;
  54.         $notifyExpiry false;
  55.         $notifyPersonalIsNotCompleted false;
  56.         $routeName = (string) $event->getRequest()->get('_route''');
  57.         if ($securityToken && !strpos($routeName'api_') && !strpos($routeName'admin_')) {
  58.             /** @var User $user */
  59.             $user $this->security->getUser();
  60.             if ($user instanceof User && UserTypes::INDIVIDUAL != $user->getUserType()) {
  61.                 if (!$user->getLogo()) {
  62.                     $notifyLogo true;
  63.                 }
  64.                 if (!$user->getPersonalPhoto()) {
  65.                     $notifyPersonalPhoto true;
  66.                 }
  67.                 if (=== $user->getAbsoluteCreditExpiryDays()) {
  68.                     $notifyExpiry true;
  69.                 }
  70.             }
  71.             if ($user instanceof User && UserTypes::INDIVIDUAL === $user->getUserType()) {
  72.                 /** @var PersonalInfo $personalInfo */
  73.                 $personalInfo $user->getActivePersonalInfo();
  74.                 $notifyPersonalIsNotCompleted $personalInfo ? !$personalInfo->isDataCompleted() : true;
  75.             }
  76.         }
  77.         $this->twig_Environment->addGlobal('notifyLogo'$notifyLogo);
  78.         $this->twig_Environment->addGlobal('notifyPersonalPhoto'$notifyPersonalPhoto);
  79.         $this->twig_Environment->addGlobal('notifyExpiry'$notifyExpiry);
  80.         $this->twig_Environment->addGlobal('notifyPersonalIsNotCompleted'$notifyPersonalIsNotCompleted);
  81.         $browser = new Browser();
  82.         $event->getRequest()->attributes->set('_browser', [
  83.             'browser' => $browser->getBrowser(),
  84.             'version' => $browser->getVersion(),
  85.             'platform' => $browser->getPlatform(),
  86.         ]);
  87.     }
  88.     /**
  89.      * @return array<string, mixed>
  90.      */
  91.     public static function getSubscribedEvents(): array
  92.     {
  93.         return [KernelEvents::REQUEST => 'onKernelRequest'];
  94.     }
  95. }