Completed
Push — master ( 3a4c95...865c46 )
by Andrii
02:08
created

Modifier::addAddon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2.032
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 hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\charge\ChargeInterface;
15
use hiqdev\php\billing\charge\ChargeModifier;
16
use hiqdev\php\billing\charge\modifiers\addons\Reason;
17
use hiqdev\php\billing\charge\modifiers\addons\Since;
18
use hiqdev\php\billing\charge\modifiers\addons\Till;
19
20
/**
21
 * Fixed discount.
22
 *
23
 * @author Andrii Vasyliev <[email protected]>
24
 */
25
class Modifier implements ChargeModifier
26
{
27
    const REASON = 'reason';
28
    const SINCE = 'since';
29
    const TILL = 'till';
30
31
    /**
32
     * @var AddonInterface[]
33
     */
34
    protected $addons;
35
36 12
    public function __construct(array $addons = [])
37
    {
38 12
        $this->addons = $addons;
39 12
    }
40
41
    public function modifyCharge(ChargeInterface $charge, ActionInterface $action): array
42
    {
43
        throw new \Exception('not finished modifier');
44
    }
45
46 3
    public function discount()
47
    {
48 3
        return new Discount($this->addons);
49
    }
50
51 4
    public function reason($text)
52
    {
53 4
        return $this->addAddon(self::REASON, new Reason($text));
54
    }
55
56 3
    public function since($time)
57
    {
58 3
        return $this->addAddon(self::SINCE, new Since($time));
59
    }
60
61 3
    public function till($time)
62
    {
63 3
        return $this->addAddon(self::TILL, new Till($time));
64
    }
65
66 6
    public function addAddon($name, $addon)
67
    {
68 6
        if (isset($this->addons[$name])) {
69
            throw new \Exception("'$name' is already set");
70
        }
71 6
        $this->addons[$name] = $addon;
72
73 6
        return $this;
74
    }
75
76 5
    public function getAddon($name)
77
    {
78 5
        return empty($this->addons[$name]) ? null : $this->addons[$name];
79
    }
80
81 3
    public function getReason()
82
    {
83 3
        return $this->getAddon(self::REASON);
84
    }
85
86 3
    public function getSince()
87
    {
88 3
        return $this->getAddon(self::SINCE);
89
    }
90
91 3
    public function getTill()
92
    {
93 3
        return $this->getAddon(self::TILL);
94
    }
95
}
96