<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Entity\Vacancy;
use App\Event\PageEvent;
use App\Event\VacancyRedirectEvent;
use App\Form\Setting\VacancySettingType;
use App\Service\VacancyService;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Http\FirewallMapInterface;
class VacancyListener
{
public function __construct(
private readonly VacancyService $vacancyService,
private readonly Config $config,
private readonly FirewallMapInterface $firewallMap,
private readonly RequestStack $requestStack,
) {
}
/** @throws \Exception */
public function postLoad(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
/** @var Vacancy $entity */
if (!$entity instanceof Vacancy) {
return;
}
$daysVacancyIsNew = (int) $this->config->get('site_vacancy_days_new_state');
$daysVacancyAge = $entity->getCreated() ? $entity->getCreated()->diff(new \DateTime())->days : 0;
$entity->setNew($daysVacancyAge <= $daysVacancyIsNew);
if (($startDate = $entity->getStartDate()) && $startDate > new \DateTime()) {
$entity->setActive(false);
}
if (($endDate = $entity->getEndDate()) && $endDate < new \DateTime()) {
$entity->setActive(false);
}
$currentRequest = $this->requestStack->getCurrentRequest();
if (
$this->firewallMap instanceof FirewallMap
&& $currentRequest instanceof Request
&& 'admin' !== $this->firewallMap->getFirewallConfig($currentRequest)?->getName()
) {
$this->activateVacancy($entity);
}
if (!$entity->isPublished()) {
$entity->setCanApply(VacancySettingType::DELETED_STRATEGY_SHOW_WITH_APPLY ===
$this->config->get('site_vacancy_deleted_vacancy_strategy'));
}
}
public function postPersist(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
/** @var Vacancy $entity */
if (!$entity instanceof Vacancy) {
return;
}
}
public function postUpdate(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
/** @var Vacancy $entity */
if (!$entity instanceof Vacancy) {
return;
}
$this->activateVacancy($entity);
}
/**
* @throws \Exception
*/
public function onBeforeRender(PageEvent $pageEvent): void
{
$page = $pageEvent->getPage();
if (false !== mb_strpos($page?->getBody() ?? '', '{recentVacancies}')) {
$page->setBody(str_replace(
'{recentVacancies}',
$this->vacancyService->renderLatestVacancies(4, $page->getLocale()),
$page->getBody()
));
}
}
public function onRedirectToExternalUrl(VacancyRedirectEvent $vacancyRedirectEvent): void
{
$this->vacancyService->addRedirectClick($vacancyRedirectEvent->getVacancy());
}
private function activateVacancy(Vacancy $entity): void
{
$currentDateTime = new \DateTime();
if (
$this->config->get('site_vacancy_state_change_automatically') &&
!$entity->isDeleted() &&
(!$entity->getStartDate() || ($currentDateTime->getTimestamp() >= $entity->getStartDate()->getTimeStamp())) &&
(!$entity->getEndDate() || ($currentDateTime->getTimestamp() <= $entity->getEndDate()->getTimeStamp()))
) {
$entity->setActive(true);
}
}
}