src/Controller/PageController.php line 36

Open in your IDE?
  1. <?php
  2. namespace Slivki\Controller;
  3. use Slivki\Repository\InfoPage\InfoPageRepositoryInterface;
  4. use Slivki\Repository\Seo\SeoRepositoryInterface;
  5. use Slivki\Services\DeviceTypeService;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\KernelInterface;
  9. use Slivki\Entity\InfoPage;
  10. use function str_replace;
  11. class PageController extends SiteController
  12. {
  13.     private const CONTACTS_PHONE_VERSION_SESSION_KEY 'contactsPhoneNumbersVersion';
  14.     private const CONTACTS_PHONE_VERSION_MAX 2;
  15.     private DeviceTypeService $deviceTypeService;
  16.     private SeoRepositoryInterface $seoRepository;
  17.     private InfoPageRepositoryInterface $infoPageRepository;
  18.     public function __construct(
  19.         KernelInterface $kernel,
  20.         DeviceTypeService $deviceTypeService,
  21.         SeoRepositoryInterface $seoRepository,
  22.         InfoPageRepositoryInterface $infoPageRepository
  23.     ) {
  24.         parent::__construct($kernel);
  25.         $this->deviceTypeService $deviceTypeService;
  26.         $this->seoRepository $seoRepository;
  27.         $this->infoPageRepository $infoPageRepository;
  28.     }
  29.     public function detailsAction(Request $request): Response
  30.     {
  31.         $isMobile $this->deviceTypeService->isMobileDevice($request);
  32.         $seoEntityId $this->seoRepository
  33.             ->getInfoPageByAlias($request->getPathInfo())
  34.             ->getEntityID();
  35.         $infoPage $this->infoPageRepository->getById($seoEntityId);
  36.         $data = [
  37.             'infoPage' => $infoPage,
  38.             'text' => $this->getPageContent($infoPage$isMobile$request),
  39.             'director' => $infoPage->getDirector(),
  40.         ];
  41.         $template $isMobile
  42.             'Slivki/mobile/info_pages/base.html.twig'
  43.             'Slivki/pages/pages.html.twig';
  44.         return $this->render($template$data);
  45.     }
  46.     private function getPageContent(InfoPage $infoPagebool $isMobileRequest $request): string
  47.     {
  48.         $content $isMobile && $infoPage->getMobileContent()
  49.             ? $infoPage->getMobileContent()
  50.             : $infoPage->getContent();
  51.         if ($infoPage->getId() === InfoPage::CONTACT_PAGE_ID) {
  52.             $content $this->processContactsPageContent($content$isMobile$request);
  53.         }
  54.         return $content;
  55.     }
  56.     private function processContactsPageContent(string $contentbool $isMobileRequest $request): string
  57.     {
  58.         $user $this->getUser();
  59.         $content str_replace('[user_email]'$user $user->getEmail() : ''$content);
  60.         if (!$isMobile) {
  61.             return $content;
  62.         }
  63.         $session $request->getSession();
  64.         $version = ($session->get(self::CONTACTS_PHONE_VERSION_SESSION_KEY0) % self::CONTACTS_PHONE_VERSION_MAX) + 1;
  65.         $session->set(self::CONTACTS_PHONE_VERSION_SESSION_KEY$version);
  66.         return $this->renderView(
  67.             'Slivki/mobile/info_pages/contacts.html.twig',
  68.             ['contactsPhoneNumbersVersion' => $version]
  69.         );
  70.     }
  71. }