Completed
Push — master ( 3ee5ff...5084e8 )
by Joachim
12:38
created

OrderLine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 2
1
<?php
2
namespace Loevgaard\AltaPay\Payload;
3
4
use Assert\Assert;
5
6
class OrderLine extends Payload implements OrderLineInterface
7
{
8
    const GOODS_TYPE_SHIPMENT = 'shipment';
9
    const GOODS_TYPE_HANDLING = 'handling';
10
    const GOODS_TYPE_ITEM = 'item';
11
    const GOODS_TYPE_REFUND = 'refund';
12
13
    /**
14
     * @var string
15
     */
16
    protected $description;
17
18
    /**
19
     * @var string
20
     */
21
    protected $itemId;
22
23
    /**
24
     * @var float
25
     */
26
    protected $quantity;
27
28
    /**
29
     * @var float
30
     */
31
    protected $unitPrice;
32
33
    /**
34
     * @var float
35
     */
36
    protected $taxPercent;
37
38
    /**
39
     * @var float
40
     */
41
    protected $taxAmount;
42
43
    /**
44
     * @var string
45
     */
46
    protected $unitCode;
47
48
    /**
49
     * @var float
50
     */
51
    protected $discount;
52
53
    /**
54
     * @var string
55
     */
56
    protected $goodsType;
57
58
    /**
59
     * @var string
60
     */
61 3
    protected $imageUrl;
62
63
    public function __construct(string $description, string $itemId, float $quantity, float $unitPrice)
64
    {
65
        $this->description = $description;
66
        $this->itemId = $itemId;
67
        $this->quantity = $quantity;
68
        $this->unitPrice = $unitPrice;
69
    }
70
71
    public function getPayload() : array
72
    {
73
        $payload = [
74 3
            'description' => $this->description,
75 3
            'itemId' => $this->itemId,
76 3
            'quantity' => $this->quantity,
77 3
            'unitPrice' => $this->unitPrice,
78 3
            'taxPercent' => $this->taxPercent,
79 3
            'taxAmount' => $this->taxAmount,
80 3
            'unitCode' => $this->unitCode,
81 3
            'discount' => $this->discount,
82 3
            'goodsType' => $this->goodsType,
83 3
            'imageUrl' => $this->imageUrl,
84 3
        ];
85
86 3
        $this->validate();
87
88
        return static::simplePayload($payload);
89 3
    }
90 3
91 3
    public function validate()
92 3
    {
93 3
        Assert::that($this->description)->string();
94 3
        Assert::that($this->itemId)->string();
95 3
        Assert::that($this->quantity)->float();
96 3
        Assert::that($this->unitPrice)->float();
97 3
        Assert::thatNullOr($this->taxPercent)->float();
98 3
        Assert::thatNullOr($this->taxAmount)->float();
99 2
        Assert::thatNullOr($this->unitCode)->string();
100
        Assert::thatNullOr($this->discount)->float();
101 3
        Assert::thatNullOr($this->goodsType)->string()->inArray(static::getGoodsTypes());
102
        Assert::thatNullOr($this->imageUrl)->string();
103
    }
104
105
    /**
106
     * @return array
107 3
     */
108
    public static function getGoodsTypes() : array
109
    {
110 3
        return [
111 3
            self::GOODS_TYPE_HANDLING,
112 3
            self::GOODS_TYPE_ITEM,
113 3
            self::GOODS_TYPE_REFUND,
114 2
            self::GOODS_TYPE_SHIPMENT,
115
        ];
116
    }
117
118
    /**
119
     * @return string
120 3
     */
121
    public function getDescription() : string
122 3
    {
123
        return $this->description;
124
    }
125
126
    /**
127
     * @param string $description
128
     * @return OrderLine
129 3
     */
130
    public function setDescription(string $description) : self
131 3
    {
132 3
        $this->description = $description;
133 3
        return $this;
134
    }
135
136
    /**
137
     * @return string
138
     */
139 3
    public function getItemId() : string
140
    {
141 3
        return $this->itemId;
142
    }
143
144
    /**
145
     * @param string $itemId
146
     * @return OrderLine
147
     */
148 3
    public function setItemId(string $itemId) : self
149
    {
150 3
        $this->itemId = $itemId;
151 3
        return $this;
152 3
    }
153
154
    /**
155
     * @return float
156
     */
157
    public function getQuantity() : float
158 3
    {
159
        return $this->quantity;
160 3
    }
161
162
    /**
163
     * @param float $quantity
164
     * @return OrderLine
165
     */
166
    public function setQuantity(float $quantity) : self
167 3
    {
168
        $this->quantity = $quantity;
169 3
        return $this;
170 3
    }
171 3
172
    /**
173
     * @return float
174
     */
175
    public function getUnitPrice() : float
176
    {
177 3
        return $this->unitPrice;
178
    }
179 3
180
    /**
181
     * @param float $unitPrice
182
     * @return OrderLine
183
     */
184
    public function setUnitPrice(float $unitPrice) : self
185
    {
186 3
        $this->unitPrice = $unitPrice;
187
        return $this;
188 3
    }
189 3
190 3
    /**
191
     * @return float
192
     */
193
    public function getTaxPercent() : ?float
194
    {
195
        return $this->taxPercent;
196 3
    }
197
198 3
    /**
199
     * @param float $taxPercent
200
     * @return OrderLine
201
     */
202
    public function setTaxPercent(float $taxPercent) : self
203
    {
204
        $this->taxPercent = $taxPercent;
205 3
        return $this;
206
    }
207 3
208 3
    /**
209 3
     * @return float
210
     */
211
    public function getTaxAmount() : ?float
212
    {
213
        return $this->taxAmount;
214
    }
215 3
216
    /**
217 3
     * @param float $taxAmount
218
     * @return OrderLine
219
     */
220
    public function setTaxAmount(float $taxAmount) : self
221
    {
222
        $this->taxAmount = $taxAmount;
223
        return $this;
224 3
    }
225
226 3
    /**
227 3
     * @return string
228 3
     */
229
    public function getUnitCode() : ?string
230
    {
231
        return $this->unitCode;
232
    }
233
234 3
    /**
235
     * @param string $unitCode
236 3
     * @return OrderLine
237
     */
238
    public function setUnitCode(string $unitCode) : self
239
    {
240
        $this->unitCode = $unitCode;
241
        return $this;
242
    }
243 3
244
    /**
245 3
     * @return float
246 3
     */
247 3
    public function getDiscount() : ?float
248
    {
249
        return $this->discount;
250
    }
251
252
    /**
253 3
     * @param float $discount
254
     * @return OrderLine
255 3
     */
256
    public function setDiscount(float $discount) : self
257
    {
258
        $this->discount = $discount;
259
        return $this;
260
    }
261
262 3
    /**
263
     * @return string
264 3
     */
265 3
    public function getGoodsType() : ?string
266 3
    {
267
        return $this->goodsType;
268
    }
269
270
    /**
271
     * @param string $goodsType
272 3
     * @return OrderLine
273
     */
274 3
    public function setGoodsType(string $goodsType) : self
275
    {
276
        $this->goodsType = $goodsType;
277
        return $this;
278
    }
279
280
    /**
281 3
     * @return string
282
     */
283 3
    public function getImageUrl() : ?string
284 3
    {
285 3
        return $this->imageUrl;
286
    }
287
288
    /**
289
     * @param string $imageUrl
290
     * @return OrderLine
291 3
     */
292
    public function setImageUrl(string $imageUrl) : self
293 3
    {
294
        $this->imageUrl = $imageUrl;
295
        return $this;
296
    }
297
}
298