src/EventListener/TwigGlobalsListener.php line 38

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