Invoice::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 8
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 1
rs 9.6333
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
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
    use AttributesTrait;
15
16
    /**
17
     * @var string Invoice serial number
18
     */
19
    private $serial;
20
21
    /**
22
     * @var AgentInterface Registered seller
23
     */
24
    private $seller;
25
26
    /**
27
     * @var AgentInterface Registered buyer
28
     */
29
    private $buyer;
30
31
    /**
32
     * @var string Payment reference number
33
     */
34
    private $ocr;
35
36
    /**
37
     * @var ItemBasket Container for charged items
38
     */
39
    private $itemBasket;
40
41
    /**
42
     * @var \DateTimeInterface Creation date
43
     */
44
    private $billDate;
45
46
    /**
47
     * @var integer Number of days before invoice expires
48
     */
49
    private $expiresAfter;
50
51
    /**
52
     * @var ?Amount Prepaid amound to deduct
53
     */
54
    private $deduction;
55
56
    /**
57
     * Load values at construct
58
     *
59
     * @param string             $serial       Invoice serial number
60
     * @param AgentInterface     $seller       Registered seller
61
     * @param AgentInterface     $buyer        Registered buyer
62
     * @param string             $ocr          Payment reference number
63
     * @param ItemBasket         $itemBasket   Container for charged items
64
     * @param \DateTimeInterface $billDate     Date of invoice creation
65
     * @param integer            $expiresAfter Nr of days before invoice expires
66
     * @param Amount             $deduction    Prepaid amound to deduct
67
     */
68 15
    public function __construct(
69
        string $serial,
70
        AgentInterface $seller,
71
        AgentInterface $buyer,
72
        string $ocr,
73
        ItemBasket $itemBasket,
74
        \DateTimeInterface $billDate,
75
        int $expiresAfter,
76
        ?Amount $deduction = null
77
    ) {
78 15
        $this->serial = $serial;
79 15
        $this->seller = $seller;
80 15
        $this->buyer = $buyer;
81 15
        $this->ocr = $ocr;
82 15
        $this->itemBasket = $itemBasket;
83 15
        $this->billDate = $billDate;
84 15
        $this->expiresAfter = $expiresAfter;
85 15
        $this->deduction = $deduction;
86 15
    }
87
88
    /**
89
     * Get invoice serial number
90
     */
91 1
    public function getSerial(): string
92
    {
93 1
        return $this->serial;
94
    }
95
96
    /**
97
     * Get seller
98
     */
99 1
    public function getSeller(): AgentInterface
100
    {
101 1
        return $this->seller;
102
    }
103
104
    /**
105
     * Get buyer
106
     */
107 1
    public function getBuyer(): AgentInterface
108
    {
109 1
        return $this->buyer;
110
    }
111
112
    /**
113
     * Get invoice reference number
114
     */
115 4
    public function getOcr(): string
116
    {
117 4
        return $this->ocr;
118
    }
119
120
    /**
121
     * Get item container
122
     */
123 4
    public function getItems(): ItemBasket
124
    {
125 4
        return $this->itemBasket;
126
    }
127
128
    /**
129
     * Get charged amount (including VAT and deduction)
130
     */
131 3
    public function getInvoiceTotal(): Amount
132
    {
133 3
        return $this->getItems()->getTotalCost()->subtract($this->getDeduction());
134
    }
135
136
    /**
137
     * Get date of invoice creation
138
     */
139 3
    public function getBillDate(): \DateTimeInterface
140
    {
141 3
        return $this->billDate;
142
    }
143
144
    /**
145
     * Get number of days before invoice expires
146
     */
147 1
    public function getExpiresAfter(): int
148
    {
149 1
        return $this->expiresAfter;
150
    }
151
152
    /**
153
     * Get date when invoice expires
154
     */
155 1
    public function getExpirationDate(): \DateTimeInterface
156
    {
157 1
        return new \DateTimeImmutable(
158 1
            '@' . ($this->billDate->getTimestamp() + $this->getExpiresAfter() * 24 * 60 * 60)
159
        );
160
    }
161
162
    /**
163
     * Get prepaid amound to deduct
164
     */
165 5
    public function getDeduction(): Amount
166
    {
167 5
        if (!isset($this->deduction)) {
168 1
            return $this->itemBasket->createCurrencyObject('0');
169
        }
170
171 4
        return $this->deduction;
172
    }
173
}
174