src/ViewManager/Landing/LandingService.php line 117

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