Completed
Push — master ( 041620...15d4af )
by Andrii
14:30
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 0
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
    /** @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
    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
        $this->id           = $id;
75
        $this->type         = $type;
76
        $this->time         = $time;
77
        $this->sum          = $sum;
78
        $this->quantity     = $quantity;
79
        $this->customer     = $customer;
80
        $this->target       = $target;
81
        $this->plan         = $plan;
82
        $this->charges      = $charges;
83
        $this->state        = $state;
84 1
    }
85
86
    public function getUniqueString(): string
87
    {
88
        $parts = [
89
            'buyer'     => $this->customer->getUniqueId(),
90
            'currency'  => $this->sum->getCurrency()->getCode(),
91
            'target'    => $this->target ? $this->target->getUniqueId() : null,
92
            'type'      => $this->type->getUniqueId(),
93
            'time'      => $this->time->format('c'),
94
            'plan'      => $this->plan ? $this->plan->getUniqueId() : null,
95
        ];
96 1
97 1
        return implode('-', $parts);
98 1
    }
99 1
100 1
    public function calculatePrice()
101 1
    {
102 1
        $quantity = $this->quantity->getQuantity();
103 1
104 1
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
105 1
    }
106 1
107
    /**
108 1
     * @return int|string
109
     */
110
    public function getId()
111 1
    {
112 1
        return $this->id;
113 1
    }
114 1
115 1
    public function setId($id)
116 1
    {
117
        if ($this->id === $id) {
118
            return;
119 1
        }
120
        if ($this->id !== null) {
121
            throw new \Exception('cannot reassign bill id');
122
        }
123
        $this->id = $id;
124
    }
125
126
    /**
127
     * @return TypeInterface
128
     */
129
    public function getType(): TypeInterface
130
    {
131
        return $this->type;
132
    }
133
134
    /**
135
     * @return DateTimeImmutable
136
     */
137
    public function getTime(): DateTimeImmutable
138
    {
139
        return $this->time;
140
    }
141
142
    /**
143
     * @return TargetInterface
144
     */
145
    public function getTarget()
146
    {
147
        return $this->target;
148
    }
149
150
    /**
151 1
     * @return CustomerInterface
152
     */
153 1
    public function getCustomer()
154
    {
155
        return $this->customer;
156
    }
157
158
    /**
159 1
     * @return QuantityInterface
160
     */
161 1
    public function getQuantity()
162
    {
163
        return $this->quantity;
164
    }
165
166
    /**
167 1
     * @return Money
168
     */
169 1
    public function getSum()
170
    {
171
        return $this->sum;
172
    }
173
174
    /**
175 1
     * @return PlanInterface
176
     */
177 1
    public function getPlan()
178
    {
179
        return $this->plan;
180
    }
181
182
    /**
183 1
     * @return ChargeInterface[]
184
     */
185 1
    public function getCharges()
186
    {
187
        return $this->charges;
188
    }
189
190
    public function getState(): ?BillState
191 1
    {
192
        return $this->state;
193 1
    }
194
195
    public function setFinished(): void
196
    {
197
        $this->state = BillState::finished();
198
    }
199 1
200
    public function isFinished(): ?bool
201 1
    {
202
        return $this->state === null ? null : $this->state->isFinished();
203
    }
204
205
    public function getComment()
206
    {
207 1
        return $this->comment;
208
    }
209 1
210
    public function setComment(string $comment)
211
    {
212
        $this->comment = $comment;
213
    }
214
215
    public function jsonSerialize()
216
    {
217
        return get_object_vars($this);
218
    }
219
}
220