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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 6
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\OrderItemInterface;
8
use Sylius\Component\Core\Model\ProductInterface;
9
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
10
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
11
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
12
use Sylius\Component\Order\Processor\OrderProcessorInterface;
13
use Sylius\ShopApiPlugin\Command\PutVariantBasedConfigurableItemToCart;
14
use Webmozart\Assert\Assert;
15
16
final class PutVariantBasedConfigurableItemToCartHandler
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $cartRepository;
22
23
    /**
24
     * @var ProductVariantRepositoryInterface
25
     */
26
    private $productVariantRepository;
27
28
    /**
29
     * @var CartItemFactoryInterface
30
     */
31
    private $cartItemFactory;
32
33
    /**
34
     * @var OrderItemQuantityModifierInterface
35
     */
36
    private $orderItemModifier;
37
38
    /**
39
     * @var OrderProcessorInterface
40
     */
41
    private $orderProcessor;
42
43
    /**
44
     * @var ObjectManager
45
     */
46
    private $manager;
47
48
    /**
49
     * @param OrderRepositoryInterface $cartRepository
50
     * @param ProductVariantRepositoryInterface $productVariantRepository
51
     * @param CartItemFactoryInterface $cartItemFactory
52
     * @param OrderItemQuantityModifierInterface $orderItemModifier
53
     * @param OrderProcessorInterface $orderProcessor
54
     * @param ObjectManager $manager
55
     */
56 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...
57
        OrderRepositoryInterface $cartRepository,
58
        ProductVariantRepositoryInterface $productVariantRepository,
59
        CartItemFactoryInterface $cartItemFactory,
60
        OrderItemQuantityModifierInterface $orderItemModifier,
61
        OrderProcessorInterface $orderProcessor,
62
        ObjectManager $manager
63
    ) {
64
        $this->cartRepository = $cartRepository;
65
        $this->productVariantRepository = $productVariantRepository;
66
        $this->cartItemFactory = $cartItemFactory;
67
        $this->orderItemModifier = $orderItemModifier;
68
        $this->orderProcessor = $orderProcessor;
69
        $this->manager = $manager;
70
    }
71
72
    /**
73
     * @param PutVariantBasedConfigurableItemToCart $putConfigurableItemToCart
74
     */
75 View Code Duplication
    public function handle(PutVariantBasedConfigurableItemToCart $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...
76
    {
77
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]);
78
79
        Assert::notNull($cart, 'Cart has not been found');
80
81
        /** @var ProductInterface $product */
82
        $productVariant = $this->productVariantRepository->findOneByCodeAndProductCode($putConfigurableItemToCart->productVariant(), $putConfigurableItemToCart->product());
83
84
        Assert::notNull($productVariant, 'Product variant has not been found');
85
86
        /** @var OrderItemInterface $cartItem */
87
        $cartItem = $this->cartItemFactory->createForCart($cart);
0 ignored issues
show
Documentation introduced by
$cart is of type object|null, but the function expects a object<Sylius\Component\...e\Model\OrderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
        $cartItem->setVariant($productVariant);
0 ignored issues
show
Documentation introduced by
$productVariant is of type object<Sylius\Component\...tVariantInterface>|null, but the function expects a object<Sylius\Component\...roductVariantInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
        $this->orderItemModifier->modify($cartItem, $putConfigurableItemToCart->quantity());
90
91
        $cart->addItem($cartItem);
92
93
        $this->orderProcessor->process($cart);
0 ignored issues
show
Documentation introduced by
$cart is of type object|null, but the function expects a object<Sylius\Component\...r\Model\OrderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
95
        $this->manager->persist($cart);
0 ignored issues
show
Bug introduced by
It seems like $cart defined by $this->cartRepository->f...mToCart->orderToken())) on line 77 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
    }
97
}
98