src/EventSubscriber/LeaveCancelGuardSubscriber.php line 22
<?phpnamespace App\EventSubscriber;use App\Entity\Leave;use App\Enum\LeaveCancellationRequestStatus;use App\WorkMonthManagement\WorkMonthChecker;use Symfony\Component\Workflow\Event\GuardEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class LeaveCancelGuardSubscriber implements EventSubscriberInterface{private WorkMonthChecker $workMonthChecker;public function __construct(WorkMonthChecker $workMonthChecker,){$this->workMonthChecker = $workMonthChecker;}public function guardCancel(GuardEvent $event): void{/** @var Leave $leave */$leave = $event->getSubject();$leaveCancellationRequest = $leave->getLeaveCancellationRequest();$alreadyHasACancellationRequest = null !== $leaveCancellationRequest;if ($alreadyHasACancellationRequest&& ($leaveCancellationRequest->getStatus() === LeaveCancellationRequestStatus::REQUESTED|| $leaveCancellationRequest->getStatus() === LeaveCancellationRequestStatus::ACCEPTED)) {$event->setBlocked(true,'A cancellation request already exists for this leave, please accept it instead.');}$employee = $leave->getEmployee();$startDate = $leave->getStartDate();$endDate = $leave->getEndDate();if ($this->workMonthChecker->isLockedWorkMonthIntersectingPeriod($employee, $startDate, $endDate)) {$event->setBlocked(true,'The month the leave is in was already validated by a Project Admin.');}}public static function getSubscribedEvents(): array{$workflowName = 'leave';$transitionName = 'cancel';return ['workflow.' . $workflowName . '.guard.' . $transitionName => ['guardCancel'],];}}