src/EventSubscriber/LeaveCancellationAcceptGuardSubscriber.php line 23

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