Completed
Push — master ( 308de5...6bfaa4 )
by Hannes
03:47 queued 02:10
created

Invoice::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 23
c 3
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.0856
cc 3
eloc 21
nc 4
nop 10
crap 3

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
declare(strict_types=1);
4
5
namespace byrokrat\billing;
6
7
use byrokrat\amount\Amount;
8
9
/**
10
 * Generic invoice container object
11
 */
12
class Invoice
13
{
14
    /**
15
     * @var string Invoice serial number
16
     */
17
    private $serial;
18
19
    /**
20
     * @var Seller Registered seller
21
     */
22
    private $seller;
23
24
    /**
25
     * @var Buyer Registered buyer
26
     */
27
    private $buyer;
28
29
    /**
30
     * @var string Message to buyer
31
     */
32
    private $message;
33
34
    /**
35
     * @var string Payment reference number
36
     */
37
    private $ocr;
38
39
    /**
40
     * @var ItemBasket Container for charged items
41
     */
42
    private $itemBasket = [];
43
44
    /**
45
     * @var \DateTime Creation date
46
     */
47
    private $billDate;
48
49
    /**
50
     * @var integer Number of days before invoice expires
51
     */
52
    private $expiresAfter;
53
54
    /**
55
     * @var Amount Prepaid amound to deduct
56
     */
57
    private $deduction;
58
59
    /**
60
     * @var string 3-letter ISO 4217 currency code indicating currency
61
     */
62
    private $currency;
63
64
    /**
65
     * Construct invoice
66
     *
67
     * @param string         $serial       Invoice serial number
68
     * @param Seller         $seller       Registered seller
69
     * @param Buyer          $buyer        Registered buyer
70
     * @param string         $message      Invoice message
71
     * @param string         $ocr          Payment reference number
72
     * @param ItemBasket     $itemBasket  Container for charged items
73
     * @param \DateTime|null $billDate     Date of invoice creation
74
     * @param integer        $expiresAfter Nr of days before invoice expires
75
     * @param Amount|null    $deduction    Prepaid amound to deduct
76
     * @param string         $currency     3-letter ISO 4217 currency code indicating currency
77
     */
78 15
    public function __construct(
79
        string $serial,
80
        Seller $seller,
81
        Buyer $buyer,
82
        string $message = '',
83
        string $ocr = '',
84
        ItemBasket $itemBasket = null,
85
        \DateTime $billDate = null,
86
        int $expiresAfter = 30,
87
        Amount $deduction = null,
88
        string $currency = 'SEK'
89
    ) {
90 15
        $this->serial = $serial;
91 15
        $this->seller = $seller;
92 15
        $this->buyer = $buyer;
93 15
        $this->itemBasket = $itemBasket;
94 15
        $this->ocr = $ocr;
95 15
        $this->message = $message;
96 15
        $this->billDate = $billDate ?: new \DateTime;
97 15
        $this->expiresAfter = $expiresAfter;
98 15
        $this->deduction = $deduction ?: new Amount('0');
99 15
        $this->currency = $currency;
100 15
    }
101
102
    /**
103
     * Get invoice serial number
104
     */
105 1
    public function getSerial(): string
106
    {
107 1
        return $this->serial;
108
    }
109
110
    /**
111
     * Get seller
112
     */
113 1
    public function getSeller(): Seller
114
    {
115 1
        return $this->seller;
116
    }
117
118
    /**
119
     * Get buyer
120
     */
121 1
    public function getBuyer(): Buyer
122
    {
123 1
        return $this->buyer;
124
    }
125
126
    /**
127
     * Get invoice message
128
     */
129 2
    public function getMessage(): string
130
    {
131 2
        return $this->message;
132
    }
133
134
    /**
135
     * Get invoice reference number
136
     */
137 4
    public function getOcr(): string
138
    {
139 4
        return $this->ocr;
140
    }
141
142
    /**
143
     * Get item container
144
     */
145 2
    public function getItems(): ItemBasket
146
    {
147 2
        return $this->itemBasket;
148
    }
149
150
    /**
151
     * Get charged amount (including VAT and deduction)
152
     */
153 1
    public function getInvoiceTotal(): Amount
154
    {
155 1
        return $this->getItems()->getTotalCost()->subtract($this->getDeduction());
156
    }
157
158
    /**
159
     * Get date of invoice creation
160
     */
161 3
    public function getBillDate(): \DateTime
162
    {
163 3
        return $this->billDate;
164
    }
165
166
    /**
167
     * Get number of days before invoice expires
168
     */
169 2
    public function getExpiresAfter(): int
170
    {
171 2
        return $this->expiresAfter;
172
    }
173
174
    /**
175
     * Get date when invoice expires
176
     */
177 1
    public function getExpirationDate(): \DateTime
178
    {
179 1
        $expireDate = clone $this->billDate;
180 1
        $expireDate->add(new \DateInterval("P{$this->getExpiresAfter()}D"));
181
182 1
        return $expireDate;
183
    }
184
185
    /**
186
     * Get prepaid amound to deduct
187
     */
188 3
    public function getDeduction(): Amount
189
    {
190 3
        return $this->deduction;
191
    }
192
193
    /**
194
     * Get the 3-letter ISO 4217 currency code indicating the invoice currency
195
     */
196 2
    public function getCurrency(): string
197
    {
198 2
        return $this->currency;
199
    }
200
}
201