src/Features/JobApplication/JobsApplicatonsWorkflowSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Features\JobApplication;
  3. use App\Entity\JobsApplications;
  4. use App\Features\JobApplication\Event\JobApplicationAcceptedEvent;
  5. use App\Features\JobApplication\Event\JobApplicationRejectedEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\Workflow\Event\Event;
  11. class JobsApplicatonsWorkflowSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(private EventDispatcherInterface $dispatcher,
  14.                                 private EntityManagerInterface $manager, private Security $security){}
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             'workflow.jobs_applications.completed.accept_application' => 'onApplicationAccepted',
  19.             'workflow.jobs_applications.completed.reject_application' => 'onApplicationRejected',
  20.         ];
  21.     }
  22.     public function onApplicationAccepted(Event $event): void
  23.     {
  24.         $jobApplication $event->getSubject();
  25.         if($jobApplication instanceof JobsApplications) {
  26.             $jobApplication->setAcceptedAt(new \DateTimeImmutable());
  27.             $jobApplication->setAcceptedBy($this->security->getUser());
  28.             $this->manager->flush();
  29.             $this->dispatcher->dispatch(new JobApplicationAcceptedEvent($jobApplication), JobApplicationAcceptedEvent::NAME);
  30.         }
  31.     }
  32.     public function onApplicationRejected(Event $event): void
  33.     {
  34.         $jobApplication $event->getSubject();
  35.         if($jobApplication instanceof JobsApplications) {
  36.             $jobApplication->setRejectedAt(new \DateTimeImmutable());
  37.             $jobApplication->setRejectedBy($this->security->getUser());
  38.             $this->manager->flush();
  39.             $this->dispatcher->dispatch(new JobApplicationRejectedEvent($jobApplication), JobApplicationRejectedEvent::NAME);
  40.         }
  41.     }
  42. }