|
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); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
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: