src/EventSubscriber/OvertimeRequestStatusWorkflowSubscriber.php line 55

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Generator;
  4. use App\Entity\Overtime;
  5. use App\Entity\OvertimeRequest;
  6. use App\Notifier\NotificationType;
  7. use App\Notifier\NotificationFactory;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Workflow\Event\CompletedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class OvertimeRequestStatusWorkflowSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private readonly EntityManagerInterface $entityManager,
  15.         private readonly NotificationFactory $notificationFactory,
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): Generator
  19.     {
  20.         $workflowName 'leave_request';
  21.         // Do not create HR notification on instant_request transition
  22.         // The notification is handled post persist of OvertimeRequest entity to contain ID information
  23.         $transitionNames = [
  24.             'request' => ['completedRequest'],
  25.             'accept' => ['completedAccept'],
  26.             'cancel' => ['completedCancel'],
  27.             'decline' => ['completedDecline'],
  28.         ];
  29.         foreach ($transitionNames as $transitionName => $functionNames) {
  30.             yield 'workflow.' $workflowName '.completed.' $transitionName => $functionNames;
  31.         }
  32.     }
  33.     public function completedRequest(CompletedEvent $event)
  34.     {
  35.         $overtimeRequest $event->getSubject();
  36.         if ($overtimeRequest instanceof OvertimeRequest) {
  37.             $notification $this->notificationFactory
  38.                 ->getNotification(
  39.                     NotificationType::OVERTIME_REQUESTED__HR,
  40.                     ['overtimeRequest' => $overtimeRequest]
  41.                 );
  42.             $this->entityManager->persist($notification);
  43.         }
  44.     }
  45.     public function completedDecline(CompletedEvent $event)
  46.     {
  47.         $overtimeRequest $event->getSubject();
  48.         if ($overtimeRequest instanceof OvertimeRequest) {
  49.             $notification $this->notificationFactory
  50.                 ->getNotification(
  51.                     NotificationType::OVERTIME_REQUEST_DECLINED__REQUESTER,
  52.                     ['overtimeRequest' => $overtimeRequest]
  53.                 );
  54.             $this->entityManager->persist($notification);
  55.         }
  56.     }
  57.     public function completedCancel(CompletedEvent $event)
  58.     {
  59.         $overtimeRequest $event->getSubject();
  60.         if ($overtimeRequest instanceof OvertimeRequest) {
  61.             $notification $this->notificationFactory
  62.                 ->getNotification(
  63.                     NotificationType::OVERTIME_REQUEST_CANCELLED_BY_REQUESTER__HR,
  64.                     ['overtimeRequest' => $overtimeRequest]
  65.                 );
  66.             $this->entityManager->persist($notification);
  67.         }
  68.     }
  69.     public function completedAccept(CompletedEvent $event)
  70.     {
  71.         $overtimeRequest $event->getSubject();
  72.         if ($overtimeRequest instanceof OvertimeRequest) {
  73.             $overtime = new Overtime();
  74.             $overtime->setEmployee($overtimeRequest->getEmployee());
  75.             $overtime->setDate($overtimeRequest->getDate());
  76.             $overtime->setDescription($overtimeRequest->getDescription());
  77.             $overtime->setRequest($overtimeRequest);
  78.             $overtime->setDuration($overtimeRequest->getDuration());
  79.             $this->entityManager->persist($overtime);
  80.             $notification $this->notificationFactory
  81.                 ->getNotification(
  82.                     NotificationType::OVERTIME_REQUEST_ACCEPTED__REQUESTER,
  83.                     ['overtimeRequest' => $overtimeRequest]
  84.                 );
  85.             $this->entityManager->persist($notification);
  86.         }
  87.     }
  88. }