src/EventListener/VacancyListener.php line 91

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Component\Configuration\Util\Config;
  5. use App\Entity\Vacancy;
  6. use App\Event\PageEvent;
  7. use App\Event\VacancyRedirectEvent;
  8. use App\Form\Setting\VacancySettingType;
  9. use App\Service\VacancyService;
  10. use Doctrine\Persistence\Event\LifecycleEventArgs;
  11. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Http\FirewallMapInterface;
  15. class VacancyListener
  16. {
  17.     public function __construct(
  18.         private readonly VacancyService $vacancyService,
  19.         private readonly Config $config,
  20.         private readonly FirewallMapInterface $firewallMap,
  21.         private readonly RequestStack $requestStack,
  22.     ) {
  23.     }
  24.     /** @throws \Exception */
  25.     public function postLoad(LifecycleEventArgs $args): void
  26.     {
  27.         $entity $args->getObject();
  28.         /** @var Vacancy $entity */
  29.         if (!$entity instanceof Vacancy) {
  30.             return;
  31.         }
  32.         $daysVacancyIsNew = (int) $this->config->get('site_vacancy_days_new_state');
  33.         $daysVacancyAge $entity->getCreated() ? $entity->getCreated()->diff(new \DateTime())->days 0;
  34.         $entity->setNew($daysVacancyAge <= $daysVacancyIsNew);
  35.         if (($startDate $entity->getStartDate()) && $startDate > new \DateTime()) {
  36.             $entity->setActive(false);
  37.         }
  38.         if (($endDate $entity->getEndDate()) && $endDate < new \DateTime()) {
  39.             $entity->setActive(false);
  40.         }
  41.         $currentRequest $this->requestStack->getCurrentRequest();
  42.         if (
  43.             $this->firewallMap instanceof FirewallMap
  44.             && $currentRequest instanceof Request
  45.             && 'admin' !== $this->firewallMap->getFirewallConfig($currentRequest)?->getName()
  46.         ) {
  47.             $this->activateVacancy($entity);
  48.         }
  49.         if (!$entity->isPublished()) {
  50.             $entity->setCanApply(VacancySettingType::DELETED_STRATEGY_SHOW_WITH_APPLY ===
  51.                 $this->config->get('site_vacancy_deleted_vacancy_strategy'));
  52.         }
  53.     }
  54.     public function postPersist(LifecycleEventArgs $args): void
  55.     {
  56.         $entity $args->getObject();
  57.         /** @var Vacancy $entity */
  58.         if (!$entity instanceof Vacancy) {
  59.             return;
  60.         }
  61.     }
  62.     public function postUpdate(LifecycleEventArgs $args): void
  63.     {
  64.         $entity $args->getObject();
  65.         /** @var Vacancy $entity */
  66.         if (!$entity instanceof Vacancy) {
  67.             return;
  68.         }
  69.         $this->activateVacancy($entity);
  70.     }
  71.     /**
  72.      * @throws \Exception
  73.      */
  74.     public function onBeforeRender(PageEvent $pageEvent): void
  75.     {
  76.         $page $pageEvent->getPage();
  77.         if (false !== mb_strpos($page?->getBody() ?? '''{recentVacancies}')) {
  78.             $page->setBody(str_replace(
  79.                 '{recentVacancies}',
  80.                 $this->vacancyService->renderLatestVacancies(4$page->getLocale()),
  81.                 $page->getBody()
  82.             ));
  83.         }
  84.     }
  85.     public function onRedirectToExternalUrl(VacancyRedirectEvent $vacancyRedirectEvent): void
  86.     {
  87.         $this->vacancyService->addRedirectClick($vacancyRedirectEvent->getVacancy());
  88.     }
  89.     private function activateVacancy(Vacancy $entity): void
  90.     {
  91.         $currentDateTime = new \DateTime();
  92.         if (
  93.             $this->config->get('site_vacancy_state_change_automatically') &&
  94.             !$entity->isDeleted() &&
  95.             (!$entity->getStartDate() || ($currentDateTime->getTimestamp() >= $entity->getStartDate()->getTimeStamp())) &&
  96.             (!$entity->getEndDate() || ($currentDateTime->getTimestamp() <= $entity->getEndDate()->getTimeStamp()))
  97.         ) {
  98.             $entity->setActive(true);
  99.         }
  100.     }
  101. }