Completed
Pull Request — master (#290)
by
unknown
41:51
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Handler;
6
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\ProductVariantInterface;
9
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
10
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
11
use Sylius\Component\Product\Model\ProductInterface;
12
use Sylius\ShopApiPlugin\Command\PutOptionBasedConfigurableItemToCart;
13
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface;
14
use Webmozart\Assert\Assert;
15
16
final class PutOptionBasedConfigurableItemToCartHandler
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $cartRepository;
22
23
    /**
24
     * @var ProductRepositoryInterface
25
     */
26
    private $productRepository;
27
28
    /**
29
     * @var OrderModifierInterface
30
     */
31
    private $orderModifier;
32
33
    public function __construct(
34
        OrderRepositoryInterface $cartRepository,
35
        ProductRepositoryInterface $productRepository,
36
        OrderModifierInterface $orderModifier
37
    ) {
38
        $this->cartRepository = $cartRepository;
39
        $this->productRepository = $productRepository;
40
        $this->orderModifier = $orderModifier;
41
    }
42
43 View Code Duplication
    public function handle(PutOptionBasedConfigurableItemToCart $putConfigurableItemToCart)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
44
    {
45
        /** @var OrderInterface $cart */
46
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]);
47
48
        Assert::notNull($cart, 'Cart has not been found');
49
50
        /** @var ProductInterface $product */
51
        $product = $this->productRepository->findOneByCode($putConfigurableItemToCart->product());
52
53
        Assert::notNull($product, 'Product has not been found');
54
55
        $productVariant = $this->getVariant($putConfigurableItemToCart->options(), $product);
56
57
        $this->orderModifier->modify($cart, $productVariant, $putConfigurableItemToCart->quantity());
58
    }
59
60
    /**
61
     * @param array $options
62
     * @param ProductInterface $product
63
     *
64
     * @return ProductVariantInterface|null
65
     */
66
    private function getVariant(array $options, ProductInterface $product)
67
    {
68
        foreach ($product->getVariants() as $variant) {
69
            if ($this->areOptionsMatched($options, $variant)) {
70
                return $variant;
71
            }
72
        }
73
74
        throw new \InvalidArgumentException('Variant could not be resolved');
75
    }
76
77
    /**
78
     * @param array $options
79
     * @param ProductVariantInterface $variant
80
     *
81
     * @return bool
82
     */
83
    private function areOptionsMatched(array $options, ProductVariantInterface $variant)
84
    {
85
        foreach ($variant->getOptionValues() as $optionValue) {
86
            if (!isset($options[$optionValue->getOptionCode()]) || $optionValue->getCode() !== $options[$optionValue->getOptionCode()]) {
87
                return false;
88
            }
89
        }
90
91
        return true;
92
    }
93
}
94