Completed
Push — master ( 161548...3f9aa9 )
by Andrii
03:06
created

Bill::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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\bill;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\charge\ChargeInterface;
15
use hiqdev\php\billing\customer\CustomerInterface;
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
 * Bill.
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class Bill implements BillInterface
28
{
29
    /** @var int|string */
30
    protected $id;
31
32
    /** @var TypeInterface */
33
    protected $type;
34
35
    /** @var DateTimeImmutable */
36
    protected $time;
37
38
    /** @var Money */
39
    protected $sum;
40
41
    /** @var QuantityInterface */
42
    protected $quantity;
43
44
    /** @var CustomerInterface */
45
    protected $customer;
46
47
    /** @var TargetInterface */
48
    protected $target;
49
50
    /** @var PlanInterface */
51
    protected $plan;
52
53
    /** @var ChargeInterface[] */
54
    protected $charges = [];
55
56
    /** @var BillState */
57
    protected $state;
58
59
    /** @var string */
60
    protected $comment;
61
62 1
    public function __construct(
63
                            $id,
64
        TypeInterface       $type,
65
        DateTimeImmutable   $time,
66
        Money               $sum,
67
        QuantityInterface   $quantity,
68
        CustomerInterface   $customer,
69
        TargetInterface     $target = null,
70
        PlanInterface       $plan = null,
71
        array               $charges = [],
72
        BillState           $state = null
73
    ) {
74 1
        $this->id           = $id;
75 1
        $this->type         = $type;
76 1
        $this->time         = $time;
77 1
        $this->sum          = $sum;
78 1
        $this->quantity     = $quantity;
79 1
        $this->customer     = $customer;
80 1
        $this->target       = $target;
81 1
        $this->plan         = $plan;
82 1
        $this->charges      = $charges;
83 1
        $this->state        = $state;
84 1
    }
85
86
    /**
87
     * Provides unique string.
88
     * Can be used to compare or aggregate bills.
89
     */
90 1
    public function getUniqueString(): string
91
    {
92
        $parts = [
93 1
            'currency'  => $this->sum->getCurrency()->getCode(),
94 1
            'buyer'     => $this->customer->getUniqueId(),
95 1
            'target'    => $this->target ? $this->target->getUniqueId() : null,
96 1
            'type'      => $this->type->getUniqueId(),
97 1
            'time'      => $this->time->format('c'),
98
        ];
99
100 1
        return implode('-', $parts);
101
    }
102
103
    public function calculatePrice()
104
    {
105
        $quantity = $this->quantity->getQuantity();
106
107
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
108
    }
109
110
    /**
111
     * @return int|string
112
     */
113 1
    public function getId()
114
    {
115 1
        return $this->id;
116
    }
117
118
    public function setId($id)
119
    {
120
        if ($this->id === $id) {
121
            return;
122
        }
123
        if ($this->id !== null) {
124
            throw new \Exception('cannot reassign bill id');
125
        }
126
        $this->id = $id;
127
    }
128
129
    /**
130
     * @return TypeInterface
131
     */
132 1
    public function getType(): TypeInterface
133
    {
134 1
        return $this->type;
135
    }
136
137
    /**
138
     * @return DateTimeImmutable
139
     */
140 1
    public function getTime(): DateTimeImmutable
141
    {
142 1
        return $this->time;
143
    }
144
145
    /**
146
     * @return TargetInterface
147
     */
148 1
    public function getTarget()
149
    {
150 1
        return $this->target;
151
    }
152
153
    /**
154
     * @return CustomerInterface
155
     */
156 1
    public function getCustomer()
157
    {
158 1
        return $this->customer;
159
    }
160
161
    /**
162
     * @return QuantityInterface
163
     */
164 1
    public function getQuantity()
165
    {
166 1
        return $this->quantity;
167
    }
168
169
    /**
170
     * @return BillInterface
171
     */
172
    public function setQuantity(QuantityInterface $quantity): BillInterface
173
    {
174
        $this->quantity = $quantity;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return Money
181
     */
182 1
    public function getSum()
183
    {
184 1
        return $this->sum;
185
    }
186
187
    /**
188
     * @return PlanInterface
189
     */
190 1
    public function getPlan()
191
    {
192 1
        return $this->plan;
193
    }
194
195
    /**
196
     * @return bool
197
     */
198
    public function hasCharges(): bool
199
    {
200
        return $this->charges !== [];
201
    }
202
203
    /**
204
     * @return ChargeInterface[]
205
     */
206 1
    public function getCharges(): array
207
    {
208 1
        return $this->charges;
209
    }
210
211
    /**
212
     * @param ChargeInterface[] $prices
213
     * @return self
214
     * @throws \Exception
215
     */
216
    public function setCharges(array $charges): self
217
    {
218
        if ($this->hasCharges()) {
219
            throw new \Exception('cannot reassign charges for bill');
220
        }
221
        $this->charges = $charges;
222
223
        return $this;
224
    }
225
226
    public function getState(): ?BillState
227
    {
228
        return $this->state;
229
    }
230
231
    public function setFinished(): void
232
    {
233
        $this->state = BillState::finished();
234
    }
235
236
    public function isFinished(): ?bool
237
    {
238
        return $this->state === null ? null : $this->state->isFinished();
239
    }
240
241
    public function getComment()
242
    {
243
        return $this->comment;
244
    }
245
246
    public function setComment(string $comment)
247
    {
248
        $this->comment = $comment;
249
    }
250
251
    public function jsonSerialize()
252
    {
253
        return get_object_vars($this);
254
    }
255
}
256