DeleteCartHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\CommandHandler\Cart;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\Cart\DeleteCart;
16
use BitBag\SyliusVueStorefrontPlugin\Sylius\Entity\Order\OrderItem;
17
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
18
use Sylius\Component\Order\Model\OrderInterface;
19
use Sylius\Component\Order\Processor\OrderProcessorInterface;
20
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface;
21
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
22
23
final class DeleteCartHandler implements MessageHandlerInterface
24
{
25
    /** @var OrderRepositoryInterface */
26
    private $orderRepository;
27
28
    /** @var OrderItemRepositoryInterface */
29
    private $orderItemRepository;
30
31
    /** @var OrderProcessorInterface */
32
    private $orderProcessor;
33
34
    public function __construct(
35
        OrderRepositoryInterface $orderRepository,
36
        OrderItemRepositoryInterface $orderItemRepository,
37
        OrderProcessorInterface $orderProcessor
38
    ) {
39
        $this->orderRepository = $orderRepository;
40
        $this->orderItemRepository = $orderItemRepository;
41
        $this->orderProcessor = $orderProcessor;
42
    }
43
44
    public function __invoke(DeleteCart $deleteCart): void
45
    {
46
        /** @var OrderInterface $order */
47
        $order = $this->orderRepository->findOneBy([
48
            'tokenValue' => $deleteCart->cartId(),
49
            'state' => OrderInterface::STATE_CART,
50
        ]);
51
52
        if (!$order) {
53
            return;
54
        }
55
56
        /** @var OrderItem|null $orderItem */
57
        $orderItem = $this->orderItemRepository->findOneBy(['id' => $deleteCart->cartItem()->getItemId()]);
58
59
        if ($orderItem) {
60
            $order->removeItem($orderItem);
61
            $this->orderItemRepository->remove($orderItem);
62
        }
63
64
        $this->orderProcessor->process($order);
65
    }
66
}
67