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\ProductRepositoryInterface; |
11
|
|
|
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
12
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Command\PutSimpleItemToCart; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
final class PutSimpleItemToCartHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var OrderRepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $cartRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ProductRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $productRepository; |
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 ProductRepositoryInterface $productRepository |
51
|
|
|
* @param CartItemFactoryInterface $cartItemFactory |
52
|
|
|
* @param OrderItemQuantityModifierInterface $orderItemModifier |
53
|
|
|
* @param OrderProcessorInterface $orderProcessor |
54
|
|
|
* @param ObjectManager $manager |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
OrderRepositoryInterface $cartRepository, |
58
|
|
|
ProductRepositoryInterface $productRepository, |
59
|
|
|
CartItemFactoryInterface $cartItemFactory, |
60
|
|
|
OrderItemQuantityModifierInterface $orderItemModifier, |
61
|
|
|
OrderProcessorInterface $orderProcessor, |
62
|
|
|
ObjectManager $manager |
63
|
|
|
) { |
64
|
|
|
$this->cartRepository = $cartRepository; |
65
|
|
|
$this->productRepository = $productRepository; |
66
|
|
|
$this->cartItemFactory = $cartItemFactory; |
67
|
|
|
$this->orderItemModifier = $orderItemModifier; |
68
|
|
|
$this->orderProcessor = $orderProcessor; |
69
|
|
|
$this->manager = $manager; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function handle(PutSimpleItemToCart $putSimpleItemToCart) |
73
|
|
|
{ |
74
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $putSimpleItemToCart->token()]); |
75
|
|
|
|
76
|
|
|
Assert::notNull($cart, 'Cart has not been found'); |
77
|
|
|
|
78
|
|
|
/** @var ProductInterface $product */ |
79
|
|
|
$product = $this->productRepository->findOneBy(['code' => $putSimpleItemToCart->product()]); |
80
|
|
|
|
81
|
|
|
Assert::notNull($product, 'Product has not been found'); |
82
|
|
|
|
83
|
|
|
$productVariant = $product->getVariants()[0]; |
84
|
|
|
|
85
|
|
|
/** @var OrderItemInterface $cartItem */ |
86
|
|
|
$cartItem = $this->cartItemFactory->createForCart($cart); |
|
|
|
|
87
|
|
|
$cartItem->setVariant($productVariant); |
88
|
|
|
$this->orderItemModifier->modify($cartItem, $putSimpleItemToCart->quantity()); |
89
|
|
|
|
90
|
|
|
$cart->addItem($cartItem); |
91
|
|
|
|
92
|
|
|
$this->orderProcessor->process($cart); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->manager->persist($cart); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: