RulesEligibilityChecker::isEligible()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0466
1
<?php
2
3
/*
4
 * This file is part of ibrand/discount.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Discount\Checkers;
13
14
use iBrand\Component\Discount\Contracts\DiscountContract;
15
use iBrand\Component\Discount\Contracts\DiscountSubjectContract;
16
use iBrand\Component\Discount\Models\Rule;
17
18
class RulesEligibilityChecker
19
{
20 4
    public function isEligible(DiscountSubjectContract $subject, DiscountContract $discount)
21
    {
22 4
        if (!$discount->hasRules()) {
23
            return true;
24
        }
25
26 4
        foreach ($discount->getRules() as $rule) {
27 4
            if (!$this->isEligibleToRule($subject, $rule, $discount)) {
28 4
                return false;
29
            }
30
        }
31
32 3
        return true;
33
    }
34
35 4
    protected function isEligibleToRule(DiscountSubjectContract $subject, Rule $rule, DiscountContract $discountContract)
36
    {
37 4
        $checker = app($rule->type);
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<iBrand\Component\Discount\Models\Rule>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
38
39 4
        $configuration = json_decode($rule->configuration, true);
0 ignored issues
show
Documentation introduced by
The property configuration does not exist on object<iBrand\Component\Discount\Models\Rule>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
41 4
        return $checker->isEligible($subject, $configuration, $discountContract);
42
    }
43
}
44