src/EventSubscriber/Inventory/ShipmentStatusWatcher.php line 38
<?php
namespace App\EventSubscriber\Inventory;
use App\Doctrine\Type\Expediting\ShippingSegmentStatus;
use App\Entity\Inventory\Warehouse;
use App\Event\Expediting\ShipmentCancelationEvent;
use App\Event\Expediting\ShipmentReschedulingEvent;
use App\Event\Expediting\ShipmentStatusUpdateEvent;
use App\Event\Expediting\ShippingSegmentEventInterface;
use App\Event\Inventory\ShipmentStockEntryEvent;
use App\Event\Inventory\ShipmentStockExitEvent;
use App\Processor\Expediting\ShipmentStatusConsumerTrait;
use App\Repository\Scheduler\TaskRepository;
use DateTimeInterface;
use DomainException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
readonly class ShipmentStatusWatcher implements EventSubscriberInterface
{
use ShipmentStatusConsumerTrait;
public function __construct(
private TaskRepository $taskRepository,
private EventDispatcherInterface $eventDispatcher,
) {}
public static function getSubscribedEvents(): array
{
return [
ShipmentStatusUpdateEvent::IDENTIFIER => ['handleUpdate', 0],
ShipmentReschedulingEvent::IDENTIFIER => ['handleUpdate', -100],
ShipmentCancelationEvent::IDENTIFIER => ['handleCancelation', -100],
];
}
public function handleUpdate(ShippingSegmentEventInterface $event): void
{
$shippingSegment = $event->getShippingSegment();
if (
$shippingSegment->getPlaceOfDischarge() instanceof Warehouse &&
ShippingSegmentStatus::COMPLETED === $shippingSegment->getStatus()
) {
// If ShippingSegment status is completed and destination is a hub, we need to log packages stock entries
$completedOn = $this->getArrivalTask($shippingSegment)->getCompletedOn();
if (!$completedOn instanceof DateTimeInterface) {
throw new DomainException('ShipmentStockEntryEvent fired without an completion date.');
}
$this->eventDispatcher->dispatch(new ShipmentStockEntryEvent($shippingSegment, $completedOn), ShipmentStockEntryEvent::IDENTIFIER);
} elseif (
$shippingSegment->getPlaceOfLoading() instanceof Warehouse &&
ShippingSegmentStatus::STARTED === $shippingSegment->getStatus()
) {
// If ShippingSegment status is started and loading place is a hub, we need to log packages stock exists
$completedOn = $this->getDepartureTask($shippingSegment)->getCompletedOn();
if (!$completedOn instanceof DateTimeInterface) {
throw new DomainException('ShipmentStockExitEvent fired without an completion date.');
}
$this->eventDispatcher->dispatch(new ShipmentStockExitEvent($shippingSegment, $completedOn), ShipmentStockExitEvent::IDENTIFIER);
}
}
}