ApplyCouponHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 17 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\ApplyCoupon;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Core\Model\PromotionCouponInterface;
18
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
19
use Sylius\Component\Order\Processor\OrderProcessorInterface;
20
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface;
21
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
use Webmozart\Assert\Assert;
24
25
final class ApplyCouponHandler implements MessageHandlerInterface
26
{
27
    /** @var OrderRepositoryInterface */
28
    private $orderRepository;
29
30
    /** @var PromotionCouponRepositoryInterface */
31
    private $couponRepository;
32
33
    /** @var OrderProcessorInterface */
34
    private $orderProcessor;
35
36
    /** @var PromotionCouponEligibilityCheckerInterface */
37
    private $couponEligibilityChecker;
38
39
    public function __construct(
40
        OrderRepositoryInterface $orderRepository,
41
        PromotionCouponRepositoryInterface $couponRepository,
42
        OrderProcessorInterface $orderProcessor,
43
        PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
44
    ) {
45
        $this->orderRepository = $orderRepository;
46
        $this->couponRepository = $couponRepository;
47
        $this->orderProcessor = $orderProcessor;
48
        $this->couponEligibilityChecker = $couponEligibilityChecker;
49
    }
50
51
    public function __invoke(ApplyCoupon $applyCoupon): void
52
    {
53
        /** @var OrderInterface $cart */
54
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $applyCoupon->cartId()]);
55
56
        Assert::notNull($cart, sprintf('Cart with token value of %s has not been found.', $applyCoupon->cartId()));
57
58
        /** @var PromotionCouponInterface $coupon */
59
        $coupon = $this->couponRepository->findOneBy(['code' => $applyCoupon->coupon()]);
60
61
        Assert::notNull($coupon, sprintf('Coupon with code %s has not been found.', $applyCoupon->coupon()));
62
        Assert::true($this->couponEligibilityChecker->isEligible($cart, $coupon));
63
64
        $cart->setPromotionCoupon($coupon);
65
66
        $this->orderProcessor->process($cart);
67
    }
68
}
69