Completed
Push — master ( 428142...9f94b9 )
by Andrii
02:14
created

Bill::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 9
crap 1

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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\bill;
12
13
use DateTime;
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 DateTime
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;
73
74
    /**
75
     * @var ChargeInterface[]
76
     */
77
    protected $charges = [];
78
79 1
    public function __construct(
80
                            $id,
81
        TypeInterface       $type,
82
        DateTime            $time,
83
        Money               $sum,
84
        QuantityInterface   $quantity,
85
        CustomerInterface   $customer,
86
        TargetInterface     $target = null,
87
        PlanInterface       $plan = null,
88
        array               $charges = []
89
    ) {
90 1
        $this->id       = $id;
91 1
        $this->type     = $type;
92 1
        $this->time     = $time;
93 1
        $this->sum      = $sum;
94 1
        $this->quantity = $quantity;
95 1
        $this->customer = $customer;
96 1
        $this->target   = $target;
97 1
        $this->plan     = $plan;
98 1
        $this->charges  = $charges;
99 1
    }
100
101 1
    public function getUniqueId()
102
    {
103
        $parts = [
104 1
            $this->customer->getUniqueId(),
105 1
            $this->sum->getCurrency()->getCode(),
106 1
            $this->target ? $this->target->getUniqueId() : null,
107 1
            $this->type->getUniqueId(),
108 1
            $this->time->format('c'),
109 1
            $this->plan ? $this->plan->getUniqueId() : null,
110
        ];
111
112 1
        return implode('-', array_filter($parts));
113
    }
114
115
    public function calculatePrice()
116
    {
117
        $quantity = $this->quantity->getQuantity();
118
119
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
120
    }
121
122
    /**
123
     * @return int|string
124
     */
125
    public function getId()
126
    {
127
        return $this->id;
128
    }
129
130
    /**
131
     * @return TypeInterface
132
     */
133 1
    public function getType()
134
    {
135 1
        return $this->type;
136
    }
137
138
    /**
139
     * @return DateTime
140
     */
141 1
    public function getTime()
142
    {
143 1
        return $this->time;
144
    }
145
146
    /**
147
     * @return TargetInterface
148
     */
149 1
    public function getTarget()
150
    {
151 1
        return $this->target;
152
    }
153
154
    /**
155
     * @return CustomerInterface
156
     */
157 1
    public function getCustomer()
158
    {
159 1
        return $this->customer;
160
    }
161
162
    /**
163
     * @return QuantityInterface
164
     */
165 1
    public function getQuantity()
166
    {
167 1
        return $this->quantity;
168
    }
169
170
    /**
171
     * @return Money
172
     */
173 1
    public function getSum()
174
    {
175 1
        return $this->sum;
176
    }
177
178
    /**
179
     * @return PlanInterface
180
     */
181 1
    public function getPlan()
182
    {
183 1
        return $this->plan;
184
    }
185
186
    /**
187
     * @return ChargeInterface[]
188
     */
189 1
    public function getCharges()
190
    {
191 1
        return $this->charges;
192
    }
193
194
    public function jsonSerialize()
195
    {
196
        return get_object_vars($this);
197
    }
198
}
199