1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\SyliusShopApiPlugin\Checker; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
8
|
|
|
use Sylius\Component\Core\Model\PromotionInterface; |
9
|
|
|
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface; |
10
|
|
|
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface; |
11
|
|
|
use Sylius\Component\Promotion\Model\PromotionCouponInterface; |
12
|
|
|
use Sylius\Component\Promotion\Model\PromotionSubjectInterface; |
13
|
|
|
use Webmozart\Assert\Assert; |
14
|
|
|
|
15
|
|
|
final class PromotionCouponEligibilityChecker implements PromotionCouponEligibilityCheckerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var PromotionEligibilityCheckerInterface |
19
|
|
|
*/ |
20
|
|
|
private $promotionEligibilityChecker; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PromotionCouponEligibilityCheckerInterface |
24
|
|
|
*/ |
25
|
|
|
private $couponEligibilityChecker; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PromotionEligibilityCheckerInterface $promotionEligibilityChecker |
29
|
|
|
* @param PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
PromotionEligibilityCheckerInterface $promotionEligibilityChecker, |
33
|
|
|
PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker |
34
|
|
|
) { |
35
|
|
|
$this->promotionEligibilityChecker = $promotionEligibilityChecker; |
36
|
|
|
$this->couponEligibilityChecker = $couponEligibilityChecker; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function isEligible(PromotionSubjectInterface $cart, PromotionCouponInterface $coupon): bool |
43
|
|
|
{ |
44
|
|
|
/** @var OrderInterface $cart */ |
45
|
|
|
Assert::isInstanceOf($cart, OrderInterface::class); |
46
|
|
|
|
47
|
|
|
/** @var PromotionInterface $promotion */ |
48
|
|
|
$promotion = $coupon->getPromotion(); |
49
|
|
|
|
50
|
|
|
$cart->setPromotionCoupon($coupon); |
51
|
|
|
|
52
|
|
|
$isEligible = |
53
|
|
|
$promotion->hasChannel($cart->getChannel()) |
|
|
|
|
54
|
|
|
&& $this->couponEligibilityChecker->isEligible($cart, $coupon) |
55
|
|
|
&& $this->promotionEligibilityChecker->isEligible($cart, $coupon->getPromotion()) |
56
|
|
|
; |
57
|
|
|
|
58
|
|
|
$cart->setPromotionCoupon(null); |
59
|
|
|
|
60
|
|
|
return $isEligible; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: