src/EventListener/TwigGlobalsListener.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Application\Service\Customer\CustomerGroupService;
  4. use App\Application\Service\Session\SessionService;
  5. use App\Manager\System\CatalogManager;
  6. use App\Service\EnvironmentService;
  7. use App\Service\MinimumPurchaseAmountService;
  8. use App\Service\SecurityService;
  9. use Doctrine\ORM\NonUniqueResultException;
  10. use Doctrine\ORM\NoResultException;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Twig\Environment;
  14. use App\Application\Service\Customer\CustomerStatsService;
  15. use App\Manager\System\CustomerManager;
  16. class TwigGlobalsListener
  17. {
  18.     private Environment $twigEnv;
  19.     private RequestStack $requestStack;
  20.     private CustomerManager $customerManager;
  21.     private EnvironmentService $environmentService;
  22.     private CatalogManager $catalogManager;
  23.     private SecurityService $securityService;
  24.     private SessionService $sessionService;
  25.     private CustomerGroupService $customerGroupService;
  26.     public function __construct(
  27.         Environment $twigEnv,
  28.         RequestStack $requestStack,
  29.         CustomerManager $customerManager,
  30.         EnvironmentService $environmentService,
  31.         CatalogManager $catalogManager,
  32.         SecurityService $securityService,
  33.         SessionService $sessionService,
  34.         CustomerGroupService $customerGroupService
  35.     ) {
  36.         $this->twigEnv $twigEnv;
  37.         $this->requestStack $requestStack;
  38.         $this->customerManager $customerManager;
  39.         $this->environmentService $environmentService;
  40.         $this->catalogManager $catalogManager;
  41.         $this->securityService $securityService;
  42.         $this->sessionService $sessionService;
  43.         $this->customerGroupService $customerGroupService;
  44.     }
  45.     public function onKernelRequest(RequestEvent $event): void
  46.     {
  47.         if ($this->environmentService->isApi()) {
  48.             return;
  49.         }
  50.         $packId null;
  51.         $hasPack false;
  52.         $customerCatalogs = [];
  53.         $showWholesalePrice false;
  54.         $request $event->getRequest();
  55.         $customer $this->securityService->findSessionCustomer();
  56.         $languageIsoCode $this->sessionService->getLocale();
  57.         if ($customer !== null) {
  58.             try {
  59.                 $customerPackInfo $this->customerManager->getCustomerPackInfo($customer->getId(), 4);
  60.                 $hasPack = \array_key_exists('pack_id'$customerPackInfo) && $customerPackInfo['pack_id'] > 0;
  61.                 $showWholesalePrice $this->customerGroupService->customerHasProfitWholesale($customer->getId());
  62.                 if ($hasPack) {
  63.                     $packId $customerPackInfo['pack_id'];
  64.                 }
  65.             } catch (NoResultException|NonUniqueResultException $exception) {
  66.             }
  67.             $customerCatalogs $this->catalogManager->findCatalogNamesByCustomerId($customer->getId());
  68.         }
  69.         $minimumPurchaseAmount MinimumPurchaseAmountService::getByPackId($packId);
  70.         $this->twigEnv->addGlobal('minimum_purchase_amount'$minimumPurchaseAmount);
  71.         $customerEmail null;
  72.         $customerId null;
  73.         if ($customer) {
  74.             $customerId $customer->getId();
  75.             $customerEmail $customer->getEmail();
  76.         }
  77.         if ($customer !== null && !$languageIsoCode) {
  78.             $languageIsoCode $customer->getLanguage()->getIsoCode();
  79.         }
  80.         // TODO si es ajax no pasamos
  81.         if ($this->requestStack->getSession()->get('ajax')) {
  82.             return;
  83.         }
  84.         $bbNotificationDialog $request->cookies->get('bb-notification-dialog');
  85.         if ($bbNotificationDialog === null) {
  86.             $request->cookies->set('bb-notification-dialog''default');
  87.         }
  88.         $dataLayerUserData = [
  89.             'userId' => $this->requestStack->getCurrentRequest()->cookies->get(CustomerStatsService::COOKIE_ID_CUSTOMER_STASH),
  90.             'pageName' => $this->requestStack->getSession()->get('url'),
  91.         ];
  92.         $posthogData = [
  93.             'customerId' => $customerId,
  94.             'email' => $customerEmail,
  95.             'hasPack' => $hasPack,
  96.         ];
  97.         $empathyData = [
  98.             'customerId' => $customerId,
  99.             'email' => $customerEmail,
  100.             'hasPack' => $hasPack,
  101.             'isWholesalePrice' => $showWholesalePrice,
  102.             'catalogs' => $customerCatalogs,
  103.             'languageIsoCode' => $languageIsoCode,
  104.         ];
  105.         $this->twigEnv->addGlobal('dataLayerUserData'$dataLayerUserData);
  106.         $this->twigEnv->addGlobal('posthogData'$posthogData);
  107.         $this->twigEnv->addGlobal('empathyData'$empathyData);
  108.     }
  109. }