<?php
namespace App\Controller\Page;
use App\Component\Configuration\Util\Config;
use App\Entity\Company;
use App\Entity\Vacancy;
use App\Renderer\Page;
use App\Service\SiteService;
use App\Service\VacancyService;
use Doctrine\ORM\EntityManagerInterface;
use FOS\HttpCacheBundle\Configuration\Tag;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class VacanciesController extends AbstractController
{
private RequestStack $requestStack;
private Page $pageRenderer;
private VacancyService $service;
private Config $config;
private SiteService $siteService;
private EntityManagerInterface $entityManager;
public function __construct(
RequestStack $requestStack,
Page $pageRenderer,
VacancyService $service,
Config $config,
SiteService $siteService,
EntityManagerInterface $entityManager
) {
$this->requestStack = $requestStack;
$this->pageRenderer = $pageRenderer;
$this->service = $service;
$this->config = $config;
$this->siteService = $siteService;
$this->entityManager = $entityManager;
}
/**
* @Tag({"pages", "vacancy_overview"})
*
* @param null $filters
*
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function indexAction(
$filters = null
): Response {
$company = null;
$range = 10;
if ($filters) {
$companyCandidate = $filters;
if (str_contains($companyCandidate, '/')) {
$urlFilters = explode('/', $companyCandidate);
$companyCandidate = reset($urlFilters);
$range = $this->findRange($urlFilters);
}
$company = $this->entityManager->getRepository(Company::class)->findOneBy([
'slug' => $companyCandidate,
]);
}
$map = null !== $this->requestStack->getMainRequest()?->get('map');
$seoSnippet = $this->getSeoSnippet();
return $this
->pageRenderer
->renderPage(
placeholder: '{vacancies}',
template: '@default/pages/vacancy_overview.html.twig',
data: [
'map' => $map,
'limitPerPage' => $this->config->get('site_vacancy_limit_per_page'),
'sortForm' => $this->service->getSortingForm()->createView(),
'company' => $company,
'range' => $range,
// this is needed for bc when theme has no theme inheritance
'e_commerce_script' => '<script> window.dataLayer = window.dataLayer || []; </script>',
'seoSnippet' => $seoSnippet,
],
metaData: $seoSnippet,
options: [
'skip_tealium' => true,
]
)
;
}
private function findRange(array $urlFilters): int
{
$range = 10;
if (!$rangeKey = array_search('range', $urlFilters, true)) {
return $range;
}
/* @var int $rangeKey */
++$rangeKey;
if (!empty($urlFilters[$rangeKey]) && \is_int($urlFilters[$rangeKey])) {
$range = $urlFilters[$rangeKey];
}
return $range;
}
/**
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function favoritesOverviewAction(): Response
{
return $this
->pageRenderer
->renderPage('{favorites}', '@default/pages/vacancy_favorites.html.twig', []);
}
private function getSeoSnippet(): array
{
/** @var Request $request */
$request = $this->requestStack->getCurrentRequest() ?? $this->requestStack->getMainRequest();
$filters = $this->service->fetchFiltersFromUrl($request);
return $this->service->getSeoSnippetsFromFilters($filters, $this->siteService->getSite());
}
}