src/Component/ExternalIntegration/EventSubscriber/SendApplicationSubscriber.php line 91

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Component\ExternalIntegration\EventSubscriber;
  4. use App\Component\ExternalIntegration\AbstractExternalIntegration;
  5. use App\Component\ExternalIntegration\DependencyInjection\ExternalIntegrationCollection;
  6. use App\Component\ExternalIntegration\Event\PostApplicantFailEvent;
  7. use App\Component\ExternalIntegration\Event\PostApplicantSuccessEvent;
  8. use App\Component\ExternalIntegration\Message\Command\SendApplication;
  9. use App\Component\GDPR\Anonymize\AnonymizeApplicant;
  10. use App\Enum\DataTypeEnum;
  11. use App\Event\ApplicantEvent;
  12. use App\Mail\NotificationAddress;
  13. use App\Mail\RecipientsByImportance;
  14. use App\Notification\ExternalApplicantNotification;
  15. use App\Util\TypeUtil;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Messenger\MessageBusInterface;
  18. use Symfony\Component\Notifier\NotifierInterface;
  19. class SendApplicationSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         private readonly ExternalIntegrationCollection $externalIntegrationCollection,
  23.         private readonly MessageBusInterface $messageBus,
  24.         private readonly AnonymizeApplicant $anonymizeApplicant,
  25.         private readonly NotifierInterface $notifier,
  26.         private readonly RecipientsByImportance $recipientsByImportance,
  27.         private readonly NotificationAddress $notificationAddress,
  28.     ) {
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             ApplicantEvent::EVENT_POST_PERSIST => 'onApplicantPostPersist',
  34.             PostApplicantSuccessEvent::class => 'onPostApplicantSuccess',
  35.             PostApplicantFailEvent::class => 'onPostApplicantFail',
  36.         ];
  37.     }
  38.     public function onApplicantPostPersist(ApplicantEvent $applicantEvent): void
  39.     {
  40.         $applicant $applicantEvent->getApplicant();
  41.         $externalReference $applicant->getExternalVacancyReference() ?? '';
  42.         if (!TypeUtil::checkTypeAndTruthy($externalReferenceDataTypeEnum::STRING)
  43.             && $this->externalIntegrationCollection->hasActiveExternalIntegrations()
  44.         ) {
  45.             $activeExternalIntegrations $this->externalIntegrationCollection->getActiveExternalIntegrations();
  46.             /** @var AbstractExternalIntegration $firstActiveIntegration */
  47.             $firstActiveIntegration reset($activeExternalIntegrations);
  48.             $externalReference $firstActiveIntegration::getReference();
  49.         }
  50.         // only accept vacancies with valid external reference
  51.         if (!TypeUtil::checkTypeAndTruthy($externalReferenceDataTypeEnum::STRING)
  52.             || !\is_int($applicant->getId())
  53.         ) {
  54.             return;
  55.         }
  56.         $this->messageBus->dispatch(new SendApplication($applicant->getId(), $externalReference));
  57.     }
  58.     public function onPostApplicantSuccess(PostApplicantSuccessEvent $event): void
  59.     {
  60.         $from $this->notificationAddress->getFrom();
  61.         $applicant $event->getApplicant();
  62.         if (\is_string($applicant->getExternalId())
  63.             && \is_string($applicant->getExternalCandidateId())
  64.         ) {
  65.             $this->anonymizeApplicant->anonymize($applicant);
  66.         } elseif ('' !== $from) {
  67.             $notification ExternalApplicantNotification::create(
  68.                 $from,
  69.                 $event->getAts(),
  70.                 $applicant,
  71.                 'An Applicant could not be anonymized.'
  72.             );
  73.             $this->notifier->send(
  74.                 $notification,
  75.                 ...$this->recipientsByImportance->get($notification->getImportance())
  76.             );
  77.         }
  78.     }
  79.     public function onPostApplicantFail(PostApplicantFailEvent $event): void
  80.     {
  81.         $from $this->notificationAddress->getFrom();
  82.         if ('' !== $from) {
  83.             $notification ExternalApplicantNotification::create(
  84.                 $from,
  85.                 $event->getAts(),
  86.                 $event->getApplicant(),
  87.                 'Applicant could not be sent.'
  88.                 .\PHP_EOL
  89.                 .($event->getThrowable()?->getMessage() ?? 'There was no error message.')
  90.             );
  91.             $this->notifier->send($notification, ...$this->recipientsByImportance->get($notification->getImportance()));
  92.         }
  93.     }
  94. }