<?php
namespace App\ViewManager\Landing;
use App\Application\Service\Menu\ShopMenuService;
use App\Entity\System\Cms;
use App\Entity\System\CmsLanguage;
use App\Entity\System\Language;
use App\Helper\StringHelper;
use App\Manager\System\MenuItemManager;
use App\Manager\System\LanguageManager;
use App\Service\FileStorageService;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class LandingService
{
public const LANDING_STATIC_DIR = 'landing-static';
public const DEFAULT_ISO_CODE = 'en';
public const TEMPLATE_BASE = 'base_landing.html.twig';
public const TEMPLATE_BASE_LOGIN = 'base_login_landing.html.twig';
public const TEMPLATES_BASE_PATH = 'front/landings/';
private ParameterBagInterface $parameterBag;
private RequestStack $requestStack;
private MenuItemManager $menuItemManager;
private ShopMenuService $shopMenuService;
private LanguageManager $languageManager;
private FileStorageService $fileStorageService;
private CacheItemPoolInterface $cacheItemPool;
public function __construct(
ParameterBagInterface $parameterBag,
RequestStack $requestStack,
ShopMenuService $shopMenuService,
LanguageManager $languageManager,
MenuItemManager $menuItemManager,
FileStorageService $fileStorageService,
CacheItemPoolInterface $cacheItemPool
) {
$this->parameterBag = $parameterBag;
$this->requestStack = $requestStack;
$this->shopMenuService = $shopMenuService;
$this->languageManager = $languageManager;
$this->menuItemManager = $menuItemManager;
$this->fileStorageService = $fileStorageService;
$this->cacheItemPool = $cacheItemPool;
}
/**
* @param string $isoCode
*
* @return string
*/
public function setDefaultLocale(string $isoCode)
{
$languages = ['es', 'en', 'it', 'fr', 'de'];
if (!in_array($isoCode, $languages)) {
return self::DEFAULT_ISO_CODE;
}
return $isoCode;
}
/**
* @deprecated to be removed, landing in database
*/
public function getTemplate(string $landingName): string
{
$templateName = str_replace('-', '_', $landingName);
return self::TEMPLATES_BASE_PATH.$templateName.'.html.twig';
}
private function getLandingDir(string $landingName): string
{
$projectDir = $this->parameterBag->get('kernel.project_dir');
return $projectDir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.self::LANDING_STATIC_DIR.DIRECTORY_SEPARATOR.$landingName;
}
public function getLandingBaseTemplate(CmsLanguage $cmsLanguage): string
{
if ($cmsLanguage->getCms()->getId() === Cms::ID_CMS_DROPSHIPPING_MARKETPLACES || $cmsLanguage->getCms()->getId() === Cms::ID_CMS_DROPSHIPPING_ECOMMERCE) {
return LandingService::TEMPLATES_BASE_PATH.LandingService::TEMPLATE_BASE_LOGIN;
}
return LandingService::TEMPLATES_BASE_PATH.LandingService::TEMPLATE_BASE;
}
public function getParametersForBaseCms(CmsLanguage $cmsLanguage, bool $update = false): array
{
if ($cmsLanguage->getContent()) {
$content = $this->getLandingContentExternal($cmsLanguage->getContent(), $update);
} else {
$content = $this->getLandingContentInternal($cmsLanguage->getCms()->getTpl(), $update);
}
return [
'landingHtml' => $content,
'cmsLanguage' => $cmsLanguage,
'languages' => $this->getCmsHrefLanguages(),
'existsMenu' => $this->menuItemManager->existsCmsInMenu($cmsLanguage->getCms()->getId()),
];
}
public function getCmsHrefLanguages(): array
{
$languages = $this->languageManager->findAllActiveNamesAndIsoCodes();
return $this->shopMenuService->getLanguagesCmsController($languages);
}
public function getLandingContentInternal(string $landingName, bool $update = false): string
{
$isoCode = $this->requestStack->getSession()->get('lang');
$lang = ($isoCode === Language::SPANISH_ISO) ? 'index' : $isoCode;
$landingFile = $lang.'.html';
$landingContent = @file_get_contents($this->getLandingDir($landingName).DIRECTORY_SEPARATOR.$landingFile);
if (!$landingContent || $update) {
$this->fileStorageService->updateLanding($landingName, $this->getLandingDir($landingName));
$landingContent = file_get_contents($this->getLandingDir($landingName).DIRECTORY_SEPARATOR.$landingFile);
}
return $landingContent;
}
private function getLandingContentExternal(string $landingUrl, bool $update = false): string
{
$isoCode = $this->requestStack->getSession()->get('lang');
$landingContentItem = $this->cacheItemPool->getItem(StringHelper::slugify($landingUrl));
if (!$landingContentItem->get() || $update) {
$landingContent = file_get_contents($landingUrl.'?lang='.$isoCode);
$landingContentItem->set($landingContent);
$landingContentItem->expiresAfter(604800);
$this->cacheItemPool->save($landingContentItem);
}
return $landingContentItem->get();
}
}