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\OrderItemInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
9
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
10
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
11
|
|
|
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
12
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Command\PutVariantBasedConfigurableItemToCart; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
final class PutVariantBasedConfigurableItemToCartHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var OrderRepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $cartRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ProductVariantRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $productVariantRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CartItemFactoryInterface |
30
|
|
|
*/ |
31
|
|
|
private $cartItemFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var OrderItemQuantityModifierInterface |
35
|
|
|
*/ |
36
|
|
|
private $orderItemModifier; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var OrderProcessorInterface |
40
|
|
|
*/ |
41
|
|
|
private $orderProcessor; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ObjectManager |
45
|
|
|
*/ |
46
|
|
|
private $manager; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param OrderRepositoryInterface $cartRepository |
50
|
|
|
* @param ProductVariantRepositoryInterface $productVariantRepository |
51
|
|
|
* @param CartItemFactoryInterface $cartItemFactory |
52
|
|
|
* @param OrderItemQuantityModifierInterface $orderItemModifier |
53
|
|
|
* @param OrderProcessorInterface $orderProcessor |
54
|
|
|
* @param ObjectManager $manager |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
57
|
|
|
OrderRepositoryInterface $cartRepository, |
58
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
59
|
|
|
CartItemFactoryInterface $cartItemFactory, |
60
|
|
|
OrderItemQuantityModifierInterface $orderItemModifier, |
61
|
|
|
OrderProcessorInterface $orderProcessor, |
62
|
|
|
ObjectManager $manager |
63
|
|
|
) { |
64
|
|
|
$this->cartRepository = $cartRepository; |
65
|
|
|
$this->productVariantRepository = $productVariantRepository; |
66
|
|
|
$this->cartItemFactory = $cartItemFactory; |
67
|
|
|
$this->orderItemModifier = $orderItemModifier; |
68
|
|
|
$this->orderProcessor = $orderProcessor; |
69
|
|
|
$this->manager = $manager; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param PutVariantBasedConfigurableItemToCart $putConfigurableItemToCart |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function handle(PutVariantBasedConfigurableItemToCart $putConfigurableItemToCart) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]); |
78
|
|
|
|
79
|
|
|
Assert::notNull($cart, 'Cart has not been found'); |
80
|
|
|
|
81
|
|
|
/** @var ProductInterface $product */ |
82
|
|
|
$productVariant = $this->productVariantRepository->findOneByCodeAndProductCode($putConfigurableItemToCart->productVariant(), $putConfigurableItemToCart->product()); |
83
|
|
|
|
84
|
|
|
Assert::notNull($productVariant, 'Product variant has not been found'); |
85
|
|
|
|
86
|
|
|
/** @var OrderItemInterface $cartItem */ |
87
|
|
|
$cartItem = $this->cartItemFactory->createForCart($cart); |
|
|
|
|
88
|
|
|
$cartItem->setVariant($productVariant); |
|
|
|
|
89
|
|
|
$this->orderItemModifier->modify($cartItem, $putConfigurableItemToCart->quantity()); |
90
|
|
|
|
91
|
|
|
$cart->addItem($cartItem); |
92
|
|
|
|
93
|
|
|
$this->orderProcessor->process($cart); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$this->manager->persist($cart); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.