src/ViewManager/Landing/LandingService.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\ViewManager\Landing;
  3. use App\Entity\System\Language;
  4. use App\Service\Storage\FileStorageService;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. class LandingService
  8. {
  9. public const LANDING_STATIC_DIR = 'landing-static';
  10. public const DEFAULT_ISO_CODE = 'en';
  11. private ParameterBagInterface $parameterBag;
  12. private RequestStack $requestStack;
  13. private FileStorageService $fileStorageService;
  14. public function __construct(
  15. ParameterBagInterface $parameterBag,
  16. RequestStack $requestStack,
  17. FileStorageService $fileStorageService
  18. ) {
  19. $this->parameterBag = $parameterBag;
  20. $this->requestStack = $requestStack;
  21. $this->fileStorageService = $fileStorageService;
  22. }
  23. public function setDefaultLocale(string $isoCode): string
  24. {
  25. $languages = ['es', 'en', 'it', 'fr', 'de'];
  26. if (!in_array($isoCode, $languages)) {
  27. return self::DEFAULT_ISO_CODE;
  28. }
  29. return $isoCode;
  30. }
  31. private function getLandingDir(string $landingName): string
  32. {
  33. $projectDir = $this->parameterBag->get('kernel.project_dir');
  34. return $projectDir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.self::LANDING_STATIC_DIR.DIRECTORY_SEPARATOR.$landingName;
  35. }
  36. public function getLandingContentInternal(string $landingName): string
  37. {
  38. $update = (bool)$this->requestStack->getSession()->get('update', false);
  39. $isoCode = $this->requestStack->getSession()->get('lang');
  40. $lang = ($isoCode === Language::SPANISH_ISO) ? 'index' : $isoCode;
  41. $landingFile = $lang.'.html';
  42. $landingContent = @file_get_contents($this->getLandingDir($landingName).DIRECTORY_SEPARATOR.$landingFile);
  43. if (!$landingContent || $update) {
  44. $this->fileStorageService->updateLanding($landingName, $this->getLandingDir($landingName));
  45. $landingContent = file_get_contents($this->getLandingDir($landingName).DIRECTORY_SEPARATOR.$landingFile);
  46. }
  47. return $landingContent;
  48. }
  49. }