ValidPromotionCouponCodeValidator::validate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Validator;
6
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\PromotionCouponInterface;
9
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
10
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface;
11
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;
12
use Sylius\ShopApiPlugin\Request\AddCouponRequest;
13
use Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
use Webmozart\Assert\Assert;
16
17
final class ValidPromotionCouponCodeValidator extends ConstraintValidator
18
{
19
    /**
20
     * @var OrderRepositoryInterface
21
     */
22
    private $orderRepository;
23
24
    /**
25
     * @var PromotionCouponRepositoryInterface
26
     */
27
    private $promotionCouponRepository;
28
29
    /**
30
     * @var PromotionCouponEligibilityCheckerInterface
31
     */
32
    private $couponEligibilityChecker;
33
34
    /**
35
     * @param OrderRepositoryInterface $orderRepository
36
     * @param PromotionCouponRepositoryInterface $promotionCouponRepository
37
     * @param PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
38
     */
39
    public function __construct(
40
        OrderRepositoryInterface $orderRepository,
41
        PromotionCouponRepositoryInterface $promotionCouponRepository,
42
        PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
43
    ) {
44
        $this->orderRepository = $orderRepository;
45
        $this->promotionCouponRepository = $promotionCouponRepository;
46
        $this->couponEligibilityChecker = $couponEligibilityChecker;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function validate($request, Constraint $constraint)
53
    {
54
        /** @var AddCouponRequest $request */
55
        Assert::isInstanceOf($request, AddCouponRequest::class);
56
57
        try {
58
            $command = $request->getCommand();
59
        } catch (\InvalidArgumentException $exception) {
60
            $this->buildViolation($constraint);
61
62
            return;
63
        }
64
65
        /** @var OrderInterface $cart */
66
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $command->orderToken(), 'state' => OrderInterface::STATE_CART]);
67
68
        if (null === $cart) {
69
            return;
70
        }
71
72
        /** @var PromotionCouponInterface $coupon */
73
        $coupon = $this->promotionCouponRepository->findOneBy(['code' => $command->couponCode()]);
74
75
        if (null === $coupon || !$this->couponEligibilityChecker->isEligible($cart, $coupon)) {
76
            $this->buildViolation($constraint);
77
78
            return;
79
        }
80
    }
81
82
    /**
83
     * @param Constraint $constraint
84
     */
85
    private function buildViolation(Constraint $constraint)
86
    {
87
        $this->context
88
            ->buildViolation($constraint->message)
89
            ->atPath('coupon')
90
            ->addViolation()
91
        ;
92
    }
93
}
94