src/ViewManager/Landing/LandingService.php line 116

Open in your IDE?
  1. <?php
  2. namespace App\ViewManager\Landing;
  3. use App\Application\Service\Menu\ShopMenuService;
  4. use App\Application\Service\Session\SessionService;
  5. use App\Entity\System\Cms;
  6. use App\Entity\System\CmsLanguage;
  7. use App\Entity\System\Language;
  8. use App\Helper\StringHelper;
  9. use App\Manager\System\MenuItemManager;
  10. use App\Manager\System\LanguageManager;
  11. use App\Service\FileStorageService;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class LandingService
  16. {
  17.     public const LANDING_STATIC_DIR 'landing-static';
  18.     public const DEFAULT_ISO_CODE 'en';
  19.     public const TEMPLATE_BASE 'base_landing.html.twig';
  20.     public const TEMPLATE_BASE_LOGIN 'base_login_landing.html.twig';
  21.     public const TEMPLATES_BASE_PATH 'front/landings/';
  22.     private ParameterBagInterface $parameterBag;
  23.     private RequestStack $requestStack;
  24.     private MenuItemManager $menuItemManager;
  25.     private ShopMenuService $shopMenuService;
  26.     private LanguageManager $languageManager;
  27.     private FileStorageService $fileStorageService;
  28.     private CacheItemPoolInterface $cacheItemPool;
  29.     private SessionService $sessionService;
  30.     public function __construct(
  31.         ParameterBagInterface $parameterBag,
  32.         RequestStack $requestStack,
  33.         ShopMenuService $shopMenuService,
  34.         LanguageManager $languageManager,
  35.         MenuItemManager $menuItemManager,
  36.         FileStorageService $fileStorageService,
  37.         CacheItemPoolInterface $cacheItemPool,
  38.         SessionService $sessionService
  39.     ) {
  40.         $this->parameterBag $parameterBag;
  41.         $this->requestStack $requestStack;
  42.         $this->shopMenuService $shopMenuService;
  43.         $this->languageManager $languageManager;
  44.         $this->menuItemManager $menuItemManager;
  45.         $this->fileStorageService $fileStorageService;
  46.         $this->cacheItemPool $cacheItemPool;
  47.         $this->sessionService $sessionService;
  48.     }
  49.     public function setDefaultLocale(string $isoCode): string
  50.     {
  51.         $languages = ['es''en''it''fr''de'];
  52.         if (!in_array($isoCode$languages)) {
  53.             return self::DEFAULT_ISO_CODE;
  54.         }
  55.         return $isoCode;
  56.     }
  57.     /**
  58.      * @deprecated to be removed, landing in database
  59.      */
  60.     public function getTemplate(string $landingName): string
  61.     {
  62.         $templateName str_replace('-''_'$landingName);
  63.         return self::TEMPLATES_BASE_PATH.$templateName.'.html.twig';
  64.     }
  65.     private function getLandingDir(string $landingName): string
  66.     {
  67.         $projectDir $this->parameterBag->get('kernel.project_dir');
  68.         return $projectDir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.self::LANDING_STATIC_DIR.DIRECTORY_SEPARATOR.$landingName;
  69.     }
  70.     public function getLandingBaseTemplate(CmsLanguage $cmsLanguage): string
  71.     {
  72.         if ($cmsLanguage->getCms()->getId() === Cms::ID_CMS_DROPSHIPPING_MARKETPLACES || $cmsLanguage->getCms()->getId() === Cms::ID_CMS_DROPSHIPPING_ECOMMERCE) {
  73.             return LandingService::TEMPLATES_BASE_PATH.LandingService::TEMPLATE_BASE_LOGIN;
  74.         }
  75.         return LandingService::TEMPLATES_BASE_PATH.LandingService::TEMPLATE_BASE;
  76.     }
  77.     public function getParametersForBaseCms(CmsLanguage $cmsLanguage): array
  78.     {
  79.         if ($cmsLanguage->getContent()) {
  80.             $content $this->getLandingContentExternal($cmsLanguage->getContent());
  81.         } else {
  82.             $content $this->getLandingContentInternal($cmsLanguage->getCms()->getTpl());
  83.         }
  84.         return [
  85.             'landingHtml' => $content,
  86.             'cmsLanguage' => $cmsLanguage,
  87.             'languages' => $this->getCmsHrefLanguages(),
  88.             'existsMenu' => $this->menuItemManager->existsCmsInMenu($cmsLanguage->getCms()->getId()),
  89.         ];
  90.     }
  91.     public function getCmsHrefLanguages(): array
  92.     {
  93.         $languages $this->languageManager->findAllActiveNamesAndIsoCodes();
  94.         return $this->shopMenuService->getLanguagesCmsController($languages);
  95.     }
  96.     public function getLandingContentInternal(string $landingName): string
  97.     {
  98.         $update = (bool)$this->requestStack->getSession()->get('update'false);
  99.         $isoCode $this->requestStack->getSession()->get('lang');
  100.         $lang = ($isoCode === Language::SPANISH_ISO) ? 'index' $isoCode;
  101.         $landingFile $lang.'.html';
  102.         $landingContent = @file_get_contents($this->getLandingDir($landingName).DIRECTORY_SEPARATOR.$landingFile);
  103.         if (!$landingContent || $update) {
  104.             $this->fileStorageService->updateLanding($landingName$this->getLandingDir($landingName));
  105.             $landingContent file_get_contents($this->getLandingDir($landingName).DIRECTORY_SEPARATOR.$landingFile);
  106.         }
  107.         return $landingContent;
  108.     }
  109.     private function getLandingContentExternal(string $landingUrl): string
  110.     {
  111.         $landingContentItem $this->cacheItemPool->getItem(StringHelper::slugify($landingUrl));
  112.         $update = (bool)$this->requestStack->getSession()->get('update'false);
  113.         if (!$landingContentItem->get() || $update) {
  114.             $landingContent file_get_contents($landingUrl.'?lang='.$this->sessionService->getLocale());
  115.             $landingContentItem->set($landingContent);
  116.             $landingContentItem->expiresAfter(604800);
  117.             $this->cacheItemPool->save($landingContentItem);
  118.         }
  119.         return $landingContentItem->get();
  120.     }
  121. }