Completed
Pull Request — master (#196)
by Mateusz
03:21
created

PutSimpleItemToCartHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
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