<?php
namespace App\EventListener;
use App\Manager\System\CatalogManager;
use App\Service\EnvironmentService;
use App\Service\MinimumPurchaseAmountService;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Environment;
use App\Application\Service\Customer\CustomerStatsService;
use App\Manager\System\CustomerManager;
class TwigGlobalsListener
{
private Environment $twigEnv;
private RequestStack $requestStack;
private CustomerManager $customerManager;
private EnvironmentService $environmentService;
private CatalogManager $catalogManager;
public function __construct(
Environment $twigEnv,
RequestStack $requestStack,
CustomerManager $customerManager,
EnvironmentService $environmentService,
CatalogManager $catalogManager
) {
$this->twigEnv = $twigEnv;
$this->requestStack = $requestStack;
$this->customerManager = $customerManager;
$this->environmentService = $environmentService;
$this->catalogManager = $catalogManager;
}
public function onKernelRequest(RequestEvent $event): void
{
if ($this->environmentService->isApi()) {
return;
}
$packId = null;
$hasPack = false;
$customerCatalogs = [];
$showWholesalePrice = false;
$request = $event->getRequest();
$customerId = $this->requestStack->getSession()->get('id_customer');
$languageIsoCode = $this->requestStack->getSession()->get('lang');
if ($customerId !== null) {
try {
$customerPackInfo = $this->customerManager->getCustomerPackInfo($customerId, 4);
$hasPack = \array_key_exists('packId', $customerPackInfo) && $customerPackInfo['packId'] > 0;
$currentDate = new \DateTime();
$showWholesalePrice = $customerPackInfo['dateCsv'] > $currentDate;
if ($hasPack) {
$packId = $customerPackInfo['packId'];
}
} catch (NoResultException|NonUniqueResultException $exception) {
}
$customerCatalogs = $this->catalogManager->findCatalogNamesByCustomerId($customerId);
}
$minimumPurchaseAmount = MinimumPurchaseAmountService::getByPackId($packId);
$this->twigEnv->addGlobal('minimum_purchase_amount', $minimumPurchaseAmount);
$customerEmail = null;
$customer = null;
if ($customerId) {
$customer = $this->customerManager->findOneById($customerId);
$customerEmail = $customer->getEmail();
}
if ($customer !== null && !$languageIsoCode) {
$languageIsoCode = $customer->getLanguage()->getIsoCode();
}
// TODO si es ajax no pasamos
if ($this->requestStack->getSession()->get('ajax')) {
return;
}
$bbNotificationDialog = $request->cookies->get('bb-notification-dialog');
if ($bbNotificationDialog === null) {
$request->cookies->set('bb-notification-dialog', 'default');
}
$dataLayerUserData = [
'userId' => $this->requestStack->getCurrentRequest()->cookies->get(CustomerStatsService::COOKIE_ID_CUSTOMER_STASH),
'pageName' => $this->requestStack->getSession()->get('url'),
];
$posthogData = [
'customerId' => $customerId,
'email' => $customerEmail,
'hasPack' => $hasPack,
];
$empathyData = [
'customerId' => $customerId,
'email' => $customerEmail,
'hasPack' => $hasPack,
'isWholesalePrice' => $showWholesalePrice,
'catalogs' => $customerCatalogs,
'languageIsoCode' => $languageIsoCode,
];
$this->twigEnv->addGlobal('dataLayerUserData', $dataLayerUserData);
$this->twigEnv->addGlobal('posthogData', $posthogData);
$this->twigEnv->addGlobal('empathyData', $empathyData);
}
}