src/EventSubscriber/OrderSubscriber.php line 36

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Order;
  4. use App\Event\OrderCreatedEvent;
  5. use App\Service\InventoryService;
  6. use App\Service\OrderService;
  7. use Exception;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class OrderSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @param InventoryService $inventoryService
  13.      * @param OrderService $orderService
  14.      */
  15.     public function __construct(
  16.         private InventoryService $inventoryService,
  17.         private OrderService     $orderService
  18.     ) {}
  19.     /**
  20.      * @return array
  21.      */
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             OrderCreatedEvent::class => 'orderCreatedAction',
  26.         ];
  27.     }
  28.     /**
  29.      * @throws Exception
  30.      */
  31.     public function orderCreatedAction(OrderCreatedEvent $event)
  32.     {
  33.         $order $event->getOrder();
  34.         if ($order->isInvalidDeliveryDataStatus() || $order->isDuplicateStatus()) {
  35.             return;
  36.         }
  37.         if ($this->inventoryService->isProductsReservationCouldBeDone($order)) {
  38.             $this->inventoryService->orderProductsReservation($order);
  39.         } else {
  40.             $this->orderService->changeStatus($orderOrder::STATUS_NOT_ENOUGH_RESERVATION_QUANTITY);
  41.         }
  42.     }
  43. }