Completed
Push — master ( 66b796...ebbacf )
by Kamil
17:43 queued 10:16
created

ChangeItemQuantityHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
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\Modifier\OrderItemQuantityModifierInterface;
9
use Sylius\Component\Order\Processor\OrderProcessorInterface;
10
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface;
11
use Sylius\ShopApiPlugin\Command\ChangeItemQuantity;
12
use Webmozart\Assert\Assert;
13
14 View Code Duplication
final class ChangeItemQuantityHandler
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...
15
{
16
    /**
17
     * @var OrderItemRepositoryInterface
18
     */
19
    private $orderItemRepository;
20
21
    /**
22
     * @var OrderRepositoryInterface
23
     */
24
    private $orderRepository;
25
26
    /**
27
     * @var OrderItemQuantityModifierInterface
28
     */
29
    private $orderItemModifier;
30
31
    /**
32
     * @var OrderProcessorInterface
33
     */
34
    private $orderProcessor;
35
36
    /**
37
     * @param OrderItemRepositoryInterface $orderItemRepository
38
     * @param OrderRepositoryInterface $orderRepository
39
     * @param OrderItemQuantityModifierInterface $orderItemModifier
40
     * @param OrderProcessorInterface $orderProcessor
41
     */
42
    public function __construct(
43
        OrderItemRepositoryInterface $orderItemRepository,
44
        OrderRepositoryInterface $orderRepository,
45
        OrderItemQuantityModifierInterface $orderItemModifier,
46
        OrderProcessorInterface $orderProcessor
47
    ) {
48
        $this->orderItemRepository = $orderItemRepository;
49
        $this->orderRepository = $orderRepository;
50
        $this->orderItemModifier = $orderItemModifier;
51
        $this->orderProcessor = $orderProcessor;
52
    }
53
54
    public function handle(ChangeItemQuantity $changeItemQuantity)
55
    {
56
        /** @var OrderInterface $order */
57
        $order = $this->orderRepository->findOneBy(['tokenValue' => $changeItemQuantity->orderToken(), 'state' => OrderInterface::STATE_CART]);
58
59
        Assert::notNull($order, sprintf('Cart with %s token has not been found.', $changeItemQuantity->orderToken()));
60
61
        /** @var OrderItemInterface $orderItem */
62
        $orderItem = $this->orderItemRepository->find($changeItemQuantity->itemIdentifier());
63
64
        Assert::notNull($orderItem, 'Cart item has not been found.');
65
        Assert::true($order->hasItem($orderItem), sprintf('Cart item with %s id does not exists', $changeItemQuantity->itemIdentifier()));
66
67
        $this->orderItemModifier->modify($orderItem, $changeItemQuantity->quantity());
68
69
        $this->orderProcessor->process($order);
70
    }
71
}
72