Completed
Push — master ( 6d95b5...d7bed1 )
by Andrii
09:03
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.2
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
    /**
30
     * @var int|string
31
     */
32
    protected $id;
33
34
    /**
35
     * @var TypeInterface
36
     */
37
    protected $type;
38
39
    /**
40
     * @var DateTimeImmutable
41
     */
42
    protected $time;
43
44
    /**
45
     * @var Money
46
     */
47
    protected $sum;
48
49
    /**
50
     * @var QuantityInterface
51
     */
52
    protected $quantity;
53
54
    /**
55
     * @var CustomerInterface
56
     */
57
    protected $customer;
58
59
    /**
60
     * @var TargetInterface
61
     */
62
    protected $target;
63
64
    /**
65
     * @var PlanInterface
66
     */
67
    protected $plan;
68
69
    /**
70
     * @var bool
71
     */
72
    protected $isFinished = false;
73
74
    /**
75
     * @var ChargeInterface[]
76
     */
77
    protected $charges = [];
78
79
    /**
80
     * @var string
81
     */
82
    protected $comment;
83
84 1
    public function __construct(
85
                            $id,
86
        TypeInterface       $type,
87
        DateTimeImmutable   $time,
88
        Money               $sum,
89
        QuantityInterface   $quantity,
90
        CustomerInterface   $customer,
91
        TargetInterface     $target = null,
92
        PlanInterface       $plan = null,
93
        array               $charges = [],
94
        bool                $isFinished = false
95
    ) {
96 1
        $this->id           = $id;
97 1
        $this->type         = $type;
98 1
        $this->time         = $time;
99 1
        $this->sum          = $sum;
100 1
        $this->quantity     = $quantity;
101 1
        $this->customer     = $customer;
102 1
        $this->target       = $target;
103 1
        $this->plan         = $plan;
104 1
        $this->charges      = $charges;
105 1
        $this->isFinished   = $isFinished;
106 1
    }
107
108 1
    public function getUniqueString()
109
    {
110
        $parts = [
111 1
            'buyer'     => $this->customer->getUniqueId(),
112 1
            'currency'  => $this->sum->getCurrency()->getCode(),
113 1
            'target'    => $this->target ? $this->target->getUniqueId() : null,
114 1
            'type'      => $this->type->getUniqueId(),
0 ignored issues
show
Bug introduced by
The method getUniqueId() does not exist on hiqdev\php\billing\type\TypeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\type\TypeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
            'type'      => $this->type->/** @scrutinizer ignore-call */ getUniqueId(),
Loading history...
115 1
            'time'      => $this->time->format('c'),
116 1
            'plan'      => $this->plan ? $this->plan->getUniqueId() : null,
0 ignored issues
show
Bug introduced by
The method getUniqueId() does not exist on hiqdev\php\billing\plan\PlanInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\plan\PlanInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
            'plan'      => $this->plan ? $this->plan->/** @scrutinizer ignore-call */ getUniqueId() : null,
Loading history...
117
        ];
118
119 1
        return implode('-', $parts);
120
    }
121
122
    public function calculatePrice()
123
    {
124
        $quantity = $this->quantity->getQuantity();
125
126
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
127
    }
128
129
    /**
130
     * @return int|string
131
     */
132
    public function getId()
133
    {
134
        return $this->id;
135
    }
136
137
    public function setId($id)
138
    {
139
        if ($this->id === $id) {
140
            return;
141
        }
142
        if ($this->id !== null) {
143
            throw new \Exception('cannot reassign bill id');
144
        }
145
        $this->id = $id;
146
    }
147
148
    /**
149
     * @return TypeInterface
150
     */
151 1
    public function getType()
152
    {
153 1
        return $this->type;
154
    }
155
156
    /**
157
     * @return DateTimeImmutable
158
     */
159 1
    public function getTime()
160
    {
161 1
        return $this->time;
162
    }
163
164
    /**
165
     * @return TargetInterface
166
     */
167 1
    public function getTarget()
168
    {
169 1
        return $this->target;
170
    }
171
172
    /**
173
     * @return CustomerInterface
174
     */
175 1
    public function getCustomer()
176
    {
177 1
        return $this->customer;
178
    }
179
180
    /**
181
     * @return QuantityInterface
182
     */
183 1
    public function getQuantity()
184
    {
185 1
        return $this->quantity;
186
    }
187
188
    /**
189
     * @return Money
190
     */
191 1
    public function getSum()
192
    {
193 1
        return $this->sum;
194
    }
195
196
    /**
197
     * @return PlanInterface
198
     */
199 1
    public function getPlan()
200
    {
201 1
        return $this->plan;
202
    }
203
204
    /**
205
     * @return ChargeInterface[]
206
     */
207 1
    public function getCharges()
208
    {
209 1
        return $this->charges;
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function getIsFinished()
216
    {
217
        return $this->isFinished;
218
    }
219
220
    public function getComment()
221
    {
222
        return $this->comment;
223
    }
224
225
    public function setComment(string $comment)
226
    {
227
        $this->comment = $comment;
228
    }
229
230
    public function jsonSerialize()
231
    {
232
        return get_object_vars($this);
233
    }
234
}
235