Completed
Pull Request — master (#290)
by
unknown
41:51
created

PromotionCouponEligibilityChecker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isEligible() 0 20 3
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())
0 ignored issues
show
Bug introduced by
It seems like $cart->getChannel() can be null; however, hasChannel() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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