Completed
Push — master ( a718f2...d99910 )
by Andrii
04:05
created

AbstractAction   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 23
lcom 3
cbo 2
dl 0
loc 216
ccs 29
cts 58
cp 0.5
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A createSubaction() 0 4 1
A getId() 0 4 1
A getCustomer() 0 4 1
A getTarget() 0 4 1
A getType() 0 4 1
A getQuantity() 0 4 1
A getTime() 0 4 1
A getParent() 0 4 1
A hasParent() 0 4 1
A getSale() 0 4 1
A hasId() 0 4 1
A setId() 0 10 3
A hasSale() 0 4 1
A setSale() 0 7 2
A jsonSerialize() 0 4 1
B calculateCharge() 0 26 4
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\action;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\customer\CustomerInterface;
17
use hiqdev\php\billing\EntityInterface;
18
use hiqdev\php\billing\price\PriceInterface;
19
use hiqdev\php\billing\sale\SaleInterface;
20
use hiqdev\php\billing\target\TargetInterface;
21
use hiqdev\php\billing\type\TypeInterface;
22
use hiqdev\php\units\QuantityInterface;
23
24
/**
25
 * Chargeable Action.
26
 *
27
 * @see ActionInterface
28
 *
29
 * @author Andrii Vasyliev <[email protected]>
30
 */
31
abstract class AbstractAction implements ActionInterface, EntityInterface
32
{
33
    /**
34
     * @var int
35
     */
36
    protected $id;
37
38
    /**
39
     * @var TypeInterface
40
     */
41
    protected $type;
42
43
    /**
44
     * @var TargetInterface
45
     */
46
    protected $target;
47
48
    /**
49
     * @var QuantityInterface
50
     */
51
    protected $quantity;
52
53
    /**
54
     * @var CustomerInterface
55
     */
56
    protected $customer;
57
58
    /**
59
     * @var SaleInterface
60
     */
61
    protected $sale;
62
63
    /**
64
     * @var DateTimeImmutable
65
     */
66
    protected $time;
67
68
    /**
69
     * @var ActionInterface
70
     */
71
    protected $parent;
72
73
    /**
74
     * @param TypeInterface $type
75
     * @param TargetInterface $target
76
     * @param QuantityInterface $quantity
77
     * @param CustomerInterface $customer
78
     * @param SaleInterface $sale
79
     * @param DateTimeImmutable $time
80
     * @param ActionInterface $parent
81
     */
82 6
    public function __construct(
83
        $id,
84
        TypeInterface $type,
85
        TargetInterface $target,
86
        QuantityInterface $quantity,
87
        CustomerInterface $customer,
88
        SaleInterface $sale = null,
89
        DateTimeImmutable $time = null,
90
        ActionInterface $parent = null
91
    ) {
92 6
        $this->id       = $id;
93 6
        $this->type     = $type;
94 6
        $this->target   = $target;
95 6
        $this->quantity = $quantity;
96 6
        $this->customer = $customer;
97 6
        $this->sale     = $sale;
98 6
        $this->time     = $time;
99 6
        $this->parent   = $parent;
100 6
    }
101
102
    public function createSubaction(CustomerInterface $customer)
103
    {
104
        return new static(null, $this->type, $this->target, $this->quantity, $customer, $this->sale, $this->time, $this);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getId()
111
    {
112
        return $this->id;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function getCustomer(): CustomerInterface
119
    {
120 1
        return $this->customer;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 6
    public function getTarget(): TargetInterface
127
    {
128 6
        return $this->target;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 6
    public function getType(): TypeInterface
135
    {
136 6
        return $this->type;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 6
    public function getQuantity(): QuantityInterface
143
    {
144 6
        return $this->quantity;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 2
    public function getSale()
151
    {
152 2
        return $this->sale;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getTime(): DateTimeImmutable
159
    {
160
        return $this->time;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getParent()
167
    {
168
        return $this->parent;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function hasParent()
175
    {
176
        return $this->parent !== null;
177
    }
178
179
    public function hasId()
180
    {
181
        return $this->id !== null;
182
    }
183
184
    public function setId($id)
185
    {
186
        if ($this->id === $id) {
187
            return;
188
        }
189
        if ($this->hasId()) {
190
            throw new \Exception('cannot reassign action id');
191
        }
192
        $this->id = $id;
193
    }
194
195
    public function hasSale()
196
    {
197
        return $this->sale !== null;
198
    }
199
200
    public function setSale(SaleInterface $sale)
201
    {
202
        if ($this->hasSale()) {
203
            throw new \Exception('cannot reassign sale for action');
204
        }
205
        $this->sale = $sale;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function jsonSerialize()
212
    {
213
        return get_object_vars($this);
214
    }
215
216
    /**
217
     * @param PriceInterface $price
218
     * @return ChargeInterface|Charge|null
219
     */
220 6
    public function calculateCharge(PriceInterface $price): ?ChargeInterface
221
    {
222 6
        if (!$this->isApplicable($price)) {
223 4
            return null;
224
        }
225
226 6
        $usage = $price->calculateUsage($this->getQuantity());
227 6
        if ($usage === null) {
228 1
            return null;
229
        }
230
231 5
        $sum = $price->calculateSum($this->getQuantity());
232 5
        if ($sum === null) {
233
            return null;
234
        }
235
236
        /* sorry, debugging facility
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
237
         * var_dump([
238
            'unit'      => $usage->getUnit()->getName(),
239
            'quantity'  => $usage->getQuantity(),
240
            'price'     => $price->calculatePrice($usage)->getAmount(),
241
            'sum'       => $sum->getAmount(),
242
        ]);*/
243
244 5
        return new Charge(null, $this, $price, $this->getTarget(), $usage, $sum);
245
    }
246
}
247