Completed
Push — master ( a29fc9...07f98c )
by Andrii
06:20
created

Bill::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 22
ccs 0
cts 22
cp 0
crap 2
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
    public function __construct(
80
                            $id,
81
        TypeInterface       $type,
82
        DateTimeImmutable   $time,
83
        Money               $sum,
84
        QuantityInterface   $quantity,
85
        CustomerInterface   $customer,
86
        TargetInterface     $target = null,
87
        PlanInterface       $plan = null,
88
        array               $charges = [],
89
        bool                $isFinished = false
90
    ) {
91
        $this->id           = $id;
92
        $this->type         = $type;
93
        $this->time         = $time;
94
        $this->sum          = $sum;
95
        $this->quantity     = $quantity;
96
        $this->customer     = $customer;
97
        $this->target       = $target;
98
        $this->plan         = $plan;
99
        $this->charges      = $charges;
100
        $this->isFinished   = $isFinished;
101
    }
102
103
    public function getUniqueString()
104
    {
105
        $parts = [
106
            'buyer'     => $this->customer->getUniqueId(),
107
            'currency'  => $this->sum->getCurrency()->getCode(),
108
            'target'    => $this->target ? $this->target->getUniqueId() : null,
109
            '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

109
            'type'      => $this->type->/** @scrutinizer ignore-call */ getUniqueId(),
Loading history...
110
            'time'      => $this->time->format('c'),
111
            '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

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