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.                 $event->getResponse()
  49.             );
  50.         } catch (\Exception $exception) {
  51.         }
  52.         // Only display the logo alert if user is logged in and he is a broker
  53.         $notifyLogo false;
  54.         $notifyPersonalPhoto false;
  55.         $notifyExpiry false;
  56.         $notifyPersonalIsNotCompleted false;
  57.         $routeName = (string) $event->getRequest()->get('_route''');
  58.         if ($securityToken && !strpos($routeName'api_') && !strpos($routeName'admin_')) {
  59.             /** @var User $user */
  60.             $user $this->security->getUser();
  61.             if ($user instanceof User && UserTypes::INDIVIDUAL != $user->getUserType()) {
  62.                 if (!$user->getLogo()) {
  63.                     $notifyLogo true;
  64.                 }
  65.                 if (!$user->getPersonalPhoto()) {
  66.                     $notifyPersonalPhoto true;
  67.                 }
  68.                 if (=== $user->getAbsoluteCreditExpiryDays()) {
  69.                     $notifyExpiry true;
  70.                 }
  71.             }
  72.             if ($user instanceof User && UserTypes::INDIVIDUAL === $user->getUserType()) {
  73.                 /** @var PersonalInfo $personalInfo */
  74.                 $personalInfo $user->getActivePersonalInfo();
  75.                 $notifyPersonalIsNotCompleted $personalInfo ? !$personalInfo->isDataCompleted() : true;
  76.             }
  77.         }
  78.         $this->twig_Environment->addGlobal('notifyLogo'$notifyLogo);
  79.         $this->twig_Environment->addGlobal('notifyPersonalPhoto'$notifyPersonalPhoto);
  80.         $this->twig_Environment->addGlobal('notifyExpiry'$notifyExpiry);
  81.         $this->twig_Environment->addGlobal('notifyPersonalIsNotCompleted'$notifyPersonalIsNotCompleted);
  82.         $browser = new Browser();
  83.         $event->getRequest()->attributes->set('_browser', [
  84.             'browser' => $browser->getBrowser(),
  85.             'version' => $browser->getVersion(),
  86.             'platform' => $browser->getPlatform(),
  87.         ]);
  88.     }
  89.     /**
  90.      * @return array<string, mixed>
  91.      */
  92.     public static function getSubscribedEvents(): array
  93.     {
  94.         return [KernelEvents::REQUEST => 'onKernelRequest'];
  95.     }
  96. }