src/EventSubscriber/Expediting/ExpeditablesShippingStatusWatcher.php line 35

  1. <?php
  2. namespace App\EventSubscriber\Expediting;
  3. use App\Doctrine\Type\Expediting\ExpeditableStatus;
  4. use App\Doctrine\Type\Expediting\ShipmentType;
  5. use App\Doctrine\Type\Expediting\ShippingSegmentStatus;
  6. use App\Entity\Expediting\ShippingSegment;
  7. use App\Event\Expediting\ShipmentCancelationEvent;
  8. use App\Event\Expediting\ShipmentReschedulingEvent;
  9. use App\Event\Expediting\ShipmentStatusUpdateEvent;
  10. use App\Event\Expediting\ShippingSegmentEventInterface;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. readonly class ExpeditablesShippingStatusWatcher implements EventSubscriberInterface
  15. {
  16.     use ExpeditablesStatusUpdaterTrait;
  17.     public function __construct(
  18.         private readonly EntityManagerInterface   $entityManager,
  19.         private readonly EventDispatcherInterface $eventDispatcher,
  20.     ) {}
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ShipmentStatusUpdateEvent::IDENTIFIER => ['handleUpdate'0],
  25.             ShipmentReschedulingEvent::IDENTIFIER => ['handleUpdate'0],
  26.             ShipmentCancelationEvent::IDENTIFIER => ['handleCancelation'0],
  27.         ];
  28.     }
  29.     public function handleUpdate(ShippingSegmentEventInterface $event): void
  30.     {
  31.         $shippingSegment $event->getShippingSegment();
  32.         switch ($shippingSegment->getStatus()) {
  33.             case ShippingSegmentStatus::STARTED:
  34.                 $status in_array(ShipmentType::PICKUP$shippingSegment->getTypes())
  35.                     ? ExpeditableStatus::COLLECTED
  36.                     ExpeditableStatus::DEPARTED;
  37.                 break;
  38.             case ShippingSegmentStatus::COMPLETED:
  39.                 $status in_array(ShipmentType::PICKUP$shippingSegment->getTypes())
  40.                     ? ExpeditableStatus::RECEIVED
  41.                     ExpeditableStatus::ARRIVED;
  42.                 break;
  43.             case ShippingSegmentStatus::CANCELED:
  44.                 $this->eventDispatcher->dispatch(new ShipmentCancelationEvent($shippingSegment), ShipmentCancelationEvent::IDENTIFIER);
  45.                 return;
  46.             case ShippingSegmentStatus::PLANNED:
  47.             case ShippingSegmentStatus::APPROVED:
  48.             case ShippingSegmentStatus::BOOKED:
  49.                 return;
  50.         }
  51.         if ($status instanceof ExpeditableStatus) {
  52.             $this->process($shippingSegment$status);
  53.         }
  54.     }
  55.     public function handleCancelation(ShipmentCancelationEvent $event): void
  56.     {
  57.         $shippingSegment $event->getShippingSegment();
  58.         $status in_array(ShipmentType::PICKUP$shippingSegment->getTypes())
  59.             ? ExpeditableStatus::READY_FOR_PICKUP
  60.             ExpeditableStatus::MARSHALLED;
  61.         $this->process($shippingSegment$status);
  62.     }
  63.     private function process(ShippingSegment $shippingSegmentExpeditableStatus $status): void
  64.     {
  65.         $shippingPlan $shippingSegment->getShippingPlan();
  66.         if ($workOrder $shippingPlan->getWorkOrder()) {
  67.             $this->updateExpeditables($workOrder->getPackages(), $status);
  68.         } elseif ($shipment $shippingPlan->getShipment()) {
  69.             $this->updateExpeditables($shipment->getPackages(), $status);
  70.         }
  71.     }
  72. }