<?php
namespace App\Controller\Page;
use App\Component\Configuration\Util\Config;
use App\Decorator\VacancyDecorator;
use App\Entity\Company;
use App\Entity\FeedbackCompany;
use App\Entity\Option;
use App\Entity\OptionValue;
use App\Entity\Route as RouteEntity;
use App\Entity\Testimonial;
use App\EventListener\FeatureFlagListener;
use App\Form\Setting\CompanySettingType;
use App\Manager\TagManager;
use App\Renderer\Page as PageRenderer;
use App\Service\CompanyService;
use App\Service\SiteService;
use Flagception\Manager\FeatureManagerInterface;
use FOS\HttpCacheBundle\Configuration\Tag;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class CompanyController extends AbstractController
{
private PageRenderer $pageRenderer;
private TagManager $tagManager;
private FeatureManagerInterface $featureManager;
private CompanyService $service;
private SiteService $siteService;
private Config $config;
public function __construct(
PageRenderer $pageRenderer,
TagManager $tagManager,
FeatureManagerInterface $featureManager,
CompanyService $service,
SiteService $siteService,
Config $config
) {
$this->pageRenderer = $pageRenderer;
$this->tagManager = $tagManager;
$this->featureManager = $featureManager;
$this->service = $service;
$this->siteService = $siteService;
$this->config = $config;
}
/**
* @Tag("vacancies")
* @Tag("company-detail", expression="'company-'~id")
*
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws \Exception
*/
public function detailAction(
Request $request,
VacancyDecorator $vacancyDecorator
): Response {
if (!$company = $this->service->getCompanyFromRequest($request)) {
throw $this->createNotFoundException('Company is not available');
}
if (!$company->isDetailPagePublished()) {
throw $this->createNotFoundException();
}
$map = null !== $request->get('map');
if (
$company->getFriendlyUrl() &&
$this->featureManager->isActive(FeatureFlagListener::FEATURE_COMPANY_FRIENDY_URL) &&
'company-'.$company->getFriendlyUrl() !== $request->get('_route')
) {
return $this->redirectToRoute('company-'.$company->getFriendlyUrl());
}
if ($company->getDescription() && $description = json_decode($company->getDescription(), true, 512, \JSON_THROW_ON_ERROR)) {
$company->setDescription(implode('', $description));
}
$vacancyLimit = $this->config->get('site_company_vacancies_on_detail_limit');
$vacancies = [];
switch ($this->config->get('site_company_vacancies_on_detail_type')) {
case CompanySettingType::DETAIL_VACANCY_TYPE_FEATURED:
$vacancies = $this->service->getActiveVacancies(
$company,
$vacancyLimit,
$this->siteService->getSite(),
null,
$request->getLocale(),
true
);
break;
case CompanySettingType::DETAIL_VACANCY_TYPE_RECENT:
$vacancies = $this->service->getActiveVacancies(
$company,
$vacancyLimit,
$this->siteService->getSite(),
null,
$request->getLocale()
);
break;
case CompanySettingType::DETAIL_VACANCY_TYPE_RECENT_WITH_FILTER:
$preFilters = $this->config->get('site_company_vacancies_pre_filter');
$vacancies = $this->service->getActiveVacancies(
$company,
$vacancyLimit,
$this->siteService->getSite(),
$preFilters,
$request->getLocale()
);
break;
}
$vacancyDecorator->decorate($vacancies);
$relatedTestimonials = $this->tagManager->getEntitiesByRelatedTags($company, Testimonial::class, false, $request->getLocale());
$route = $this->getDoctrine()->getRepository(RouteEntity::class)->findOneBy(['name' => 'company_detail']);
$heroConfiguration = null;
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_HERO)) {
$heroConfiguration = $company->getHeroConfiguration();
}
$reviews = [];
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_FEEDBACK_COMPANY)) {
$reviews = $this->getDoctrine()->getRepository(FeedbackCompany::class)->findOneBy(['company' => $company]);
if (!empty($reviews) && \count($reviews->getReviews()) > 4) {
$reviews->setReviews(\array_slice($reviews->getReviews(), -4, 4));
}
}
return $this->pageRenderer
->withEntity($company)
->renderPage(
'{company}',
'@default/pages/company_detail.html.twig',
[
'company' => $company,
'map' => $map,
'vacancies' => $vacancies,
'companyVacancies' => $vacancies,
'companyVacanciesTotalCount' => $company->getPublishedVacancyCount(),
'relatedTestimonials' => $relatedTestimonials,
'overviewCountData' => $this->getOverviewOptionValues($company),
'reviews' => $reviews,
],
[$company->getCanonicalUrl()],
[
'metaTitle' => $company->getMetaTitle(),
'metaDescription' => $company->getMetaDescription(),
],
null,
$route,
$heroConfiguration
);
}
private function getOverviewOptionValues(Company $company): array
{
if (!$this->config->get('site_company_overview_option') instanceof Option) {
return [];
}
$optionValueRepository = $this->getDoctrine()->getRepository(OptionValue::class);
$overviewOptionValues = $optionValueRepository->findBy(
['option' => $this->config->get('site_company_overview_option')],
['value' => 'asc']
);
if (empty($overviewOptionValues)) {
return [];
}
$overviewCountData = $optionValueRepository->findOptionsWithVacancyCountForCompanyOption(
[$company->getId()], $this->config->get('site_company_overview_option')
)[$company->getId()] ?? [];
foreach ($overviewOptionValues as $optionValue) {
$count = 0;
foreach ($overviewCountData as $countData) {
if ($countData['id'] === $optionValue->getId()) {
$count = $countData['vacancy_count'];
}
}
$optionValue->setVacancyCount($count);
}
return $overviewOptionValues;
}
}