src/EventSubscriber/LeaveRequestStatusWorkflowSubscriber.php line 70

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Generator;
  4. use App\Entity\Leave;
  5. use App\Entity\LeaveRequest;
  6. use App\Notifier\NotificationType;
  7. use App\Notifier\NotificationFactory;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use App\LeaveManagement\LeaveAllowanceBooker;
  10. use Symfony\Component\Workflow\Event\CompletedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class LeaveRequestStatusWorkflowSubscriber implements EventSubscriberInterface
  13. {
  14.     use LeaveAllowanceModifying;
  15.     public function __construct(
  16.         private readonly EntityManagerInterface $entityManager,
  17.         private readonly LeaveAllowanceBooker $leaveAllowanceBooker,
  18.         private readonly NotificationFactory $notificationFactory,
  19.     ) {
  20.     }
  21.     public function completedRequest(CompletedEvent $event)
  22.     {
  23.         $leaveRequest $event->getSubject();
  24.         if ($leaveRequest instanceof LeaveRequest) {
  25.             $notification $this->notificationFactory
  26.                 ->getNotification(
  27.                     NotificationType::LEAVE_REQUESTED__HR,
  28.                     ['leaveRequest' => $leaveRequest]
  29.                 );
  30.             $this->entityManager->persist($notification);
  31.         }
  32.     }
  33.     public function completedDecline(CompletedEvent $event)
  34.     {
  35.         $leaveRequest $event->getSubject();
  36.         if ($leaveRequest instanceof LeaveRequest) {
  37.             $this->releaseAllowanceDays($event);
  38.             $notification $this->notificationFactory
  39.                 ->getNotification(
  40.                     NotificationType::LEAVE_REQUEST_DECLINED__REQUESTER,
  41.                     ['leaveRequest' => $leaveRequest]
  42.                 );
  43.             $this->entityManager->persist($notification);
  44.         }
  45.     }
  46.     public function completedCancel(CompletedEvent $event)
  47.     {
  48.         $leaveRequest $event->getSubject();
  49.         if ($leaveRequest instanceof LeaveRequest) {
  50.             $this->releaseAllowanceDays($event);
  51.             $notification $this->notificationFactory
  52.                 ->getNotification(
  53.                     NotificationType::LEAVE_REQUEST_CANCELLED_BY_REQUESTER__HR,
  54.                     ['leaveRequest' => $leaveRequest]
  55.                 );
  56.             $this->entityManager->persist($notification);
  57.         }
  58.     }
  59.     public function completedAccept(CompletedEvent $event)
  60.     {
  61.         $leaveRequest $event->getSubject();
  62.         if ($leaveRequest instanceof LeaveRequest) {
  63.             $leave = new Leave();
  64.             $leave->setEmployee($leaveRequest->getEmployee());
  65.             $leave->setStartDate($leaveRequest->getStartDate());
  66.             $leave->setFirstDayHours($leaveRequest->getFirstDayHours());
  67.             $leave->setLastDayHours($leaveRequest->getLastDayHours());
  68.             $leave->setEndDate($leaveRequest->getEndDate());
  69.             $leave->setType($leaveRequest->getType());
  70.             $leave->setDetails($leaveRequest->getDetails());
  71.             $leave->setRequest($leaveRequest);
  72.             $leave->setDuration($leaveRequest->getDuration());
  73.             $this->entityManager->persist($leave);
  74.             $notification $this->notificationFactory
  75.                 ->getNotification(
  76.                     NotificationType::LEAVE_REQUEST_ACCEPTED__REQUESTER,
  77.                     ['leaveRequest' => $leaveRequest]
  78.                 );
  79.             $this->entityManager->persist($notification);
  80.         }
  81.     }
  82.     public static function getSubscribedEvents(): Generator
  83.     {
  84.         $workflowName 'leave_request';
  85.         // Do not create HR notification on instant_request transition
  86.         // The notification is handled post persist of LeaveRequest entity to contain ID information
  87.         $transitionNames = [
  88.             'request' => ['completedRequest'],
  89.             'accept' => ['completedAccept'],
  90.             'cancel' => ['completedCancel'],
  91.             'decline' => ['completedDecline'],
  92.         ];
  93.         foreach ($transitionNames as $transitionName => $functionNames) {
  94.             yield 'workflow.' $workflowName '.completed.' $transitionName => $functionNames;
  95.         }
  96.     }
  97. }