src/EventListener/TwigGlobalsListener.php line 109

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