Completed
Push — master ( e1b51a...ef7e86 )
by Andrii
05:09
created

Modifier   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.29%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 85
rs 10
c 0
b 0
f 0
ccs 29
cts 34
cp 0.8529

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A modifyCharge() 0 4 1
A discount() 0 4 1
A reason() 0 4 1
A since() 0 4 1
A till() 0 4 1
A addAddon() 0 9 2
A getAddon() 0 4 2
A getReason() 0 4 1
A getSince() 0 4 1
A getTill() 0 4 1
B checkPeriod() 0 14 5
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\action\ActionInterface;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\charge\ChargeModifier;
17
use hiqdev\php\billing\charge\modifiers\addons\Reason;
18
use hiqdev\php\billing\charge\modifiers\addons\Since;
19
use hiqdev\php\billing\charge\modifiers\addons\Till;
20
21
/**
22
 * Fixed discount.
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class Modifier implements ChargeModifier
27
{
28
    const REASON = 'reason';
29
    const SINCE = 'since';
30
    const TILL = 'till';
31
32
    /**
33
     * @var AddonInterface[]
34
     */
35
    protected $addons;
36
37 15
    public function __construct(array $addons = [])
38
    {
39 15
        $this->addons = $addons;
40 15
    }
41
42
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
43
    {
44
        throw new \Exception('not finished modifier');
45
    }
46
47 3
    public function discount()
48
    {
49 3
        return new Discount($this->addons);
50
    }
51
52 5
    public function reason($text)
53
    {
54 5
        return $this->addAddon(self::REASON, new Reason($text));
55
    }
56
57 4
    public function since($time)
58
    {
59 4
        return $this->addAddon(self::SINCE, new Since($time));
60
    }
61
62 3
    public function till($time)
63
    {
64 3
        return $this->addAddon(self::TILL, new Till($time));
65
    }
66
67 7
    public function addAddon($name, $addon)
68
    {
69 7
        if (isset($this->addons[$name])) {
70
            throw new \Exception("'$name' is already set");
71
        }
72 7
        $this->addons[$name] = $addon;
73
74 7
        return $this;
75
    }
76
77 8
    public function getAddon($name)
78
    {
79 8
        return empty($this->addons[$name]) ? null : $this->addons[$name];
80
    }
81
82 6
    public function getReason()
83
    {
84 6
        return $this->getAddon(self::REASON);
85
    }
86
87 6
    public function getSince()
88
    {
89 6
        return $this->getAddon(self::SINCE);
90
    }
91
92 6
    public function getTill()
93
    {
94 6
        return $this->getAddon(self::TILL);
95
    }
96
    public function checkPeriod(DateTimeImmutable $time)
97 2
    {
98
        $since = $this->getSince();
99 2
        if ($since && $since->getValue() > $time) {
100 2
            return false;
101
        }
102
103
        $till = $this->getTill();
104 2
        if ($till && $till->getValue() <= $time) {
105 2
            return false;
106
        }
107
108
        return true;
109 2
    }
110
}
111