src/EventSubscriber/Shop/OrderSave/BasicOrderValidationSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Shop\OrderSave;
  3. use App\Entity\Shop\DeliveryMethod;
  4. use App\Entity\Shop\PaymentMethod;
  5. use App\Event\Shop\OrderSaveEvent;
  6. use App\Exception\Shop\OrderValidationException;
  7. use App\Shop\ShopManager;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  11.  */
  12. class BasicOrderValidationSubscriber implements EventSubscriberInterface
  13. {
  14.     protected ShopManager $shopManager;
  15.     public function __construct(ShopManager $shopManager)
  16.     {
  17.         $this->shopManager $shopManager;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             OrderSaveEvent::NAME => [
  23.                 ['shopEnabled'1000],
  24.                 ['isEmpty'999],
  25.                 ['isSellable'998],
  26.                 ['isEnoughQuantity'997],
  27.                 ['isDeliveryMethodSelected'996],
  28.                 ['isPaymentMethodSelected'995],
  29.             ]
  30.         ];
  31.     }
  32.     public function shopEnabled(OrderSaveEvent $event): void
  33.     {
  34.         if (!$this->shopManager->shopEnabled())
  35.             throw new OrderValidationException('Nie można utworzyć zamówienia');
  36.     }
  37.     public function isEmpty(OrderSaveEvent $event): void
  38.     {
  39.         if ($event->getOrder()->getProducts()->isEmpty())
  40.             throw new OrderValidationException('Zamówienie musi zawierać przynajmniej jeden produkt');
  41.     }
  42.     public function isSellable(OrderSaveEvent $event): void
  43.     {
  44.         foreach ($event->getOrder()->getProducts() as $product) {
  45.             if ($product->getProduct() == null || 
  46.                 (!$this->shopManager->isProduct($product->getProduct()->getContent()) &&
  47.                 !$this->shopManager->isAddition($product->getProduct()->getContent()))
  48.             )
  49.                 throw new OrderValidationException(sprintf('Produkt "%s" nie jest już dostępny'$product->getName()));
  50.         }
  51.     }
  52.     public function isEnoughQuantity(OrderSaveEvent $event): void
  53.     {
  54.         foreach ($event->getOrder()->getProducts() as $product) {
  55.             $baseProduct $product->getProduct();
  56.             if ($baseProduct->getQuantity() !== null) { // Check if quantity for product is set
  57.                 if ($baseProduct->getQuantity() < $product->getQuantity())
  58.                     throw new OrderValidationException(sprintf('Niewystarczająca ilość produktu "%s" w magazynie'$product->getName()));
  59.             }
  60.         }
  61.     }
  62.     public function isDeliveryMethodSelected(OrderSaveEvent $event): void
  63.     {
  64.         if (!$event->getOrder()->getDeliveryMethod() instanceof DeliveryMethod)
  65.             throw new OrderValidationException('Proszę wybrać metodę dostawy');
  66.     }
  67.     public function isPaymentMethodSelected(OrderSaveEvent $event): void
  68.     {
  69.         if (!$event->getOrder()->getPaymentMethod() instanceof PaymentMethod)
  70.             throw new OrderValidationException('Proszę wybrać metodę płatności');
  71.     }
  72. }