src/EventSubscriber/LeaveCancellationRequestStatusWorkflowSubscriber.php line 24

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Generator;
  4. use App\Notifier\NotificationType;
  5. use App\Notifier\NotificationFactory;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Entity\LeaveCancellationRequest;
  8. use Symfony\Component\Workflow\WorkflowInterface;
  9. use Symfony\Component\Workflow\Event\CompletedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class LeaveCancellationRequestStatusWorkflowSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private readonly EntityManagerInterface $entityManager,
  15.         private readonly NotificationFactory $notificationFactory,
  16.         protected readonly WorkflowInterface $leaveStateMachine,
  17.     ) {
  18.     }
  19.     public function completedDecline(CompletedEvent $event)
  20.     {
  21.         $leaveCancellationRequest $event->getSubject();
  22.         if ($leaveCancellationRequest instanceof LeaveCancellationRequest) {
  23.             $notification $this->notificationFactory
  24.                 ->getNotification(
  25.                     NotificationType::LEAVE_CANCELLATION_REQUEST_DECLINED__REQUESTER,
  26.                     ['leaveCancellationRequest' => $leaveCancellationRequest]
  27.                 );
  28.             $this->entityManager->persist($notification);
  29.         }
  30.     }
  31.     public function completedCancel(CompletedEvent $event)
  32.     {
  33.         $leaveCancellationRequest $event->getSubject();
  34.         if ($leaveCancellationRequest instanceof LeaveCancellationRequest) {
  35.             $notification $this->notificationFactory
  36.                 ->getNotification(
  37.                     NotificationType::LEAVE_CANCELLATION_REQUEST_CANCELLED_BY_REQUESTER__HR,
  38.                     ['leaveCancellationRequest' => $leaveCancellationRequest]
  39.                 );
  40.             $this->entityManager->persist($notification);
  41.         }
  42.     }
  43.     public function completedAccept(CompletedEvent $event)
  44.     {
  45.         $leaveCancellationRequest $event->getSubject();
  46.         if ($leaveCancellationRequest instanceof LeaveCancellationRequest) {
  47.             $this->leaveStateMachine
  48.                 ->apply(
  49.                     $leaveCancellationRequest->getLeave(),
  50.                     'cancel_via_request',
  51.                 );
  52.             $notification $this->notificationFactory
  53.                 ->getNotification(
  54.                     NotificationType::LEAVE_CANCELLATION_REQUEST_ACCEPTED__REQUESTER,
  55.                     ['leaveCancellationRequest' => $leaveCancellationRequest]
  56.                 );
  57.             $this->entityManager->persist($notification);
  58.         } else {
  59.             die('test');
  60.         }
  61.     }
  62.     public static function getSubscribedEvents(): Generator
  63.     {
  64.         $workflowName 'leave_cancellation_request';
  65.         // Leave cancellations requests only happen on creation (no draft status)
  66.         // The notification must be sent post persist to know the ID
  67.         $transitionNames = [
  68.             'accept' => ['completedAccept'],
  69.             'cancel' => ['completedCancel'],
  70.             'decline' => ['completedDecline'],
  71.         ];
  72.         foreach ($transitionNames as $transitionName => $functionNames) {
  73.             yield 'workflow.' $workflowName '.completed.' $transitionName => $functionNames;
  74.         }
  75.     }
  76. }