PutOptionBasedConfigurableItemToCartHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handle() 16 16 1
A getVariant() 0 10 3
A areOptionsMatched() 0 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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());
0 ignored issues
show
Bug introduced by
It seems like $productVariant defined by $this->getVariant($putCo...t->options(), $product) on line 55 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...
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