<?php
namespace Slivki\Controller;
use Slivki\Repository\InfoPage\InfoPageRepositoryInterface;
use Slivki\Repository\Seo\SeoRepositoryInterface;
use Slivki\Services\DeviceTypeService;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Slivki\Entity\InfoPage;
use function str_replace;
class PageController extends SiteController
{
private const CONTACTS_PHONE_VERSION_SESSION_KEY = 'contactsPhoneNumbersVersion';
private const CONTACTS_PHONE_VERSION_MAX = 2;
private DeviceTypeService $deviceTypeService;
private SeoRepositoryInterface $seoRepository;
private InfoPageRepositoryInterface $infoPageRepository;
public function __construct(
KernelInterface $kernel,
DeviceTypeService $deviceTypeService,
SeoRepositoryInterface $seoRepository,
InfoPageRepositoryInterface $infoPageRepository
) {
parent::__construct($kernel);
$this->deviceTypeService = $deviceTypeService;
$this->seoRepository = $seoRepository;
$this->infoPageRepository = $infoPageRepository;
}
public function detailsAction(Request $request): Response
{
$isMobile = $this->deviceTypeService->isMobileDevice($request);
$seoEntityId = $this->seoRepository
->getInfoPageByAlias($request->getPathInfo())
->getEntityID();
$infoPage = $this->infoPageRepository->getById($seoEntityId);
$data = [
'infoPage' => $infoPage,
'text' => $this->getPageContent($infoPage, $isMobile, $request),
'director' => $infoPage->getDirector(),
];
$template = $isMobile
? 'Slivki/mobile/info_pages/base.html.twig'
: 'Slivki/pages/pages.html.twig';
return $this->render($template, $data);
}
private function getPageContent(InfoPage $infoPage, bool $isMobile, Request $request): string
{
$content = $isMobile && $infoPage->getMobileContent()
? $infoPage->getMobileContent()
: $infoPage->getContent();
if ($infoPage->getId() === InfoPage::CONTACT_PAGE_ID) {
$content = $this->processContactsPageContent($content, $isMobile, $request);
}
return $content;
}
private function processContactsPageContent(string $content, bool $isMobile, Request $request): string
{
$user = $this->getUser();
$content = str_replace('[user_email]', $user ? $user->getEmail() : '', $content);
if (!$isMobile) {
return $content;
}
$session = $request->getSession();
$version = ($session->get(self::CONTACTS_PHONE_VERSION_SESSION_KEY, 0) % self::CONTACTS_PHONE_VERSION_MAX) + 1;
$session->set(self::CONTACTS_PHONE_VERSION_SESSION_KEY, $version);
return $this->renderView(
'Slivki/mobile/info_pages/contacts.html.twig',
['contactsPhoneNumbersVersion' => $version]
);
}
}