src/EventSubscriber/Expediting/FreightDocumentsUploadScheduler.php line 47

  1. <?php
  2. namespace App\EventSubscriber\Expediting;
  3. use App\Event\Sales\WorkOrderCancelationEvent;
  4. use App\Event\Sales\WorkOrderCreationEvent;
  5. use App\Event\Sales\WorkOrderEventInterface;
  6. use App\Event\Sales\WorkOrderReopeningEvent;
  7. use App\EventSubscriber\TasksCancelerTrait;
  8. use App\Repository\Scheduler\TaskRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. readonly class FreightDocumentsUploadScheduler implements EventSubscriberInterface
  12. {
  13.     use TasksCancelerTrait;
  14.     public function __construct(
  15.         private EntityManagerInterface $entityManager,
  16.         private TaskRepository         $taskRepository,
  17.     ) {}
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             WorkOrderCreationEvent::IDENTIFIER => ['handleScheduling'80],
  22.             WorkOrderReopeningEvent::IDENTIFIER => ['handleScheduling'80],
  23.             WorkOrderCancelationEvent::IDENTIFIER => ['handleCancelation'80],
  24.         ];
  25.     }
  26.     /** It is important to only execute this after work order is persisted and not after the freight entity is persisted */
  27.     public function handleScheduling(WorkOrderEventInterface $event): void
  28.     {
  29.         $workOrder $event->getWorkOrder();
  30.         foreach ($workOrder->getFreights() as $freight) {
  31.             $documentsGatheringTask $this->taskRepository->getDocumentsGatheringTask(freight$freightcreateIfNulltrue);
  32.             $documentsRequestTask $this->taskRepository->getDocumentsRequestTask(freight$freightcreateIfNulltrue);
  33.             $this->entityManager->persist($documentsGatheringTask);
  34.             $this->entityManager->persist($documentsRequestTask);
  35.         }
  36.         $this->entityManager->flush();
  37.     }
  38.     /** It is important to only execute this after work order is persisted and not after the freight entity is persisted */
  39.     public function handleCancelation(WorkOrderEventInterface $event): void
  40.     {
  41.         $workOrder $event->getWorkOrder();
  42.         $tasks = [];
  43.         foreach ($workOrder->getFreights() as $freight) {
  44.             $tasks []= $this->taskRepository->getDocumentsGatheringTask(freight$freight);
  45.             $tasks []= $this->taskRepository->getDocumentsRequestTask(freight$freight);
  46.         }
  47.         $this->cancelTasks($tasks);
  48.         $this->entityManager->flush();
  49.     }
  50. }