src/EventSubscriber/LeaveStatusWorkflowSubscriber.php line 43

  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\LeaveManagement\LeaveAllowanceBooker;
  8. use App\LeaveManagement\LeaveDurationCalculator;
  9. use Symfony\Component\Workflow\WorkflowInterface;
  10. use Symfony\Component\Workflow\Event\CompletedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class LeaveStatusWorkflowSubscriber implements EventSubscriberInterface
  13. {
  14.     use LeaveAllowanceModifying;
  15.     public function __construct(
  16.         private readonly EntityManagerInterface $entityManager,
  17.         private readonly LeaveDurationCalculator $leaveDurationCalculator,
  18.         private readonly LeaveAllowanceBooker $leaveAllowanceBooker,
  19.         private readonly NotificationFactory $notificationFactory,
  20.         protected readonly WorkflowInterface $leaveRequestStateMachine,
  21.     ) {
  22.     }
  23.     public function completedCancel(CompletedEvent $event)
  24.     {
  25.         $leave $event->getSubject();
  26.         $this->releaseAllowanceDays($event);
  27.         $this->voidLeaveRequest($event);
  28.         $notification $this->notificationFactory
  29.             ->getNotification(
  30.                 NotificationType::LEAVE_CANCELLED_BY_HR__EMPLOYEE,
  31.                 ['leave' => $leave]
  32.             );
  33.         $this->entityManager->persist($notification);
  34.     }
  35.     public function completedCancelViaRequest(CompletedEvent $event)
  36.     {
  37.         $leave $event->getSubject();
  38.         $this->releaseAllowanceDays($event);
  39.         $this->voidLeaveRequest($event);
  40.     }
  41.     public function voidLeaveRequest(CompletedEvent $event)
  42.     {
  43.         $request $event->getSubject()->getRequest();
  44.         if (!is_null($request)) {
  45.             $this->leaveRequestStateMachine
  46.                 ->apply(
  47.                     $request,
  48.                     'void',
  49.                 );
  50.         }
  51.     }
  52.     public static function getSubscribedEvents(): Generator
  53.     {
  54.         $workflowName 'leave';
  55.         $transitionNames = [
  56.             'cancel' => ['completedCancel'],
  57.             'cancel_via_request' => ['completedCancelViaRequest'],
  58.         ];
  59.         foreach ($transitionNames as $transitionName => $functionNames) {
  60.             yield 'workflow.' $workflowName '.completed.' $transitionName => $functionNames;
  61.         }
  62.     }
  63. }