Completed
Push — master ( 6ad5a2...634c9e )
by Łukasz
12s
created

RemoveItemFromCartHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handle() 0 17 1
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Handler;
4
5
use Sylius\Component\Core\Model\OrderInterface;
6
use Sylius\Component\Core\Model\OrderItemInterface;
7
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
8
use Sylius\Component\Order\Processor\OrderProcessorInterface;
9
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface;
10
use Sylius\ShopApiPlugin\Command\RemoveItemFromCart;
11
use Webmozart\Assert\Assert;
12
13
final class RemoveItemFromCartHandler
14
{
15
    /** @var OrderItemRepositoryInterface */
16
    private $orderItemRepository;
17
18
    /** @var OrderRepositoryInterface */
19
    private $orderRepository;
20
21
    /** @var OrderProcessorInterface */
22
    private $orderProcessor;
23
24
    public function __construct(
25
        OrderItemRepositoryInterface $orderItemRepository,
26
        OrderRepositoryInterface $orderRepository,
27
        OrderProcessorInterface $orderProcessor
28
    ) {
29
        $this->orderItemRepository = $orderItemRepository;
30
        $this->orderRepository = $orderRepository;
31
        $this->orderProcessor = $orderProcessor;
32
    }
33
34
    public function handle(RemoveItemFromCart $command): void
35
    {
36
        $order = $this->orderRepository->findOneBy(['tokenValue' => $command->orderToken(), 'state' => OrderInterface::STATE_CART]);
37
38
        Assert::notNull($order, sprintf('Cart with %s token has not been found.', $command->orderToken()));
39
40
        /** @var OrderItemInterface $orderItem */
41
        $orderItem = $this->orderItemRepository->find($command->itemIdentifier());
42
43
        Assert::notNull($orderItem, 'Cart item has not been found.');
44
        Assert::true($order->hasItem($orderItem), sprintf('Cart item with %s id does not exists', $command->itemIdentifier()));
45
46
        $order->removeItem($orderItem);
47
        $this->orderItemRepository->remove($orderItem);
48
49
        $this->orderProcessor->process($order);
0 ignored issues
show
Documentation introduced by
$order 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...
50
    }
51
}
52