|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
6
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
7
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
8
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
|
9
|
|
|
use Sylius\ShopApiPlugin\Command\PutSimpleItemToCart; |
|
10
|
|
|
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface; |
|
11
|
|
|
use Webmozart\Assert\Assert; |
|
12
|
|
|
|
|
13
|
|
|
final class PutSimpleItemToCartHandler |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var OrderRepositoryInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $cartRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ProductRepositoryInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $productRepository; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var OrderModifierInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $orderModifier; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
OrderRepositoryInterface $cartRepository, |
|
32
|
|
|
ProductRepositoryInterface $productRepository, |
|
33
|
|
|
OrderModifierInterface $orderModifier |
|
34
|
|
|
) { |
|
35
|
|
|
$this->cartRepository = $cartRepository; |
|
36
|
|
|
$this->productRepository = $productRepository; |
|
37
|
|
|
$this->orderModifier = $orderModifier; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function handle(PutSimpleItemToCart $putSimpleItemToCart) |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var OrderInterface $cart */ |
|
43
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $putSimpleItemToCart->orderToken()]); |
|
44
|
|
|
|
|
45
|
|
|
Assert::notNull($cart, 'Cart has not been found'); |
|
46
|
|
|
|
|
47
|
|
|
/** @var ProductInterface $product */ |
|
48
|
|
|
$product = $this->productRepository->findOneBy(['code' => $putSimpleItemToCart->product()]); |
|
49
|
|
|
|
|
50
|
|
|
Assert::notNull($product, 'Product has not been found'); |
|
51
|
|
|
Assert::true($product->isSimple(), 'Product has to be simple'); |
|
52
|
|
|
|
|
53
|
|
|
$productVariant = $product->getVariants()[0]; |
|
54
|
|
|
|
|
55
|
|
|
$this->orderModifier->modify($cart, $productVariant, $putSimpleItemToCart->quantity()); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|