Completed
Pull Request — master (#132)
by Łukasz
04:54
created

PutOptionBasedConfigurableItemToCartHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 32.77 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 12
dl 39
loc 119
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
B handle() 24 25 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
namespace Sylius\ShopApiPlugin\Handler;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Sylius\Component\Core\Factory\CartItemFactoryInterface;
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\OrderItemInterface;
9
use Sylius\Component\Core\Model\ProductVariantInterface;
10
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
11
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
12
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
13
use Sylius\Component\Order\Processor\OrderProcessorInterface;
14
use Sylius\Component\Product\Model\ProductInterface;
15
use Sylius\ShopApiPlugin\Command\PutOptionBasedConfigurableItemToCart;
16
use Webmozart\Assert\Assert;
17
18
final class PutOptionBasedConfigurableItemToCartHandler
19
{
20
    /**
21
     * @var OrderRepositoryInterface
22
     */
23
    private $cartRepository;
24
25
    /**
26
     * @var ProductRepositoryInterface
27
     */
28
    private $productRepository;
29
30
    /**
31
     * @var CartItemFactoryInterface
32
     */
33
    private $cartItemFactory;
34
35
    /**
36
     * @var OrderItemQuantityModifierInterface
37
     */
38
    private $orderItemModifier;
39
40
    /**
41
     * @var OrderProcessorInterface
42
     */
43
    private $orderProcessor;
44
45
    /**
46
     * @var ObjectManager
47
     */
48
    private $manager;
49
50
    /**
51
     * @param OrderRepositoryInterface $cartRepository
52
     * @param ProductRepositoryInterface $productRepository
53
     * @param CartItemFactoryInterface $cartItemFactory
54
     * @param OrderItemQuantityModifierInterface $orderItemModifier
55
     * @param OrderProcessorInterface $orderProcessor
56
     * @param ObjectManager $manager
57
     */
58 View Code Duplication
    public function __construct(
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...
59
        OrderRepositoryInterface $cartRepository,
60
        ProductRepositoryInterface $productRepository,
61
        CartItemFactoryInterface $cartItemFactory,
62
        OrderItemQuantityModifierInterface $orderItemModifier,
63
        OrderProcessorInterface $orderProcessor,
64
        ObjectManager $manager
65
    ) {
66
        $this->cartRepository = $cartRepository;
67
        $this->productRepository = $productRepository;
68
        $this->cartItemFactory = $cartItemFactory;
69
        $this->orderItemModifier = $orderItemModifier;
70
        $this->orderProcessor = $orderProcessor;
71
        $this->manager = $manager;
72
    }
73
74
    /**
75
     * @param PutOptionBasedConfigurableItemToCart $putConfigurableItemToCart
76
     */
77 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...
78
    {
79
        /** @var OrderInterface $cart */
80
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]);
81
82
        Assert::notNull($cart, 'Cart has not been found');
83
84
        /** @var ProductInterface $product */
85
        $product = $this->productRepository->findOneByCode($putConfigurableItemToCart->product());
86
87
        Assert::notNull($product, 'Product has not been found');
88
89
        $productVariant = $this->getVariant($putConfigurableItemToCart->options(), $product);
90
91
        /** @var OrderItemInterface $cartItem */
92
        $cartItem = $this->cartItemFactory->createForCart($cart);
93
        $cartItem->setVariant($productVariant);
0 ignored issues
show
Bug introduced by
It seems like $productVariant defined by $this->getVariant($putCo...t->options(), $product) on line 89 can be null; however, Sylius\Component\Core\Mo...Interface::setVariant() 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...
94
        $this->orderItemModifier->modify($cartItem, $putConfigurableItemToCart->quantity());
95
96
        $cart->addItem($cartItem);
97
98
        $this->orderProcessor->process($cart);
99
100
        $this->manager->persist($cart);
101
    }
102
103
    /**
104
     * @param array $options
105
     * @param ProductInterface $product
106
     *
107
     * @return null|ProductVariantInterface
108
     */
109
    private function getVariant(array $options, ProductInterface $product)
110
    {
111
        foreach ($product->getVariants() as $variant) {
112
            if ($this->areOptionsMatched($options, $variant)) {
113
                return $variant;
114
            }
115
        }
116
117
        throw new \InvalidArgumentException('Variant could not be resolved');
118
    }
119
120
    /**
121
     * @param array $options
122
     * @param ProductVariantInterface $variant
123
     *
124
     * @return bool
125
     */
126
    private function areOptionsMatched(array $options, ProductVariantInterface $variant)
127
    {
128
        foreach ($variant->getOptionValues() as $optionValue) {
129
            if (!isset($options[$optionValue->getOptionCode()]) || $optionValue->getCode() !== $options[$optionValue->getOptionCode()]) {
130
                return false;
131
            }
132
        }
133
134
        return true;
135
    }
136
}
137