src/Component/ExternalIntegration/Integration/Bullhorn/EventSubscriber/BullhornEventSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Component\ExternalIntegration\Integration\Bullhorn\EventSubscriber;
  3. use App\Client\BullhornClient;
  4. use App\Entity\ExternalIntegrationLogTransaction;
  5. use App\Entity\ExternalIntegrationLogTransactionEntry;
  6. use App\Entity\Registration;
  7. use App\Event\BullhornApplicantEvent;
  8. use App\Event\BullhornEventFormSubmitEvent;
  9. use App\Manager\LogTransactionManager;
  10. use App\Util\Bullhorn;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class BullhornEventSubscriber implements EventSubscriberInterface
  15. {
  16.     private const TAG 'BullhornEventSubscriber';
  17.     public function __construct(
  18.         private readonly EntityManagerInterface $entityManager,
  19.         private readonly BullhornClient $bullhornClient,
  20.         private readonly LoggerInterface $logger,
  21.         private readonly LogTransactionManager $logManager
  22.     ) {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             BullhornEventFormSubmitEvent::class => 'eventRegistrationSubmitted',
  28.             BullhornApplicantEvent::CANDIDATE_SAVED => 'candidateSaved',
  29.         ];
  30.     }
  31.     /**
  32.      * @throws \Exception
  33.      */
  34.     public function eventRegistrationSubmitted(BullhornEventFormSubmitEvent $event): void
  35.     {
  36.         $transaction $this->logManager->initTransaction(
  37.             'Handling registration',
  38.             Bullhorn::REFERENCE,
  39.             json_encode(compact('event'), \JSON_THROW_ON_ERROR),
  40.             Registration::class
  41.         );
  42.         $registration $event->getRegistration();
  43.         try {
  44.             $ownerId $this->bullhornClient->findVacancyOwner($registration->getEvent()?->getExternalId(), $transaction);
  45.             $candidateData = [
  46.                 'id' => $registration->getId(),
  47.                 'email' => $registration->getEmail(),
  48.                 'first_name' => $registration->getFirstName(),
  49.                 'last_name' => $registration->getLastName(),
  50.                 'phone' => $registration->getPhone(),
  51.                 'owner' => [
  52.                     'id' => $ownerId,
  53.                 ],
  54.             ];
  55.             if ($registration->getGdpr()) {
  56.                 $createdAt $registration->getCreatedAt()?->format('U');
  57.                 $candidateData['customObject1s'] = [
  58.                     [
  59.                         'text1' => 'Recruitment Activiteiten',
  60.                         'text2' => $registration->getGdpr()->getIntegrationValue() ?? $registration->getGdpr()->getTitle(),
  61.                         'text3' => 'Website',
  62.                         'date1' => (int) $createdAt 1000,
  63.                     ],
  64.                 ];
  65.             }
  66.             $candidateId $this->bullhornClient->upsertCandidate($candidateData$transaction);
  67.             $registrationId $this->bullhornClient->addCandidateToVacancy($candidateData$candidateId$event->getEvent()->getExternalId(), $transaction);
  68.             $registration->setExternalId(sprintf('%s - %s'$candidateId$registrationId));
  69.             $this->entityManager->persist($registration);
  70.             $this->entityManager->flush();
  71.             $this->logger->info(sprintf('Bullhorn integration : [%s] %s'self::TAG'Applicant has been added to vacancy in bullhorn'));
  72.             $this->logManager->log(
  73.                 $transaction,
  74.                 'Handled registration successfully',
  75.                 ExternalIntegrationLogTransaction::LEVEL_SUCCESS,
  76.                 json_encode(compact('event''registration'), \JSON_THROW_ON_ERROR),
  77.                 ExternalIntegrationLogTransaction::LEVEL_SUCCESS,
  78.                 Registration::class
  79.             );
  80.         } catch (\Exception $exception) {
  81.             $this->logger->error(sprintf('Bullhorn integration : [%s] %s'self::TAG$exception->getMessage()));
  82.             $this->logManager->log(
  83.                 $transaction,
  84.                 'Handling registration failed',
  85.                 ExternalIntegrationLogTransactionEntry::LEVEL_ERROR,
  86.                 json_encode(compact('exception''event'), \JSON_THROW_ON_ERROR),
  87.                 ExternalIntegrationLogTransaction::LEVEL_ERROR,
  88.                 Registration::class
  89.             );
  90.         } finally {
  91.             $event->stopPropagation();
  92.             $this->logManager->save($transaction);
  93.         }
  94.     }
  95.     public function candidateSaved(BullhornApplicantEvent $event): void
  96.     {
  97.         $this->entityManager->persist($event->getApplicant());
  98.         $this->entityManager->flush();
  99.     }
  100. }