Passed
Push — master ( 364662...9798b7 )
by Andrii
02:56
created

Charge::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 1
eloc 8
c 3
b 0
f 2
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10

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\charge;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\bill\BillInterface;
15
use hiqdev\php\billing\event\EventAwareTrait;
16
use hiqdev\php\billing\Exception\CannotReassignException;
17
use hiqdev\php\billing\price\PriceInterface;
18
use hiqdev\php\billing\target\TargetInterface;
19
use hiqdev\php\billing\type\TypeInterface;
20
use hiqdev\php\units\QuantityInterface;
21
use Money\Money;
22
23
/**
24
 * Charge.
25
 *
26
 * [[Action]] is charged with a number of [[Charge]]s.
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
class Charge implements ChargeInterface
31
{
32
    use EventAwareTrait;
33
34
    /** @var int */
35
    protected $id;
36
37
    /** @var TypeInterface */
38
    protected $type;
39
40
    /** @var TargetInterface */
41
    protected $target;
42
43
    /** @var ActionInterface */
44
    protected $action;
45
46
    /** @var PriceInterface */
47
    protected $price;
48
49
    /** @var QuantityInterface */
50
    protected $usage;
51
52
    /** @var Money */
53
    protected $sum;
54
55
    /** @var BillInterface */
56
    protected $bill;
57
58
    /** @var ChargeState */
59
    protected $state;
60
61
    /** @var string */
62
    protected $comment;
63
64
    /** @var ChargeInterface|null */
65
    protected $parent;
66
67 18
    public function __construct(
68
                            $id,
69
        TypeInterface $type,
70
        TargetInterface $target,
71
        ActionInterface $action,
72
        ?PriceInterface $price,
73
        QuantityInterface $usage,
74
        Money $sum,
75
        BillInterface $bill = null
76
    ) {
77 18
        $this->id       = $id;
78 18
        $this->type     = $type;
79 18
        $this->target   = $target;
80 18
        $this->action   = $action;
81 18
        $this->price    = $price;
82 18
        $this->usage    = $usage;
83 18
        $this->sum      = $sum;
84 18
        $this->bill     = $bill;
85 18
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getUniqueString(): string
91
    {
92
        return implode('-', [
93
            'currency'  => $this->sum->getCurrency()->getCode(),
94
            'buyer'     => $this->action->getCustomer()->getUniqueId(),
95
            'target'    => $this->target->getUniqueId(),
96
            'type'      => $this->type->getUniqueId(),
97
            'time'      => $this->action->getTime()->format('c'),
98
            'parent'    => '(' . ($this->parent !== null ? $this->parent->getUniqueString() : null) . ')',
99
        ]);
100
    }
101
102
    public function hasId(): bool
103
    {
104
        return $this->id !== null;
105
    }
106
107 1
    public function getId()
108
    {
109 1
        return $this->id;
110
    }
111
112 9
    public function getType(): TypeInterface
113
    {
114 9
        return $this->type;
115
    }
116
117 7
    public function getTarget(): TargetInterface
118
    {
119 7
        return $this->target;
120
    }
121
122 12
    public function getAction(): ActionInterface
123
    {
124 12
        return $this->action;
125
    }
126
127 7
    public function getPrice(): PriceInterface
128
    {
129 7
        return $this->price;
130
    }
131
132 14
    public function getUsage(): QuantityInterface
133
    {
134 14
        return $this->usage;
135
    }
136
137 14
    public function getSum(): Money
138
    {
139 14
        return $this->sum;
140
    }
141
142
    public function calculatePrice(): Money
143
    {
144
        $usage = $this->usage->getQuantity();
145
146
        return $usage ? $this->sum->divide($usage) : $this->sum;
147
    }
148
149 1
    public function getBill(): ?BillInterface
150
    {
151 1
        return $this->bill;
152
    }
153
154
    public function hasBill()
155
    {
156
        return $this->bill !== null;
157
    }
158
159
    public function setBill(BillInterface $bill): ChargeInterface
160
    {
161
        /*if ($this->hasBill()) {
162
            throw new CannotReassignException('sale bill');
163
        }*/
164
        $this->bill = $bill;
165
166
        return $this;
167
    }
168
169
    public function getState(): ?ChargeState
170
    {
171
        return $this->state;
172
    }
173
174
    public function setFinished(): ChargeInterface
175
    {
176
        $this->state = ChargeState::finished();
177
178
        return $this;
179
    }
180
181
    public function isFinished(): ?bool
182
    {
183
        return $this->state === null ? null : $this->state->isFinished();
184
    }
185
186 1
    public function getComment(): ?string
187
    {
188 1
        return $this->comment;
189
    }
190
191
    public function setComment(string $comment): ChargeInterface
192
    {
193
        $this->comment = $comment;
194
195
        return $this;
196
    }
197
198
    public function setId($id): ChargeInterface
199
    {
200
        if ((string) $this->id === (string) $id) {
201
            return $this;
202
        }
203
        if ($this->id !== null) {
204
            throw new CannotReassignException('charge id');
205
        }
206
        $this->id = $id;
207
208
        return $this;
209
    }
210
211
    public function jsonSerialize()
212
    {
213
        return array_filter(get_object_vars($this));
214
    }
215
216
    public function getParent(): ?ChargeInterface
217
    {
218
        return $this->parent;
219
    }
220
221
    /**
222
     * @param ChargeInterface|null $parent
223
     *
224
     * @throws \Exception if parent is already set
225
     * @return Charge
226
     */
227 4
    public function setParent(ChargeInterface $parent): self
228
    {
229 4
        if ($this->parent !== null) {
230
            throw new CannotReassignException('charge parent');
231
        }
232
233 4
        $this->parent = $parent;
234
235 4
        return $this;
236
    }
237
238
    /**
239
     * Forcefully changes parent charge
240
     *
241
     * @return Charge
242
     */
243
    public function overwriteParent(ChargeInterface $parent): self
244
    {
245
        $this->parent = $parent;
246
247
        return $this;
248
    }
249
}
250