<?php
namespace App\Component\ExternalIntegration\Integration\Bullhorn\EventSubscriber;
use App\Client\BullhornClient;
use App\Entity\ExternalIntegrationLogTransaction;
use App\Entity\ExternalIntegrationLogTransactionEntry;
use App\Entity\Registration;
use App\Event\BullhornApplicantEvent;
use App\Event\BullhornEventFormSubmitEvent;
use App\Manager\LogTransactionManager;
use App\Util\Bullhorn;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BullhornEventSubscriber implements EventSubscriberInterface
{
private const TAG = 'BullhornEventSubscriber';
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly BullhornClient $bullhornClient,
private readonly LoggerInterface $logger,
private readonly LogTransactionManager $logManager
) {
}
public static function getSubscribedEvents(): array
{
return [
BullhornEventFormSubmitEvent::class => 'eventRegistrationSubmitted',
BullhornApplicantEvent::CANDIDATE_SAVED => 'candidateSaved',
];
}
/**
* @throws \Exception
*/
public function eventRegistrationSubmitted(BullhornEventFormSubmitEvent $event): void
{
$transaction = $this->logManager->initTransaction(
'Handling registration',
Bullhorn::REFERENCE,
json_encode(compact('event'), \JSON_THROW_ON_ERROR),
Registration::class
);
$registration = $event->getRegistration();
try {
$ownerId = $this->bullhornClient->findVacancyOwner($registration->getEvent()?->getExternalId(), $transaction);
$candidateData = [
'id' => $registration->getId(),
'email' => $registration->getEmail(),
'first_name' => $registration->getFirstName(),
'last_name' => $registration->getLastName(),
'phone' => $registration->getPhone(),
'owner' => [
'id' => $ownerId,
],
];
if ($registration->getGdpr()) {
$createdAt = $registration->getCreatedAt()?->format('U');
$candidateData['customObject1s'] = [
[
'text1' => 'Recruitment Activiteiten',
'text2' => $registration->getGdpr()->getIntegrationValue() ?? $registration->getGdpr()->getTitle(),
'text3' => 'Website',
'date1' => (int) $createdAt * 1000,
],
];
}
$candidateId = $this->bullhornClient->upsertCandidate($candidateData, $transaction);
$registrationId = $this->bullhornClient->addCandidateToVacancy($candidateData, $candidateId, $event->getEvent()->getExternalId(), $transaction);
$registration->setExternalId(sprintf('%s - %s', $candidateId, $registrationId));
$this->entityManager->persist($registration);
$this->entityManager->flush();
$this->logger->info(sprintf('Bullhorn integration : [%s] %s', self::TAG, 'Applicant has been added to vacancy in bullhorn'));
$this->logManager->log(
$transaction,
'Handled registration successfully',
ExternalIntegrationLogTransaction::LEVEL_SUCCESS,
json_encode(compact('event', 'registration'), \JSON_THROW_ON_ERROR),
ExternalIntegrationLogTransaction::LEVEL_SUCCESS,
Registration::class
);
} catch (\Exception $exception) {
$this->logger->error(sprintf('Bullhorn integration : [%s] %s', self::TAG, $exception->getMessage()));
$this->logManager->log(
$transaction,
'Handling registration failed',
ExternalIntegrationLogTransactionEntry::LEVEL_ERROR,
json_encode(compact('exception', 'event'), \JSON_THROW_ON_ERROR),
ExternalIntegrationLogTransaction::LEVEL_ERROR,
Registration::class
);
} finally {
$event->stopPropagation();
$this->logManager->save($transaction);
}
}
public function candidateSaved(BullhornApplicantEvent $event): void
{
$this->entityManager->persist($event->getApplicant());
$this->entityManager->flush();
}
}