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