RemoveItemFromCartHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\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);
52
    }
53
}
54