1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
4
|
|
|
|
5
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
6
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
7
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
8
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
9
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
10
|
|
|
use Sylius\ShopApiPlugin\Command\PutOptionBasedConfigurableItemToCart; |
11
|
|
|
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface; |
12
|
|
|
use Webmozart\Assert\Assert; |
13
|
|
|
|
14
|
|
|
final class PutOptionBasedConfigurableItemToCartHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var OrderRepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $cartRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ProductRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $productRepository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var OrderModifierInterface |
28
|
|
|
*/ |
29
|
|
|
private $orderModifier; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
OrderRepositoryInterface $cartRepository, |
33
|
|
|
ProductRepositoryInterface $productRepository, |
34
|
|
|
OrderModifierInterface $orderModifier |
35
|
|
|
) { |
36
|
|
|
$this->cartRepository = $cartRepository; |
37
|
|
|
$this->productRepository = $productRepository; |
38
|
|
|
$this->orderModifier = $orderModifier; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function handle(PutOptionBasedConfigurableItemToCart $putConfigurableItemToCart) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
/** @var OrderInterface $cart */ |
44
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]); |
45
|
|
|
|
46
|
|
|
Assert::notNull($cart, 'Cart has not been found'); |
47
|
|
|
|
48
|
|
|
/** @var ProductInterface $product */ |
49
|
|
|
$product = $this->productRepository->findOneByCode($putConfigurableItemToCart->product()); |
50
|
|
|
|
51
|
|
|
Assert::notNull($product, 'Product has not been found'); |
52
|
|
|
|
53
|
|
|
$productVariant = $this->getVariant($putConfigurableItemToCart->options(), $product); |
54
|
|
|
|
55
|
|
|
$this->orderModifier->modify($cart, $productVariant, $putConfigurableItemToCart->quantity()); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $options |
60
|
|
|
* @param ProductInterface $product |
61
|
|
|
* |
62
|
|
|
* @return null|ProductVariantInterface |
63
|
|
|
*/ |
64
|
|
|
private function getVariant(array $options, ProductInterface $product) |
65
|
|
|
{ |
66
|
|
|
foreach ($product->getVariants() as $variant) { |
67
|
|
|
if ($this->areOptionsMatched($options, $variant)) { |
68
|
|
|
return $variant; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
throw new \InvalidArgumentException('Variant could not be resolved'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $options |
77
|
|
|
* @param ProductVariantInterface $variant |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
private function areOptionsMatched(array $options, ProductVariantInterface $variant) |
82
|
|
|
{ |
83
|
|
|
foreach ($variant->getOptionValues() as $optionValue) { |
84
|
|
|
if (!isset($options[$optionValue->getOptionCode()]) || $optionValue->getCode() !== $options[$optionValue->getOptionCode()]) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.