1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Sylius\Component\Core\Factory\CartItemFactoryInterface; |
7
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
8
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
9
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
10
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
11
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
12
|
|
|
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
13
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
14
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
15
|
|
|
use Sylius\ShopApiPlugin\Command\PutOptionBasedConfigurableItemToCart; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
final class PutOptionBasedConfigurableItemToCartHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var OrderRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $cartRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ProductRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $productRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CartItemFactoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $cartItemFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var OrderItemQuantityModifierInterface |
37
|
|
|
*/ |
38
|
|
|
private $orderItemModifier; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var OrderProcessorInterface |
42
|
|
|
*/ |
43
|
|
|
private $orderProcessor; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ObjectManager |
47
|
|
|
*/ |
48
|
|
|
private $manager; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param OrderRepositoryInterface $cartRepository |
52
|
|
|
* @param ProductRepositoryInterface $productRepository |
53
|
|
|
* @param CartItemFactoryInterface $cartItemFactory |
54
|
|
|
* @param OrderItemQuantityModifierInterface $orderItemModifier |
55
|
|
|
* @param OrderProcessorInterface $orderProcessor |
56
|
|
|
* @param ObjectManager $manager |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
59
|
|
|
OrderRepositoryInterface $cartRepository, |
60
|
|
|
ProductRepositoryInterface $productRepository, |
61
|
|
|
CartItemFactoryInterface $cartItemFactory, |
62
|
|
|
OrderItemQuantityModifierInterface $orderItemModifier, |
63
|
|
|
OrderProcessorInterface $orderProcessor, |
64
|
|
|
ObjectManager $manager |
65
|
|
|
) { |
66
|
|
|
$this->cartRepository = $cartRepository; |
67
|
|
|
$this->productRepository = $productRepository; |
68
|
|
|
$this->cartItemFactory = $cartItemFactory; |
69
|
|
|
$this->orderItemModifier = $orderItemModifier; |
70
|
|
|
$this->orderProcessor = $orderProcessor; |
71
|
|
|
$this->manager = $manager; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param PutOptionBasedConfigurableItemToCart $putConfigurableItemToCart |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function handle(PutOptionBasedConfigurableItemToCart $putConfigurableItemToCart) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
/** @var OrderInterface $cart */ |
80
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]); |
81
|
|
|
|
82
|
|
|
Assert::notNull($cart, 'Cart has not been found'); |
83
|
|
|
|
84
|
|
|
/** @var ProductInterface $product */ |
85
|
|
|
$product = $this->productRepository->findOneByCode($putConfigurableItemToCart->product()); |
86
|
|
|
|
87
|
|
|
Assert::notNull($product, 'Product has not been found'); |
88
|
|
|
|
89
|
|
|
$productVariant = $this->getVariant($putConfigurableItemToCart->options(), $product); |
90
|
|
|
|
91
|
|
|
/** @var OrderItemInterface $cartItem */ |
92
|
|
|
$cartItem = $this->cartItemFactory->createForCart($cart); |
93
|
|
|
$cartItem->setVariant($productVariant); |
|
|
|
|
94
|
|
|
$this->orderItemModifier->modify($cartItem, $putConfigurableItemToCart->quantity()); |
95
|
|
|
|
96
|
|
|
$cart->addItem($cartItem); |
97
|
|
|
|
98
|
|
|
$this->orderProcessor->process($cart); |
99
|
|
|
|
100
|
|
|
$this->manager->persist($cart); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $options |
105
|
|
|
* @param ProductInterface $product |
106
|
|
|
* |
107
|
|
|
* @return null|ProductVariantInterface |
108
|
|
|
*/ |
109
|
|
|
private function getVariant(array $options, ProductInterface $product) |
110
|
|
|
{ |
111
|
|
|
foreach ($product->getVariants() as $variant) { |
112
|
|
|
if ($this->areOptionsMatched($options, $variant)) { |
113
|
|
|
return $variant; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
throw new \InvalidArgumentException('Variant could not be resolved'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $options |
122
|
|
|
* @param ProductVariantInterface $variant |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
private function areOptionsMatched(array $options, ProductVariantInterface $variant) |
127
|
|
|
{ |
128
|
|
|
foreach ($variant->getOptionValues() as $optionValue) { |
129
|
|
|
if (!isset($options[$optionValue->getOptionCode()]) || $optionValue->getCode() !== $options[$optionValue->getOptionCode()]) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.