Completed
Push — master ( a4f70a...5b7a0f )
by Andrii
05:16
created

AbstractAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 3
    public function __construct(
83
        $id,
84
        TypeInterface $type,
85
        TargetInterface $target,
86
        QuantityInterface $quantity,
87
        CustomerInterface $customer = null,
88
        SaleInterface $sale = null,
89
        DateTimeImmutable $time = null,
90
        ActionInterface $parent = null
91
    ) {
92 3
        $this->id       = $id;
93 3
        $this->type     = $type;
94 3
        $this->target   = $target;
95 3
        $this->quantity = $quantity;
96 3
        $this->customer = $customer;
97 3
        $this->sale     = $sale;
98 3
        $this->time     = $time;
99 3
        $this->parent   = $parent;
100 3
    }
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
    public function getCustomer(): CustomerInterface
119
    {
120
        return $this->customer;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 3
    public function getTarget(): TargetInterface
127
    {
128 3
        return $this->target;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 3
    public function getType(): TypeInterface
135
    {
136 3
        return $this->type;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 3
    public function getQuantity(): QuantityInterface
143
    {
144 3
        return $this->quantity;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getSale()
151
    {
152
        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 setId($id)
180
    {
181
        if ($this->id === $id) {
182
            return;
183
        }
184
        if ($this->id !== null) {
185
            throw new \Exception('cannot reassign action id');
186
        }
187
        $this->id = $id;
188
    }
189
190
    public function hasSale()
191
    {
192
        return $this->sale !== null;
193
    }
194
195
    public function setSale(SaleInterface $sale)
196
    {
197
        if ($this->hasSale()) {
198
            throw new \Exception('cannot reassign sale for action');
199
        }
200
        $this->sale = $sale;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function jsonSerialize()
207
    {
208
        return get_object_vars($this);
209
    }
210
211
    /**
212
     * @param PriceInterface $price
213
     * @return ChargeInterface|Charge|null
214
     */
215 3
    public function calculateCharge(PriceInterface $price): ?ChargeInterface
216
    {
217 3
        if (!$this->isApplicable($price)) {
218 1
            return null;
219
        }
220
221 3
        $usage = $price->calculateUsage($this->getQuantity());
222 3
        if ($usage === null) {
223 1
            return null;
224
        }
225
226 2
        $sum = $price->calculateSum($this->getQuantity());
227 2
        if ($sum === null) {
228
            return null;
229
        }
230
231 2
        return new Charge(null, $this, $price, $this->getTarget(), $usage, $sum);
232
    }
233
}
234