src/EventSubscriber/OvertimeRequestRequestGuardSubscriber.php line 31

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\OvertimeRequest;
  4. use App\Validator\NoOvertimeOverlap;
  5. use App\Validator\WorkMonthNotValidated;
  6. use App\Validator\NotExceedMaximumHours;
  7. use Symfony\Component\Workflow\Event\GuardEvent;
  8. use App\Validator\OvertimeWithDateWithinContractPeriod;
  9. use Symfony\Component\Validator\Validator\ValidatorInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class OvertimeRequestRequestGuardSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private readonly ValidatorInterface $validator
  15.     ) {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         $workflowName 'leave_request';
  20.         $transitionName 'request';
  21.         return [
  22.             'workflow.' $workflowName '.guard.' $transitionName => ['guardRequest'],
  23.         ];
  24.     }
  25.     public function guardRequest(GuardEvent $event): void
  26.     {
  27.         $subject $event->getSubject();
  28.         if (!$subject instanceof OvertimeRequest) {
  29.             return;
  30.         }
  31.         // Need to re-test the validation constraints that might
  32.         // have been unvalidated (outside the edit form) since drafting
  33.         $violations $this->validator->validate($subject, [
  34.             new NoOvertimeOverlap(),
  35.             new OvertimeWithDateWithinContractPeriod(),
  36.             new WorkMonthNotValidated(),
  37.             new NotExceedMaximumHours(),
  38.         ]);
  39.         $violationsCount $violations->count();
  40.         if ($violationsCount 0) {
  41.             $message '';
  42.             for ($violationIndex 0$violationIndex $violationsCount$violationIndex++) {
  43.                 $message .= $violations->get($violationIndex)->getMessage() . ' ';
  44.             }
  45.             $event->setBlocked(
  46.                 true,
  47.                 $message
  48.             );
  49.         }
  50.     }
  51. }