src/EventSubscriber/Inventory/ShipmentStatusWatcher.php line 38

  1. <?php
  2. namespace App\EventSubscriber\Inventory;
  3. use App\Doctrine\Type\Expediting\ShippingSegmentStatus;
  4. use App\Entity\Inventory\Warehouse;
  5. use App\Event\Expediting\ShipmentCancelationEvent;
  6. use App\Event\Expediting\ShipmentReschedulingEvent;
  7. use App\Event\Expediting\ShipmentStatusUpdateEvent;
  8. use App\Event\Expediting\ShippingSegmentEventInterface;
  9. use App\Event\Inventory\ShipmentStockEntryEvent;
  10. use App\Event\Inventory\ShipmentStockExitEvent;
  11. use App\Processor\Expediting\ShipmentStatusConsumerTrait;
  12. use App\Repository\Scheduler\TaskRepository;
  13. use DateTimeInterface;
  14. use DomainException;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. readonly class ShipmentStatusWatcher implements EventSubscriberInterface
  18. {
  19.     use ShipmentStatusConsumerTrait;
  20.     public function __construct(
  21.         private TaskRepository           $taskRepository,
  22.         private EventDispatcherInterface $eventDispatcher,
  23.     ) {}
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ShipmentStatusUpdateEvent::IDENTIFIER => ['handleUpdate'0],
  28.             ShipmentReschedulingEvent::IDENTIFIER => ['handleUpdate', -100],
  29.             ShipmentCancelationEvent::IDENTIFIER => ['handleCancelation', -100],
  30.         ];
  31.     }
  32.     public function handleUpdate(ShippingSegmentEventInterface $event): void
  33.     {
  34.         $shippingSegment $event->getShippingSegment();
  35.         if (
  36.             $shippingSegment->getPlaceOfDischarge() instanceof Warehouse &&
  37.             ShippingSegmentStatus::COMPLETED === $shippingSegment->getStatus()
  38.         ) {
  39.             // If ShippingSegment status is completed and destination is a hub, we need to log packages stock entries
  40.             $completedOn $this->getArrivalTask($shippingSegment)->getCompletedOn();
  41.             if (!$completedOn instanceof DateTimeInterface) {
  42.                 throw new DomainException('ShipmentStockEntryEvent fired without an completion date.');
  43.             }
  44.             $this->eventDispatcher->dispatch(new ShipmentStockEntryEvent($shippingSegment$completedOn), ShipmentStockEntryEvent::IDENTIFIER);
  45.         } elseif (
  46.             $shippingSegment->getPlaceOfLoading() instanceof Warehouse &&
  47.             ShippingSegmentStatus::STARTED === $shippingSegment->getStatus()
  48.         ) {
  49.             // If ShippingSegment status is started and loading place is a hub, we need to log packages stock exists
  50.             $completedOn $this->getDepartureTask($shippingSegment)->getCompletedOn();
  51.             if (!$completedOn instanceof DateTimeInterface) {
  52.                 throw new DomainException('ShipmentStockExitEvent fired without an completion date.');
  53.             }
  54.             $this->eventDispatcher->dispatch(new ShipmentStockExitEvent($shippingSegment$completedOn), ShipmentStockExitEvent::IDENTIFIER);
  55.         }
  56.     }
  57. }