<?php
namespace App\EventListener;
use App\Entity\ApplicantStatus;
use App\Event\ApplicantEvent;
use App\Mail\Mailer;
use App\Util\StringToEntityUtil;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class ApplicantEventListener
{
public function __construct(
private readonly FeatureManagerInterface $featureManager,
private readonly EntityManagerInterface $entityManager,
private readonly Mailer $mailer,
private readonly ParameterBagInterface $parameterBag,
private readonly StringToEntityUtil $stringToEntityUtil
) {
}
public function onPostPersist(ApplicantEvent $applicantEvent): void
{
if (!$this->featureManager->isActive('feature_ats')) {
return;
}
$applicant = $applicantEvent->getApplicant();
// if vacancy is marked as sourced external, do nothing
if (!empty($applicant->getVacancy()?->getExternalReference())) {
return;
}
$status = $this->stringToEntityUtil
->stringToEntity($this->parameterBag->get('site_ats_applicant_start_status'));
if (!$status) {
return;
}
$atsApplicantStatus = (new ApplicantStatus())
->setApplicant($applicant)
->setStatus($status);
$this->entityManager->persist($atsApplicantStatus);
$this->entityManager->flush();
if (!$applicant->isManual()) {
$this->mailer->invokeEntryOfStatus($atsApplicantStatus);
return;
}
if ($mail = $applicant->getManualMail()) {
$this->mailer->invokeEntryOfStatus($atsApplicantStatus, $mail->getBody());
return;
}
$this->mailer->invokeEntryOfStatus($atsApplicantStatus, null, false);
}
}