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

handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\ProductVariantInterface;
7
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
8
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
9
use Sylius\Component\Product\Model\ProductInterface;
10
use Sylius\ShopApiPlugin\Command\PutOptionBasedConfigurableItemToCart;
11
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface;
12
use Webmozart\Assert\Assert;
13
14
final class PutOptionBasedConfigurableItemToCartHandler
15
{
16
    /**
17
     * @var OrderRepositoryInterface
18
     */
19
    private $cartRepository;
20
21
    /**
22
     * @var ProductRepositoryInterface
23
     */
24
    private $productRepository;
25
26
    /**
27
     * @var OrderModifierInterface
28
     */
29
    private $orderModifier;
30
31
    public function __construct(
32
        OrderRepositoryInterface $cartRepository,
33
        ProductRepositoryInterface $productRepository,
34
        OrderModifierInterface $orderModifier
35
    ) {
36
        $this->cartRepository = $cartRepository;
37
        $this->productRepository = $productRepository;
38
        $this->orderModifier = $orderModifier;
39
    }
40
41 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...
42
    {
43
        /** @var OrderInterface $cart */
44
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]);
45
46
        Assert::notNull($cart, 'Cart has not been found');
47
48
        /** @var ProductInterface $product */
49
        $product = $this->productRepository->findOneByCode($putConfigurableItemToCart->product());
50
51
        Assert::notNull($product, 'Product has not been found');
52
53
        $productVariant = $this->getVariant($putConfigurableItemToCart->options(), $product);
54
55
        $this->orderModifier->modify($cart, $productVariant, $putConfigurableItemToCart->quantity());
0 ignored issues
show
Bug introduced by
It seems like $productVariant defined by $this->getVariant($putCo...t->options(), $product) on line 53 can be null; however, Sylius\ShopApiPlugin\Mod...fierInterface::modify() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
56
    }
57
58
    /**
59
     * @param array $options
60
     * @param ProductInterface $product
61
     *
62
     * @return null|ProductVariantInterface
63
     */
64
    private function getVariant(array $options, ProductInterface $product)
65
    {
66
        foreach ($product->getVariants() as $variant) {
67
            if ($this->areOptionsMatched($options, $variant)) {
68
                return $variant;
69
            }
70
        }
71
72
        throw new \InvalidArgumentException('Variant could not be resolved');
73
    }
74
75
    /**
76
     * @param array $options
77
     * @param ProductVariantInterface $variant
78
     *
79
     * @return bool
80
     */
81
    private function areOptionsMatched(array $options, ProductVariantInterface $variant)
82
    {
83
        foreach ($variant->getOptionValues() as $optionValue) {
84
            if (!isset($options[$optionValue->getOptionCode()]) || $optionValue->getCode() !== $options[$optionValue->getOptionCode()]) {
85
                return false;
86
            }
87
        }
88
89
        return true;
90
    }
91
}
92