Completed
Push — master ( 6ed56d...9a6247 )
by Andrii
03:57
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 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0

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