Completed
Push — master ( 02f68d...d0258d )
by Paweł
03:13
created

PromotionCouponEligibilityChecker::isEligible()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
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