src/EventListener/TwigGlobalsListener.php line 111

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.     private MinimumPurchaseAmountService $minimumPurchaseAmountService;
  27.     public function __construct(
  28.         Environment $twigEnv,
  29.         RequestStack $requestStack,
  30.         CustomerManager $customerManager,
  31.         EnvironmentService $environmentService,
  32.         CatalogManager $catalogManager,
  33.         SecurityService $securityService,
  34.         SessionService $sessionService,
  35.         CustomerGroupService $customerGroupService,
  36.         MinimumPurchaseAmountService $minimumPurchaseAmountService
  37.     ) {
  38.         $this->twigEnv $twigEnv;
  39.         $this->requestStack $requestStack;
  40.         $this->customerManager $customerManager;
  41.         $this->environmentService $environmentService;
  42.         $this->catalogManager $catalogManager;
  43.         $this->securityService $securityService;
  44.         $this->sessionService $sessionService;
  45.         $this->customerGroupService $customerGroupService;
  46.         $this->minimumPurchaseAmountService $minimumPurchaseAmountService;
  47.     }
  48.     public function onKernelRequest(RequestEvent $event): void
  49.     {
  50.         if ($this->environmentService->isApi()) {
  51.             return;
  52.         }
  53.         $packId null;
  54.         $hasPack false;
  55.         $customerCatalogs = [];
  56.         $showWholesalePrice false;
  57.         $request $event->getRequest();
  58.         $customer $this->securityService->findSessionCustomer();
  59.         $languageIsoCode $this->sessionService->getLocale();
  60.         if ($customer !== null) {
  61.             try {
  62.                 $customerPackInfo $this->customerManager->getCustomerPackInfo($customer->getId(), 4);
  63.                 $hasPack = \array_key_exists('pack_id'$customerPackInfo) && $customerPackInfo['pack_id'] > 0;
  64.                 $showWholesalePrice $this->customerGroupService->customerHasProfitWholesale($customer->getId());
  65.                 if ($hasPack) {
  66.                     $packId $customerPackInfo['pack_id'];
  67.                 }
  68.             } catch (NoResultException|NonUniqueResultException $exception) {
  69.             }
  70.             $customerCatalogs $this->catalogManager->findCatalogNamesByCustomerId($customer->getId());
  71.         }
  72.         $customerId $customer !== null $customer->getId() : null;
  73.         $minimumPurchaseAmount $this->minimumPurchaseAmountService->getByCustomerId($customerId);
  74.         $this->twigEnv->addGlobal('minimum_purchase_amount'$minimumPurchaseAmount);
  75.         $customerEmail null;
  76.         $customerId null;
  77.         if ($customer) {
  78.             $customerId $customer->getId();
  79.             $customerEmail $customer->getEmail();
  80.         }
  81.         if ($customer !== null && !$languageIsoCode) {
  82.             $languageIsoCode $customer->getLanguage()->getIsoCode();
  83.         }
  84.         // TODO si es ajax no pasamos
  85.         if ($this->requestStack->getSession()->get('ajax')) {
  86.             return;
  87.         }
  88.         $bbNotificationDialog $request->cookies->get('bb-notification-dialog');
  89.         if ($bbNotificationDialog === null) {
  90.             $request->cookies->set('bb-notification-dialog''default');
  91.         }
  92.         $dataLayerUserData = [
  93.             'userId' => $this->requestStack->getCurrentRequest()->cookies->get(CustomerStatsService::COOKIE_ID_CUSTOMER_STASH),
  94.             'pageName' => $this->requestStack->getSession()->get('url'),
  95.         ];
  96.         $posthogData = [
  97.             'customerId' => $customerId,
  98.             'email' => $customerEmail,
  99.             'hasPack' => $hasPack,
  100.         ];
  101.         $empathyData = [
  102.             'customerId' => $customerId,
  103.             'email' => $customerEmail,
  104.             'hasPack' => $hasPack,
  105.             'isWholesalePrice' => $showWholesalePrice,
  106.             'catalogs' => $customerCatalogs,
  107.             'languageIsoCode' => $languageIsoCode,
  108.         ];
  109.         $this->twigEnv->addGlobal('dataLayerUserData'$dataLayerUserData);
  110.         $this->twigEnv->addGlobal('posthogData'$posthogData);
  111.         $this->twigEnv->addGlobal('empathyData'$empathyData);
  112.     }
  113. }