Completed
Push — master ( eebd57...9e79a2 )
by Andrii
07:00
created

Modifier::getTill()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 25
    public function __construct(array $addons = [])
38
    {
39 25
        $this->addons = $addons;
40 25
    }
41
42
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
43
    {
44
        throw new \Exception('not finished modifier');
45
    }
46
47 4
    public function isSuitable(?ChargeInterface $charge, ActionInterface $action): bool
48
    {
49 4
        $month = $action->getTime()->modify('first day of this month midnight');
50
51 4
        return $this->checkPeriod($month);
52
    }
53
54 3
    public function discount()
55
    {
56 3
        return new Discount($this->addons);
57
    }
58
59
    public function leasing()
60
    {
61
        return new Leasing($this->addons);
62
    }
63
64 7
    public function reason($text)
65
    {
66 7
        return $this->addAddon(self::REASON, new Reason($text));
67
    }
68
69 14
    public function since($time)
70
    {
71 14
        return $this->addAddon(self::SINCE, new Since($time));
72
    }
73
74 7
    public function till($time)
75
    {
76 7
        return $this->addAddon(self::TILL, new Till($time));
77
    }
78
79 23
    public function addAddon($name, $addon)
80
    {
81 23
        if (isset($this->addons[$name])) {
82
            throw new \Exception("'$name' is already set");
83
        }
84 23
        $res = $this->getNext();
85 23
        $res->addons[$name] = $addon;
86
87 23
        return $res;
88
    }
89
90 6
    public function getNext()
91
    {
92 6
        return new static($this->addons);
93
    }
94
95 19
    public function getAddon($name)
96
    {
97 19
        return empty($this->addons[$name]) ? null : $this->addons[$name];
98
    }
99
100 11
    public function getReason()
101
    {
102 11
        return $this->getAddon(self::REASON);
103
    }
104
105 12
    public function getSince()
106
    {
107 12
        return $this->getAddon(self::SINCE);
108
    }
109
110 12
    public function getTill()
111
    {
112 12
        return $this->getAddon(self::TILL);
113
    }
114
115 5
    public function checkPeriod(DateTimeImmutable $time)
116
    {
117 5
        $since = $this->getSince();
118 5
        if ($since && $since->getValue() > $time) {
119
            return false;
120
        }
121
122 5
        $till = $this->getTill();
123 5
        if ($till && $till->getValue() <= $time) {
124
            return false;
125
        }
126
127 5
        return true;
128
    }
129
}
130