1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Checker; |
4
|
|
|
|
5
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
6
|
|
|
use Sylius\Component\Core\Model\PromotionInterface; |
7
|
|
|
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface; |
8
|
|
|
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface; |
9
|
|
|
use Sylius\Component\Promotion\Model\PromotionCouponInterface; |
10
|
|
|
use Sylius\Component\Promotion\Model\PromotionSubjectInterface; |
11
|
|
|
use Webmozart\Assert\Assert; |
12
|
|
|
|
13
|
|
|
final class PromotionCouponEligibilityChecker implements PromotionCouponEligibilityCheckerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var PromotionEligibilityCheckerInterface |
17
|
|
|
*/ |
18
|
|
|
private $promotionEligibilityChecker; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PromotionCouponEligibilityCheckerInterface |
22
|
|
|
*/ |
23
|
|
|
private $couponEligibilityChecker; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param PromotionEligibilityCheckerInterface $promotionEligibilityChecker |
27
|
|
|
* @param PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
PromotionEligibilityCheckerInterface $promotionEligibilityChecker, |
31
|
|
|
PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker |
32
|
|
|
) { |
33
|
|
|
$this->promotionEligibilityChecker = $promotionEligibilityChecker; |
34
|
|
|
$this->couponEligibilityChecker = $couponEligibilityChecker; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function isEligible(PromotionSubjectInterface $cart, PromotionCouponInterface $coupon) |
41
|
|
|
{ |
42
|
|
|
/** @var OrderInterface $cart */ |
43
|
|
|
Assert::isInstanceOf($cart, OrderInterface::class); |
44
|
|
|
|
45
|
|
|
/** @var PromotionInterface $promotion */ |
46
|
|
|
$promotion = $coupon->getPromotion(); |
47
|
|
|
|
48
|
|
|
$cart->setPromotionCoupon($coupon); |
49
|
|
|
|
50
|
|
|
$isEligible = |
51
|
|
|
$promotion->hasChannel($cart->getChannel()) |
52
|
|
|
&& $this->couponEligibilityChecker->isEligible($cart, $coupon) |
53
|
|
|
&& $this->promotionEligibilityChecker->isEligible($cart, $coupon->getPromotion()) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
$cart->setPromotionCoupon(null); |
57
|
|
|
|
58
|
|
|
return $isEligible; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|