PutSimpleItemToCartHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 45
rs 10

2 Methods

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