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

Modifier::getSince()   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 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