DeleteCouponHandler::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\DeleteCoupon;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
18
use Sylius\Component\Order\Processor\OrderProcessorInterface;
19
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
20
use Webmozart\Assert\Assert;
21
22
final class DeleteCouponHandler implements MessageHandlerInterface
23
{
24
    /** @var OrderRepositoryInterface */
25
    private $orderRepository;
26
27
    /** @var OrderProcessorInterface */
28
    private $orderProcessor;
29
30
    public function __construct(
31
        OrderRepositoryInterface $orderRepository,
32
        OrderProcessorInterface $orderProcessor
33
    ) {
34
        $this->orderRepository = $orderRepository;
35
        $this->orderProcessor = $orderProcessor;
36
    }
37
38
    public function __invoke(DeleteCoupon $deleteCoupon): void
39
    {
40
        /** @var OrderInterface $cart */
41
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $deleteCoupon->cartId()]);
42
43
        Assert::notNull($cart, sprintf('Cart with token value of %s has not been found.', $deleteCoupon->cartId()));
44
45
        $cart->setPromotionCoupon(null);
46
47
        $this->orderProcessor->process($cart);
48
    }
49
}
50