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