Completed
Push — master ( 41948a...5bb6bb )
by Łukasz
17:08 queued 13:33
created

PutSimpleItemToCartHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B handle() 0 24 1
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\ProductRepositoryInterface;
11
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
12
use Sylius\Component\Order\Processor\OrderProcessorInterface;
13
use Sylius\ShopApiPlugin\Command\PutSimpleItemToCart;
14
use Webmozart\Assert\Assert;
15
16
final class PutSimpleItemToCartHandler
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $cartRepository;
22
23
    /**
24
     * @var ProductRepositoryInterface
25
     */
26
    private $productRepository;
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 ProductRepositoryInterface $productRepository
51
     * @param CartItemFactoryInterface $cartItemFactory
52
     * @param OrderItemQuantityModifierInterface $orderItemModifier
53
     * @param OrderProcessorInterface $orderProcessor
54
     * @param ObjectManager $manager
55
     */
56
    public function __construct(
57
        OrderRepositoryInterface $cartRepository,
58
        ProductRepositoryInterface $productRepository,
59
        CartItemFactoryInterface $cartItemFactory,
60
        OrderItemQuantityModifierInterface $orderItemModifier,
61
        OrderProcessorInterface $orderProcessor,
62
        ObjectManager $manager
63
    ) {
64
        $this->cartRepository = $cartRepository;
65
        $this->productRepository = $productRepository;
66
        $this->cartItemFactory = $cartItemFactory;
67
        $this->orderItemModifier = $orderItemModifier;
68
        $this->orderProcessor = $orderProcessor;
69
        $this->manager = $manager;
70
    }
71
72
    public function handle(PutSimpleItemToCart $putSimpleItemToCart)
73
    {
74
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $putSimpleItemToCart->token()]);
75
76
        Assert::notNull($cart, 'Cart has not been found');
77
78
        /** @var ProductInterface $product */
79
        $product = $this->productRepository->findOneBy(['code' => $putSimpleItemToCart->product()]);
80
81
        Assert::notNull($product, 'Product has not been found');
82
83
        $productVariant = $product->getVariants()[0];
84
85
        /** @var OrderItemInterface $cartItem */
86
        $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...
87
        $cartItem->setVariant($productVariant);
88
        $this->orderItemModifier->modify($cartItem, $putSimpleItemToCart->quantity());
89
90
        $cart->addItem($cartItem);
91
92
        $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...
93
94
        $this->manager->persist($cart);
0 ignored issues
show
Bug introduced by
It seems like $cart defined by $this->cartRepository->f...leItemToCart->token())) on line 74 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...
95
    }
96
}
97