src/EventSubscriber/Savills/WorkValidationRequestTransitionsSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Savills;
  3. use App\Repository\Savills\WorkValidationRequestRepository;
  4. use App\Service\Savills\WorkValidationRequest\WorkflowHandler;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Workflow\Event\Event;
  9. class WorkValidationRequestTransitionsSubscriber implements EventSubscriberInterface
  10. {
  11.     private WorkValidationRequestRepository $workValidationRequestRepository;
  12.     private WorkflowHandler $workflowHandler;
  13.     public function __construct(WorkflowHandler $workflowHandlerWorkValidationRequestRepository $workValidationRequestRepository,
  14.         private LoggerInterface $logger, private EntityManagerInterface $em
  15.     ) {
  16.         $this->workflowHandler $workflowHandler;
  17.         $this->workValidationRequestRepository $workValidationRequestRepository;
  18.     }
  19.     public function onChange(Event $event): void
  20.     {
  21.         $eventTransition $event->getTransition();
  22.         $transitionAuto $event->getMetadata('transition_auto'$eventTransition);
  23.         if (isset($transitionAuto) && true == $transitionAuto) {
  24.             $workValidationRequest $this->workValidationRequestRepository->find($event->getSubject()->getId());
  25.             $nextSteps $this->workflowHandler->nextStep($workValidationRequest);
  26.             $this->em->flush();
  27.             if (!$workValidationRequest) {
  28.                 $this->logger->alert(sprintf(
  29.                     'Fail to find Work Validation %s.',
  30.                     $event->getSubject()->getId()
  31.                 ));
  32.             }
  33.             $this->logger->alert(sprintf(
  34.                 'OS %s is in step %s.',
  35.                 $workValidationRequest->getId(),
  36.                 $workValidationRequest->getPlace()
  37.             ));
  38.             if (== count($nextSteps)) {
  39.                 $stepName reset($nextSteps);
  40.                 if ($this->workflowHandler->goToStep($workValidationRequest$stepName)) {
  41.                     $this->logger->alert(sprintf(
  42.                         'Success moving %s to step %s in workflow.',
  43.                         $event->getSubject()->getId(),
  44.                         $stepName
  45.                     ));
  46.                 } else {
  47.                     $this->logger->alert(sprintf(
  48.                         'Fail to move %s to step %s in workflow.',
  49.                         $event->getSubject()->getId(),
  50.                         $stepName
  51.                     ));
  52.                 }
  53.             } else {
  54.                 $this->logger->alert(sprintf(
  55.                     'Too many steps possible for %s in workflow.',
  56.                     $event->getSubject()->getId()
  57.                 ));
  58.             }
  59.         }
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             'workflow.work_validation_request.entered' => 'onChange',
  65.         ];
  66.     }
  67. }