src/OrderProcessor/OrderShipmentProcessor.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\OrderProcessor;
  12. use Sylius\Component\Core\Model\OrderInterface;
  13. use Sylius\Component\Core\Model\ShipmentInterface;
  14. use Sylius\Component\Core\OrderCheckoutStates;
  15. use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
  16. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  17. use Sylius\Component\Resource\Factory\FactoryInterface;
  18. use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException;
  19. use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface;
  20. use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Webmozart\Assert\Assert;
  23. final class OrderShipmentProcessor implements OrderProcessorInterface
  24. {
  25.     private DefaultShippingMethodResolverInterface $defaultShippingMethodResolver;
  26.     private FactoryInterface $shipmentFactory;
  27.     private ?ShippingMethodsResolverInterface $shippingMethodsResolver;
  28.     public function __construct(
  29.         DefaultShippingMethodResolverInterface $defaultShippingMethodResolver,
  30.         FactoryInterface $shipmentFactory,
  31.         ?ShippingMethodsResolverInterface $shippingMethodsResolver null,
  32.         private RequestStack $requestStack
  33.     ) {
  34.         $this->defaultShippingMethodResolver $defaultShippingMethodResolver;
  35.         $this->shipmentFactory $shipmentFactory;
  36.         $this->shippingMethodsResolver $shippingMethodsResolver;
  37.         if (=== func_num_args() || null === $shippingMethodsResolver) {
  38.             @trigger_error(
  39.                 'Not passing ShippingMethodsResolverInterface explicitly is deprecated since 1.2 and will be prohibited in 2.0',
  40.                 \E_USER_DEPRECATED
  41.             );
  42.         }
  43.     }
  44.     public function process(BaseOrderInterface $order): void
  45.     {
  46.         /** @var OrderInterface $order */
  47.         Assert::isInstanceOf($orderOrderInterface::class);
  48.         if (!$order->canBeProcessed()) {
  49.             return;
  50.         }
  51.         if ($order->isEmpty() || !$order->isShippingRequired()) {
  52.             $order->removeShipments();
  53.             return;
  54.         }
  55.         if ($order->hasShipments()) {
  56.             $this->recalculateExistingShipmentWithProperMethod($order);
  57.             return;
  58.         }
  59.         $this->createNewOrderShipment($order);
  60.     }
  61.     public function createNewOrderShipment(OrderInterface $order): void
  62.     {
  63.         /** @var ShipmentInterface $shipment */
  64.         $shipment $this->shipmentFactory->createNew();
  65.         $shipment->setOrder($order);
  66.         try {
  67.             $this->processShipmentUnits($order$shipment);
  68.             $shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
  69.             $order->addShipment($shipment);
  70.         } catch (UnresolvedDefaultShippingMethodException $exception) {
  71.             foreach ($shipment->getUnits() as $unit) {
  72.                 $shipment->removeUnit($unit);
  73.             }
  74.         }
  75.     }
  76.     private function processShipmentUnits(BaseOrderInterface $orderShipmentInterface $shipment): void
  77.     {
  78.         foreach ($shipment->getUnits() as $unit) {
  79.             $shipment->removeUnit($unit);
  80.         }
  81.         /** @var OrderInterface $order */
  82.         Assert::isInstanceOf($orderOrderInterface::class);
  83.         foreach ($order->getItemUnits() as $itemUnit) {
  84.             if (null === $itemUnit->getShipment()) {
  85.                 $shipment->addUnit($itemUnit);
  86.             }
  87.         }
  88.     }
  89.     private function recalculateExistingShipmentWithProperMethod(OrderInterface $order): void
  90.     {
  91.         /** @var ShipmentInterface $shipment */
  92.         $shipment $order->getShipments()->first();
  93.         $this->processShipmentUnits($order$shipment);
  94.         if (null === $this->shippingMethodsResolver) {
  95.             return;
  96.         }
  97.         $orderCreationAdmin false;
  98.         if ($this->requestStack->getCurrentRequest()->getSession()->has('order_creation_admin_id') && $this->requestStack->getCurrentRequest()->getSession()->get('order_creation_admin_id') === $order->getId()) {
  99.             $orderCreationAdmin true;
  100.         }
  101.         if ($order->getCheckoutState() === OrderCheckoutStates::STATE_CART && !$orderCreationAdmin) {
  102.             try {
  103.                 $shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
  104.             } catch (UnresolvedDefaultShippingMethodException $exception) {
  105.                 return;
  106.             }
  107.         }
  108.         if (!in_array($shipment->getMethod(), $this->shippingMethodsResolver->getSupportedMethods($shipment), true)) {
  109.             try {
  110.                 $shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
  111.             } catch (UnresolvedDefaultShippingMethodException $exception) {
  112.                 return;
  113.             }
  114.         }
  115.     }
  116. }