|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file has been created by developers from BitBag. |
| 5 | + * Feel free to contact us once you face any issues or want to start |
| 6 | + * You can find more information about us on https://bitbag.io and write us |
| 7 | + * an email on hello@bitbag.io. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace BitBag\SyliusProductBundlePlugin\Inventory\Operator; |
| 13 | + |
| 14 | +use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface; |
| 15 | +use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; |
| 16 | +use BitBag\SyliusProductBundlePlugin\Inventory\Checker\FeatureFlagCheckerInterface; |
| 17 | +use Doctrine\DBAL\LockMode; |
| 18 | +use Doctrine\ORM\EntityManagerInterface; |
| 19 | +use Sylius\Component\Core\Inventory\Operator\OrderInventoryOperatorInterface; |
| 20 | +use Sylius\Component\Core\Model\OrderInterface; |
| 21 | +use Sylius\Component\Core\Model\ProductVariantInterface; |
| 22 | +use Sylius\Component\Core\OrderPaymentStates; |
| 23 | + |
| 24 | +final class OrderInventoryOperator implements OrderInventoryOperatorInterface |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private readonly OrderInventoryOperatorInterface $decorated, |
| 28 | + private readonly EntityManagerInterface $productVariantManager, |
| 29 | + private readonly FeatureFlagCheckerInterface $featureFlagChecker, |
| 30 | + private readonly ProductBundleOrderInventoryOperatorInterface $productBundleOrderInventoryOperator, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function cancel(OrderInterface $order): void |
| 35 | + { |
| 36 | + $this->lockOrderProductVariants($order); |
| 37 | + |
| 38 | + if (!$this->featureFlagChecker->isEnabled()) { |
| 39 | + $this->decorated->cancel($order); |
| 40 | + } |
| 41 | + |
| 42 | + if (in_array( |
| 43 | + $order->getPaymentState(), |
| 44 | + [OrderPaymentStates::STATE_PAID, OrderPaymentStates::STATE_REFUNDED], |
| 45 | + true, |
| 46 | + )) { |
| 47 | + $this->productBundleOrderInventoryOperator->giveBack($order); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + $this->productBundleOrderInventoryOperator->release($order); |
| 53 | + } |
| 54 | + |
| 55 | + public function hold(OrderInterface $order): void |
| 56 | + { |
| 57 | + $this->lockOrderProductVariants($order); |
| 58 | + |
| 59 | + if (!$this->featureFlagChecker->isEnabled()) { |
| 60 | + $this->decorated->hold($order); |
| 61 | + |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $this->productBundleOrderInventoryOperator->hold($order); |
| 66 | + } |
| 67 | + |
| 68 | + public function sell(OrderInterface $order): void |
| 69 | + { |
| 70 | + $this->lockOrderProductVariants($order); |
| 71 | + |
| 72 | + if (!$this->featureFlagChecker->isEnabled()) { |
| 73 | + $this->decorated->sell($order); |
| 74 | + |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $this->productBundleOrderInventoryOperator->sell($order); |
| 79 | + } |
| 80 | + |
| 81 | + private function lockOrderProductVariants(OrderInterface $order): void |
| 82 | + { |
| 83 | + /** @var OrderItemInterface $orderItem */ |
| 84 | + foreach ($order->getItems() as $orderItem) { |
| 85 | + $this->lockOrderItemProductVariants($orderItem); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private function lockOrderItemProductVariants(OrderItemInterface $orderItem): void |
| 90 | + { |
| 91 | + /** @var ProductInterface $product */ |
| 92 | + $product = $orderItem->getProduct(); |
| 93 | + if ($this->featureFlagChecker->isEnabled() && $product->isBundle()) { |
| 94 | + $this->lockBundledOrderItemProductVariants($orderItem); |
| 95 | + } else { |
| 96 | + $this->lockOrderItemProductVariant($orderItem); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private function lockOrderItemProductVariant(OrderItemInterface $orderItem): void |
| 101 | + { |
| 102 | + $this->lockProductVariant($orderItem->getVariant()); |
| 103 | + } |
| 104 | + |
| 105 | + private function lockBundledOrderItemProductVariants(OrderItemInterface $orderItem): void |
| 106 | + { |
| 107 | + foreach ($orderItem->getProductBundleOrderItems() as $bundleOrderItem) { |
| 108 | + $this->lockProductVariant($bundleOrderItem->getProductVariant()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private function lockProductVariant(?ProductVariantInterface $variant): void |
| 113 | + { |
| 114 | + if (null === $variant) { |
| 115 | + throw new \InvalidArgumentException('Variant cannot be null'); |
| 116 | + } |
| 117 | + |
| 118 | + if (!$variant->isTracked()) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + $this->productVariantManager->lock($variant, LockMode::OPTIMISTIC, $variant->getVersion()); |
| 123 | + } |
| 124 | +} |
0 commit comments