<?php
namespace App\Service;
use App\Application\Service\Customer\CustomerStatsService;
use App\Application\Service\Helper\LinkGenerator;
use App\Application\Service\Session\SessionService;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
class LegacyControllerService
{
private CustomerStatsService $customerStatsService;
private SessionService $sessionService;
private Environment $twig;
private LinkGenerator $linkGenerator;
public function __construct(
CustomerStatsService $customerStatsService,
SessionService $sessionService,
Environment $twig,
LinkGenerator $linkGenerator
) {
$this->customerStatsService = $customerStatsService;
$this->sessionService = $sessionService;
$this->twig = $twig;
$this->linkGenerator = $linkGenerator;
}
public function getHtmlFromLegacyController(\Controller $legacyController): string
{
$headData = $legacyController->head();
if (null === $headData || $legacyController->getDisplay_only_view()) {
$html = $legacyController->display();
return new Response($html);
}
$customerStats = $this->customerStatsService->getCustomerStatsId();
$userId = $customerStats ? $customerStats->getId() : null;
$currentUrl = $this->getCurrentUrl($headData);
$renderedTemplateData = [
'bodyHtml' => $legacyController->display(true),
'headHtml' => $this->twig->render('front/base/legacy_head.html.twig', [
'data' => $headData,
'current_url' => $currentUrl,
'dataLayerUserData' => [
'userId' => $userId,
'pageName' => $this->sessionService->get('url'),
],
]),
'shopMenuHtml' => $legacyController->isShopMenu() ? $legacyController->mainNav() : null,
'footerJs' => $legacyController->prepareJs(true),
];
$renderedTemplate = $this->twig->render(
'front/base/base_'.($legacyController->isShopMenu() ? 'shop' : 'corporate').'_legacy.html.twig',
$renderedTemplateData
);
$this->sessionService->deleteFlashData();
return $renderedTemplate;
}
public function getCurrentUrl(\Partial $headData): string
{
$currentUrl = '';
if (!empty($headData->current_url_fb)) {
return $headData->current_url_fb;
}
if (!empty($headData->current_url)) {
return $this->linkGenerator->getCurrentLink(
$headData->current_url,
$this->sessionService->getLocaleId()
);
}
return $currentUrl;
}
}