Completed
Push — master ( 9da703...f9ad19 )
by Dmitry
02:41
created

Modifier::getAddon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * PHP Billing Library
6
 *
7
 * @link      https://github.com/hiqdev/php-billing
8
 * @package   php-billing
9
 * @license   BSD-3-Clause
10
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
11
 */
12
13
namespace hiqdev\php\billing\charge\modifiers;
14
15
use DateTimeImmutable;
16
use hiqdev\php\billing\action\ActionInterface;
17
use hiqdev\php\billing\charge\ChargeInterface;
18
use hiqdev\php\billing\charge\ChargeModifier;
19
use hiqdev\php\billing\charge\modifiers\addons\Reason;
20
use hiqdev\php\billing\charge\modifiers\addons\Since;
21
use hiqdev\php\billing\charge\modifiers\addons\Till;
22
23
/**
24
 * Fixed discount.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class Modifier implements ChargeModifier
29
{
30
    /**
31
     * @var AddonInterface[]
32
     */
33
    protected $addons;
34
35
    /**
36
     * @var string[]|AddonInterface[]
37
     */
38
    protected $supportedAddons = [];
39
40 15
    public function __construct(array $addons = [])
41
    {
42 15
        $this->addons = $addons;
43
44 15
        $this->supportedAddons = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('reason' => \hiqde...ers\addons\Till::class) of type array<string,?> is incompatible with the declared type array<integer,string|obj...ifiers\AddonInterface>> of property $supportedAddons.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
            'reason' => Reason::class,
46
            'since' => Since::class,
47
            'till' => Till::class
48
        ];
49 15
    }
50
51 9
    public function __call($name, $params)
52
    {
53 9
        if (strncmp($name, 'get', 3) === 0) {
54 8
            return $this->getAddon(strtolower(substr($name, 3)));
55
        }
56 7
        if (isset($this->supportedAddons[$name])) {
57 7
            return $this->addAddon($name, new $this->supportedAddons[$name](...$params));
58
        }
59
60
        throw new \BadMethodCallException("Method \"$name\" is not supported");
61
    }
62
63
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
64
    {
65
        throw new \Exception('not finished modifier');
66
    }
67
68 7
    public function addAddon($name, $addon)
69
    {
70 7
        if (!isset($this->supportedAddons[$name])) {
71
            throw new \Exception("Addon \"$name\" is not supported by this class");
72
        }
73 7
        if (isset($this->addons[$name])) {
74
            throw new \Exception("Addon \"$name\" is already set");
75
        }
76 7
        $this->addons[$name] = $addon;
77
78 7
        return $this;
79
    }
80
81 8
    public function getAddon(string $name): ?AddonInterface
82
    {
83 8
        return $this->addons[$name] ?? null;
84
    }
85
86
    public function hasAddon(string $name): bool
87
    {
88
        return isset($this->addons[$name]);
89
    }
90
91 3
92
    public function discount()
93 3
    {
94
        return new Discount($this->addons);
95
    }
96 2
97
    public function checkPeriod(DateTimeImmutable $time)
98 2
    {
99 2
        $since = $this->getSince();
0 ignored issues
show
Documentation Bug introduced by
The method getSince does not exist on object<hiqdev\php\billin...rge\modifiers\Modifier>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
100
        if ($since && $since->getValue() > $time) {
101
            return false;
102
        }
103 2
104 2
        $till = $this->getTill();
0 ignored issues
show
Documentation Bug introduced by
The method getTill does not exist on object<hiqdev\php\billin...rge\modifiers\Modifier>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
        if ($till && $till->getValue() <= $time) {
106
            return false;
107
        }
108 2
109
        return true;
110
    }
111
}
112