src/EventSubscriber/Expediting/BookingScheduler.php line 32

  1. <?php
  2. namespace App\EventSubscriber\Expediting;
  3. use App\Doctrine\Type\Finance\PayableStatus;
  4. use App\Doctrine\Type\Scheduler\TaskStatus;
  5. use App\Event\Expediting\ShipmentBookingEvent;
  6. use App\Event\Finance\QuotationStatusUpdateEvent;
  7. use App\EventSubscriber\TasksCancelerTrait;
  8. use App\Repository\Scheduler\TaskRepository;
  9. use DateTime;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. readonly class BookingScheduler implements EventSubscriberInterface
  13. {
  14.     use TasksCancelerTrait;
  15.     public function __construct(
  16.         private EntityManagerInterface $entityManager,
  17.         private TaskRepository         $taskRepository,
  18.     ) {}
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             QuotationStatusUpdateEvent::IDENTIFIER => ['schedule'0],
  23.             ShipmentBookingEvent::IDENTIFIER => ['close'100],
  24.         ];
  25.     }
  26.     public function schedule(QuotationStatusUpdateEvent $event): void
  27.     {
  28.         $quotation $event->getQuotation();
  29.         $currentSegment $quotation->getShippingPlan()->getCurrentSegment();
  30.         if (null === $currentSegment) {
  31.             return;
  32.         }
  33.         if (PayableStatus::ACCEPTED === $quotation->getStatus()) {
  34.             $bookingTask $this->taskRepository->getBookingTask($currentSegmentcreateIfNulltrue);
  35.             $this->entityManager->persist($bookingTask);
  36.         } else {
  37.             $bookingTask $this->taskRepository->getBookingTask($currentSegment);
  38.             $this->cancelTasks([$bookingTask]);
  39.         }
  40.         $this->entityManager->flush();
  41.     }
  42.     public function close(ShipmentBookingEvent $event): void
  43.     {
  44.         $segment $event->getShippingSegment();
  45.         $bookingTask $this->taskRepository->getBookingTask($segmentcreateIfNulltrue);
  46.         $bookingTask->setStatus(TaskStatus::COMPLETED);
  47.         $bookingTask->setCompletedOn(new DateTime());
  48.         $this->entityManager->persist($bookingTask);
  49.         $this->entityManager->flush();
  50.     }
  51. }