Completed
Push — master ( 6ed56d...9a6247 )
by Andrii
03:57
created

AbstractPrice::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\price;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\charge\ChargeModifier;
15
use hiqdev\php\billing\EntityInterface;
16
use hiqdev\php\billing\plan\PlanInterface;
17
use hiqdev\php\billing\target\TargetInterface;
18
use hiqdev\php\billing\type\TypeInterface;
19
use hiqdev\php\units\QuantityInterface;
20
use Money\Money;
21
22
/**
23
 * Price.
24
 * @see PriceInterface
25
 * By default Price is applicable when same target and same type as Action.
26
 * But it can be different e.g. same price for all targets when certain type.
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
abstract class AbstractPrice implements PriceInterface, EntityInterface
31
{
32
    /**
33
     * @var integer
34
     */
35
    protected $id;
36
37
    /**
38
     * @var TypeInterface
39
     */
40
    protected $type;
41
42
    /**
43
     * @var TargetInterface
44
     */
45
    protected $target;
46
47
    /**
48
     * @var PlanInterface
49
     */
50
    protected $plan;
51 34
52
    public function __construct(
53
                            $id,
54
        TypeInterface       $type,
55
        TargetInterface     $target,
56
        PlanInterface       $plan = null
57 34
    ) {
58 34
        $this->id = $id;
59 34
        $this->type = $type;
60 34
        $this->target = $target;
61 34
        $this->plan = $plan;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66 2
     */
67
    public function getId()
68 2
    {
69
        return $this->id;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74 23
     */
75
    public function getType()
76 23
    {
77
        return $this->type;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82 23
     */
83
    public function getTarget()
84 23
    {
85
        return $this->target;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90 1
     */
91
    public function getPlan(): ?PlanInterface
92 1
    {
93
        return $this->plan;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function hasPlan()
100
    {
101
        return $this->plan !== null;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function setPlan(PlanInterface $plan)
108
    {
109
        if ($this->hasPlan()) {
110
            throw new \Exception('cannot reassign plan for price');
111
        }
112
        $this->plan = $plan;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     * Default sum calculation method: sum = price * usage.
118 13
     */
119
    public function calculateSum(QuantityInterface $quantity): ?Money
120 13
    {
121 13
        $usage = $this->calculateUsage($quantity);
122
        if ($usage === null) {
123
            return null;
124
        }
125 13
126 13
        $price = $this->calculatePrice($quantity);
127
        if ($price === null) {
128
            return null;
129
        }
130
131 13
        /// TODO add configurable rounding mode later
132
        return $price->multiply($usage->getQuantity(), Money::ROUND_UP);
133
    }
134
135
    public function jsonSerialize()
136
    {
137
        return [
138
            'id' => $this->id,
139
            'type' => $this->type,
140
            'target' => $this->target,
141
        ];
142
    }
143
144
    /**
145
     * {@inheritdoc}
146 21
     */
147
    public function isApplicable(ActionInterface $action): bool
148
    {
149
        /* sorry, debugging facility
150
         * var_dump([
151
            'action.target'     => $action->getTarget(),
152
            'this.target'       => $this->getTarget(),
153
            'action.type'       => $action->getType(),
154
            'this.type'         => $this->getType(),
155
            'target matches'    => $action->getTarget()->matches($this->getTarget()),
156
            'type matches'      => $action->getType()->matches($this->getType()),
157 21
        ]);*/
158 21
        return $action->getTarget()->matches($this->getTarget()) &&
159
               $action->getType()->matches($this->getType());
160
    }
161
162
}
163