src/EventSubscriber/LeaveCancelGuardSubscriber.php line 22

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Leave;
  4. use App\Enum\LeaveCancellationRequestStatus;
  5. use App\WorkMonthManagement\WorkMonthChecker;
  6. use Symfony\Component\Workflow\Event\GuardEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class LeaveCancelGuardSubscriber implements EventSubscriberInterface
  9. {
  10.     private WorkMonthChecker $workMonthChecker;
  11.     public function __construct(
  12.         WorkMonthChecker $workMonthChecker,
  13.     )
  14.     {
  15.         $this->workMonthChecker $workMonthChecker;
  16.     }
  17.     public function guardCancel(GuardEvent $event): void
  18.     {
  19.         /** @var Leave $leave */
  20.         $leave $event->getSubject();
  21.         $leaveCancellationRequest $leave->getLeaveCancellationRequest();
  22.         $alreadyHasACancellationRequest null !== $leaveCancellationRequest;
  23.         if (
  24.             $alreadyHasACancellationRequest
  25.             && (
  26.                 $leaveCancellationRequest->getStatus() === LeaveCancellationRequestStatus::REQUESTED
  27.                 || $leaveCancellationRequest->getStatus() === LeaveCancellationRequestStatus::ACCEPTED
  28.             )
  29.         ) {
  30.             $event->setBlocked(
  31.                 true,
  32.                 'A cancellation request already exists for this leave, please accept it instead.'
  33.             );
  34.         }
  35.         $employee $leave->getEmployee();
  36.         $startDate $leave->getStartDate();
  37.         $endDate $leave->getEndDate();
  38.         if ($this->workMonthChecker->isLockedWorkMonthIntersectingPeriod($employee$startDate$endDate)) {
  39.             $event->setBlocked(
  40.                 true,
  41.                 'The month the leave is in was already validated by a Project Admin.'
  42.             );
  43.         }
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         $workflowName 'leave';
  48.         $transitionName 'cancel';
  49.         return [
  50.             'workflow.' $workflowName '.guard.' $transitionName => ['guardCancel'],
  51.         ];
  52.     }
  53. }