<?php
namespace App\EventSubscriber\App;
use App\Event\WantedJobCreatedDuringProfileCreationEvent;
use App\Event\WantedJobCreatedEvent;
use App\Event\WantedJobEditedEvent;
use App\Event\WantedJobEvent;
use App\Event\WantedJobSharePageRequestedEvent;
use App\Service\ConversionEventService;
use App\Service\NotificationService;
use App\Service\WantedJobService;
use Exception;
use JanusHercules\DatawarehouseIntegration\Domain\Entity\BusinessEvent;
use JanusHercules\DatawarehouseIntegration\Domain\Service\BusinessEventDomainService;
use JanusHercules\Jobradar\Domain\SymfonyMessage\CreateJobradarMatchTriggeredByWantedJobSymfonyMessage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class WantedJobEventSubscriber implements EventSubscriberInterface
{
private BusinessEventDomainService $businessEventDomainService;
private NotificationService $notificationService;
private ConversionEventService $conversionEventService;
private WantedJobService $wantedJobService;
private MessageBusInterface $messageBus;
public function __construct(
BusinessEventDomainService $businessEventDomainService,
NotificationService $notificationService,
ConversionEventService $conversionEventService,
WantedJobService $wantedJobService,
MessageBusInterface $messageBus
) {
$this->businessEventDomainService = $businessEventDomainService;
$this->notificationService = $notificationService;
$this->conversionEventService = $conversionEventService;
$this->wantedJobService = $wantedJobService;
$this->messageBus = $messageBus;
}
public static function getSubscribedEvents()
{
return [
WantedJobCreatedEvent::class => 'onWantedJobCreated',
WantedJobCreatedDuringProfileCreationEvent::class => 'onWantedJobCreatedDuringProfileCreation',
WantedJobEditedEvent::class => 'onWantedJobEdited',
WantedJobSharePageRequestedEvent::class => 'onWantedJobSharePageRequested',
];
}
/** @throws Exception */
public function onWantedJobCreated(
WantedJobEvent $event
): void {
if (!is_null($event->getWantedJob())) {
if ($event->getWantedJob()->isPublished()) {
$this->messageBus->dispatch(
new CreateJobradarMatchTriggeredByWantedJobSymfonyMessage(
$event->getWantedJob()
)
);
}
$this->businessEventDomainService->writeNewEvent(
BusinessEvent::EVENT_TYPE_WANTEDJOB_CREATED,
$event->getWantedJob()->getJobseekerProfile()->getUser(),
null,
$event->getWantedJob(),
json_encode(
[
'id' => $event->getWantedJob()->getId(),
'searchterm' => $event->getWantedJob()->getOccupationalFieldSearchterm(),
'zipcode' => $event->getWantedJob()->getZipcode()
]
)
);
$this->notificationService->uncancelNotification(
$event->getWantedJob()->getJobseekerProfile()->getUser(),
NotificationService::NOTIFICATION_TYPE_UNREAD_CONVERSATION_MESSAGES
);
$this->conversionEventService->handleConversionGoalReached(
ConversionEventService::CAMPAIGN_ID_NOTIFICATION,
NotificationService::NOTIFICATION_TYPE_MISSING_WANTED_JOBS
);
$this->wantedJobService->checkIfWisagNeedsToBeNotified($event->getWantedJob());
}
}
public function onWantedJobCreatedDuringProfileCreation(WantedJobEvent $event): void
{
if (!is_null($event->getWantedJob())) {
if ($event->getWantedJob()->isPublished()) {
$this->messageBus->dispatch(
new CreateJobradarMatchTriggeredByWantedJobSymfonyMessage(
$event->getWantedJob()
)
);
}
$this->businessEventDomainService->writeNewEvent(
BusinessEvent::EVENT_TYPE_WANTEDJOB_CREATED_DURING_PROFILE_CREATION,
$event->getWantedJob()->getJobseekerProfile()->getUser(),
null,
$event->getWantedJob(),
json_encode(
[
'id' => $event->getWantedJob()->getId(),
'searchterm' => $event->getWantedJob()->getOccupationalFieldSearchterm(),
'zipcode' => $event->getWantedJob()->getZipcode()
]
)
);
$this->wantedJobService->checkIfWisagNeedsToBeNotified($event->getWantedJob());
}
}
/** @throws Exception */
public function onWantedJobEdited(WantedJobEvent $event): void
{
if (!is_null($event->getWantedJob())) {
if ($event->getWantedJob()->isPublished()) {
$this->messageBus->dispatch(
new CreateJobradarMatchTriggeredByWantedJobSymfonyMessage(
$event->getWantedJob()
)
);
}
$this->businessEventDomainService->writeNewEvent(
BusinessEvent::EVENT_TYPE_WANTEDJOB_EDITED,
$event->getWantedJob()->getJobseekerProfile()->getUser(),
null,
$event->getWantedJob(),
json_encode(
[
'id' => $event->getWantedJob()->getId(),
'searchterm' => $event->getWantedJob()->getOccupationalFieldSearchterm(),
'zipcode' => $event->getWantedJob()->getZipcode()
]
)
);
$this->notificationService->uncancelNotification(
$event->getWantedJob()->getJobseekerProfile()->getUser(),
NotificationService::NOTIFICATION_TYPE_UNREAD_CONVERSATION_MESSAGES
);
}
}
/**
* @throws Exception
*/
public function onWantedJobSharePageRequested(WantedJobSharePageRequestedEvent $event): void
{
$this->conversionEventService
->initiateGoalReachedConversionTrackingBasedOnRequest(
null,
$event->getRequestingUser()
);
}
}