Completed
Push — master ( c071d8...d2937f )
by Andrii
02:38
created

AbstractPrice::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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\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
52 26
    public function __construct(
53
                            $id,
54
        TypeInterface       $type,
55
        TargetInterface     $target,
56
        PlanInterface       $plan = null
57
    ) {
58 26
        $this->id = $id;
59 26
        $this->type = $type;
60 26
        $this->target = $target;
61 26
        $this->plan = $plan;
62 26
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function getId()
68
    {
69 2
        return $this->id;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 19
    public function getType()
76
    {
77 19
        return $this->type;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 19
    public function getTarget()
84
    {
85 19
        return $this->target;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function getPlan()
92
    {
93 1
        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
     */
119 9
    public function calculateSum(QuantityInterface $quantity): ?Money
120
    {
121 9
        $usage = $this->calculateUsage($quantity);
122 9
        if ($usage === null) {
123
            return null;
124
        }
125
126 9
        $price = $this->calculatePrice($quantity);
127 9
        if ($price === null) {
128
            return null;
129
        }
130
131
        /// TODO add configurable rounding mode later
132 9
        return $price->multiply($usage->getQuantity(), Money::ROUND_UP);
133
    }
134
135
    public static function create(array $data)
136
    {
137
        return new SinglePrice($data['id'], $data['target']);
138
    }
139
140
    public function jsonSerialize()
141
    {
142
        return [
143
            'id' => $this->id,
144
            'type' => $this->type,
145
            'target' => $this->target,
146
        ];
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 17
    public function isApplicable(ActionInterface $action): bool
153
    {
154
        /* sorry, debugging facility
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
         * var_dump([
156
            'action.target'     => $action->getTarget(),
157
            'this.target'       => $this->getTarget(),
158
            'action.type'       => $action->getType(),
159
            'this.type'         => $this->getType(),
160
            'target matches'    => $action->getTarget()->matches($this->getTarget()),
161
            'type matches'      => $action->getType()->matches($this->getType()),
162
        ]);*/
163 17
        return $action->getTarget()->matches($this->getTarget()) &&
164 17
               $action->getType()->matches($this->getType());
165
    }
166
167
    /** {@inheritdoc} */
168 4
    public function calculateCharges(ActionInterface $action): array
169
    {
170 4
        $charge = $action->calculateCharge($this);
171 4
        if ($this instanceof ChargeModifier) {
172
            return $this->modifyCharge($charge, $action);
173
        }
174
175 4
        return $charge ? [$charge] : [];
176
    }
177
}
178