<?php
namespace App\Features\JobApplication;
use App\Entity\JobsApplications;
use App\Features\JobApplication\Event\JobApplicationAcceptedEvent;
use App\Features\JobApplication\Event\JobApplicationRejectedEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Event\Event;
class JobsApplicatonsWorkflowSubscriber implements EventSubscriberInterface
{
public function __construct(private EventDispatcherInterface $dispatcher,
private EntityManagerInterface $manager, private Security $security){}
public static function getSubscribedEvents(): array
{
return [
'workflow.jobs_applications.completed.accept_application' => 'onApplicationAccepted',
'workflow.jobs_applications.completed.reject_application' => 'onApplicationRejected',
];
}
public function onApplicationAccepted(Event $event): void
{
$jobApplication = $event->getSubject();
if($jobApplication instanceof JobsApplications) {
$jobApplication->setAcceptedAt(new \DateTimeImmutable());
$jobApplication->setAcceptedBy($this->security->getUser());
$this->manager->flush();
$this->dispatcher->dispatch(new JobApplicationAcceptedEvent($jobApplication), JobApplicationAcceptedEvent::NAME);
}
}
public function onApplicationRejected(Event $event): void
{
$jobApplication = $event->getSubject();
if($jobApplication instanceof JobsApplications) {
$jobApplication->setRejectedAt(new \DateTimeImmutable());
$jobApplication->setRejectedBy($this->security->getUser());
$this->manager->flush();
$this->dispatcher->dispatch(new JobApplicationRejectedEvent($jobApplication), JobApplicationRejectedEvent::NAME);
}
}
}