<?php
namespace App\Controller\Page;
use App\Component\Configuration\Util\Config;
use App\Component\FeedbackCompany\Util\FeedbackCompanyUtil;
use App\Entity\Company;
use App\Entity\CompanyAttributeValue;
use App\Entity\Option;
use App\Entity\OptionValue;
use App\EventListener\FeatureFlagListener;
use App\Renderer\Page as PageRenderer;
use App\Service\CompanyService;
use App\Service\SiteService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
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 Symfony\Component\Routing\Annotation\Route;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
#[Route(path: '/companies')]
class CompaniesController extends AbstractController
{
public function __construct(
private readonly PageRenderer $pageRenderer,
private readonly SiteService $siteService,
private readonly CompanyService $companyService,
private readonly FeatureManagerInterface $featureManager,
private readonly Config $config,
private readonly FeedbackCompanyUtil $feedbackCompanyUtil,
private readonly EntityManagerInterface $entityManager,
) {
}
/**
* @Tag({"pages", "company-overview"})
*
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
#[Route(path: '/', name: 'companies')]
public function indexAction(Request $request): Response
{
$map = null !== $request->get('map');
$companyRepository = $this->entityManager->getRepository(Company::class);
if (true === $this->config->get('site_company_hidden_when_detail_disabled')) {
$companies = $companyRepository->findBy(['detailPage' => true]);
} else {
$companies = $companyRepository->findAll();
}
$companyAttributeValues = $this->getDoctrine()->getRepository(CompanyAttributeValue::class)->findAll();
array_walk($companies, static fn (Company $company) => $company->setAttributes(array_values(array_filter(
$companyAttributeValues,
static fn (CompanyAttributeValue $value) => $value->getSubject()->getId() === $company->getId()
)))
);
$overviewVacancyCounts = [];
if ($this->config->get('site_company_overview_option') instanceof Option) {
$overviewVacancyCounts = $this->getDoctrine()->getRepository(OptionValue::class)
->findOptionsWithVacancyCountForCompanyOption(
array_map(static fn (Company $company) => $company->getId(), $companies),
$this->config->get('site_company_overview_option')
);
}
$reviewSummary = [];
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_FEEDBACK_COMPANY)) {
$reviewSummary = $this->feedbackCompanyUtil->fetchAllById();
}
return $this->pageRenderer
->renderPage(
'{companies}',
'@default/pages/company_overview.html.twig',
[
'companies' => $companies,
'map' => $map,
'mapStyle' => $this->config->get('site_company_map_style'),
'overviewVacancyCounts' => $overviewVacancyCounts,
'reviewSummary' => $reviewSummary,
]
)
;
}
/**
* @Tag("companies")
*/
#[Route(path: '/companies_slider_overview', name: 'companies_slider_overview')]
public function sliderOverviewAction(Request $request): Response
{
$limit = $request->get('limit', 24);
$site = $this->siteService->getSite();
$sort = $this->config->get('site_company_slider_snippet_sort');
if (Company::SORT_VACANCY_COUNT === $sort) {
$companies = $this->companyService->findCompaniesOrderedByVacancyCount(
$limit,
$site,
$request->getLocale()
);
} else {
$companies = $this->getDoctrine()->getRepository(Company::class)
->findBy([], ['name' => 'asc'], $limit);
}
$overviewVacancyCounts = [];
if ($this->config->get('site_company_overview_option') instanceof Option) {
$overviewVacancyCounts = $this->getDoctrine()->getRepository(OptionValue::class)
->findOptionsWithVacancyCountForCompanyOption(
array_map(static fn (Company $company) => $company->getId(), $companies),
$this->config->get('site_company_overview_option')
);
}
$reviewSummary = [];
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_FEEDBACK_COMPANY)) {
$reviewSummary = $this->feedbackCompanyUtil->fetchAllById();
}
$template = '@default/pages/company_slider_overview.html.twig';
if ($templateName = $request->get('template')) {
$template = sprintf('@default/pages/%s.html.twig', $templateName);
}
return $this->render($template, [
'companies' => $companies,
'overviewVacancyCounts' => $overviewVacancyCounts,
'reviewSummary' => $reviewSummary,
]);
}
/**
* @Tag("companies")
*/
#[Route(path: '/featured_grid', name: 'company_featured_grid')]
public function topCompanySnippet(Request $request): Response
{
$site = null;
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
$site = $request->getHost();
}
if ($request->get('site')) {
$site = $request->get('site');
}
try {
$companies = $this->companyService->getTopCompanies(
$request->get('limit', 4),
$site
);
} catch (Exception) {
$companies = [];
}
return $this->render(
'@default/pages/company_featured_grid.html.twig',
['companies' => $companies]
);
}
}