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

  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 Ikimea\Browser\Browser;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Twig\Environment;
  12. class RequestEventSubscriber implements EventSubscriberInterface
  13. {
  14. public function __construct(private readonly UserManager $userManager, private readonly Environment $twig_Environment, private readonly \Symfony\Bundle\SecurityBundle\Security $security)
  15. {
  16. }
  17. public function onKernelRequest(RequestEvent $event): void
  18. {
  19. $securityToken = $this->security->getToken();
  20. if ($event->getRequest()->query->get('_locale')
  21. && \in_array($event->getRequest()->get('_route'), ['listing_slug', 'listing_details', 'listing_view_notification', 'listing_view'])) {
  22. $event->getRequest()->query->remove('_locale');
  23. }
  24. try {
  25. $this->userManager->authenticateWithToken(
  26. $event->getRequest()->get('access_token', $event->getRequest()->get('oauth_access_token')),
  27. $event->getResponse()
  28. );
  29. } catch (\Exception) {
  30. }
  31. // Only display the logo alert if user is logged in and he is a broker
  32. $notifyLogo = false;
  33. $notifyPersonalPhoto = false;
  34. $notifyExpiry = false;
  35. $notifyPersonalIsNotCompleted = false;
  36. $routeName = (string) $event->getRequest()->get('_route', '');
  37. if ($securityToken && !strpos($routeName, 'api_') && !strpos($routeName, 'admin_')) {
  38. /** @var User $user */
  39. $user = $this->security->getUser();
  40. if ($user instanceof User && UserTypes::INDIVIDUAL != $user->getUserType()) {
  41. if (!$user->getLogo()) {
  42. $notifyLogo = true;
  43. }
  44. if (!$user->getPersonalPhoto()) {
  45. $notifyPersonalPhoto = true;
  46. }
  47. if (0 === $user->getAbsoluteCreditExpiryDays()) {
  48. $notifyExpiry = true;
  49. }
  50. }
  51. if ($user instanceof User && UserTypes::INDIVIDUAL === $user->getUserType()) {
  52. /** @var PersonalInfo $personalInfo */
  53. $personalInfo = $user->getActivePersonalInfo();
  54. $notifyPersonalIsNotCompleted = $personalInfo ? !$personalInfo->isDataCompleted() : true;
  55. }
  56. }
  57. $this->twig_Environment->addGlobal('notifyLogo', $notifyLogo);
  58. $this->twig_Environment->addGlobal('notifyPersonalPhoto', $notifyPersonalPhoto);
  59. $this->twig_Environment->addGlobal('notifyExpiry', $notifyExpiry);
  60. $this->twig_Environment->addGlobal('notifyPersonalIsNotCompleted', $notifyPersonalIsNotCompleted);
  61. $browser = new Browser();
  62. $event->getRequest()->attributes->set('_browser', [
  63. 'browser' => $browser->getBrowser(),
  64. 'version' => $browser->getVersion(),
  65. 'platform' => $browser->getPlatform(),
  66. ]);
  67. }
  68. /**
  69. * @return array<string, mixed>
  70. */
  71. public static function getSubscribedEvents(): array
  72. {
  73. return [KernelEvents::REQUEST => 'onKernelRequest'];
  74. }
  75. }