<?php
namespace App\ViewManager\Landing;
use App\Entity\System\Language;
use App\Service\Storage\FileStorageService;
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';
private ParameterBagInterface $parameterBag;
private RequestStack $requestStack;
private FileStorageService $fileStorageService;
public function __construct(
ParameterBagInterface $parameterBag,
RequestStack $requestStack,
FileStorageService $fileStorageService
) {
$this->parameterBag = $parameterBag;
$this->requestStack = $requestStack;
$this->fileStorageService = $fileStorageService;
}
public function setDefaultLocale(string $isoCode): string
{
$languages = ['es', 'en', 'it', 'fr', 'de'];
if (!in_array($isoCode, $languages)) {
return self::DEFAULT_ISO_CODE;
}
return $isoCode;
}
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 getLandingContentInternal(string $landingName): string
{
$update = (bool)$this->requestStack->getSession()->get('update', false);
$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;
}
}