Completed
Pull Request — master (#290)
by
unknown
41:51
created

RemoveItemFromCartHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

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