Completed
Push — master ( 9e79a2...d4f851 )
by Andrii
03:09
created

Modifier::leasing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
18
/**
19
 * Fixed discount.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Modifier implements ChargeModifier
24
{
25
    use \hiqdev\php\billing\charge\modifiers\addons\WithReason;
26
    use \hiqdev\php\billing\charge\modifiers\addons\WithSince;
27
    use \hiqdev\php\billing\charge\modifiers\addons\WithTill;
28
    use \hiqdev\php\billing\charge\modifiers\addons\WithTerm;
29
30
    /**
31
     * @var AddonInterface[]
32
     */
33
    protected $addons;
34
35 25
    public function __construct(array $addons = [])
36
    {
37 25
        $this->addons = $addons;
38 25
    }
39
40
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
41
    {
42
        throw new \Exception('not finished modifier');
43
    }
44
45
    public function isSuitable(?ChargeInterface $charge, ActionInterface $action): bool
46
    {
47
        $month = $action->getTime()->modify('first day of this month midnight');
48
49
        $since = $this->getSince();
50
        if ($since && $since->getValue() > $month) {
51
            return false;
52
        }
53
54
        return true;
55
    }
56
57 3
    public function discount()
58
    {
59 3
        return new Discount($this->addons);
60
    }
61
62
    public function leasing()
63
    {
64
        return new Leasing($this->addons);
65
    }
66
67 23
    public function addAddon($name, $addon)
68
    {
69 23
        if (isset($this->addons[$name])) {
70
            throw new \Exception("'$name' is already set");
71
        }
72 23
        $res = $this->getNext();
73 23
        $res->addons[$name] = $addon;
74
75 23
        return $res;
76
    }
77
78 6
    public function getNext()
79
    {
80 6
        return new static($this->addons);
81
    }
82
83 19
    public function getAddon($name)
84
    {
85 19
        return empty($this->addons[$name]) ? null : $this->addons[$name];
86
    }
87
88 5
    public function checkPeriod(DateTimeImmutable $time)
89
    {
90 5
        $since = $this->getSince();
91 5
        if ($since && $since->getValue() > $time) {
92
            return false;
93
        }
94
95 5
        $till = $this->getTill();
96 5
        if ($till && $till->getValue() <= $time) {
97
            return false;
98
        }
99
100 5
        $term = $this->getTerm();
101 5
        if ($term) {
102 1
            if (!$since) {
103
                throw new \Exception('since must be set to use term');
104
            }
105 1
            if ($term->countPeriodsPassed($since->getValue(), $time) >= 1) {
106
                return false;
107
            }
108
        }
109
110 5
        return true;
111
    }
112
}
113