src/EventSubscriber/Expediting/BookingScheduler.php line 50
<?php
namespace App\EventSubscriber\Expediting;
use App\Doctrine\Type\Finance\PayableStatus;
use App\Doctrine\Type\Scheduler\TaskStatus;
use App\Event\Expediting\ShipmentBookingEvent;
use App\Event\Finance\QuotationStatusUpdateEvent;
use App\EventSubscriber\TasksCancelerTrait;
use App\Repository\Scheduler\TaskRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
readonly class BookingScheduler implements EventSubscriberInterface
{
use TasksCancelerTrait;
public function __construct(
private EntityManagerInterface $entityManager,
private TaskRepository $taskRepository,
) {}
public static function getSubscribedEvents(): array
{
return [
QuotationStatusUpdateEvent::IDENTIFIER => ['schedule', 0],
ShipmentBookingEvent::IDENTIFIER => ['close', 100],
];
}
public function schedule(QuotationStatusUpdateEvent $event): void
{
$quotation = $event->getQuotation();
$currentSegment = $quotation->getShippingPlan()->getCurrentSegment();
if (null === $currentSegment) {
return;
}
if (PayableStatus::ACCEPTED === $quotation->getStatus()) {
$bookingTask = $this->taskRepository->getBookingTask($currentSegment, createIfNull: true);
$this->entityManager->persist($bookingTask);
} else {
$bookingTask = $this->taskRepository->getBookingTask($currentSegment);
$this->cancelTasks([$bookingTask]);
}
$this->entityManager->flush();
}
public function close(ShipmentBookingEvent $event): void
{
$segment = $event->getShippingSegment();
$bookingTask = $this->taskRepository->getBookingTask($segment, createIfNull: true);
$bookingTask->setStatus(TaskStatus::COMPLETED);
$bookingTask->setCompletedOn(new DateTime());
$this->entityManager->persist($bookingTask);
$this->entityManager->flush();
}
}