Completed
Push — master ( 9396da...236110 )
by Joachim
15:12
created

OrderLine::getUnitPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Loevgaard\DandomainFoundation\Entity;
6
7
use Brick\Math\BigDecimal;
8
use Doctrine\ORM\Mapping as ORM;
9
use Loevgaard\DandomainFoundation;
10
use Loevgaard\DandomainFoundation\Entity\Generated\OrderLineInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...ated\OrderLineInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Loevgaard\DandomainFoundation\Entity\Generated\OrderLineTrait;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...enerated\OrderLineTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Loevgaard\DandomainFoundation\Entity\Generated\ProductInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...erated\ProductInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Money\Money;
14
15
/**
16
 * @ORM\Entity()
17
 * @ORM\Table(name="ldf_order_lines")
18
 */
19
class OrderLine extends AbstractEntity implements OrderLineInterface
20
{
21
    use OrderLineTrait;
22
23
    protected $hydrateConversions = [
24
        'id' => 'externalId',
25
        'productId' => 'productNumber',
26
    ];
27
28
    /**
29
     * @var int
30
     *
31
     * @ORM\Id
32
     * @ORM\GeneratedValue
33
     * @ORM\Column(type="integer")
34
     **/
35
    protected $id;
36
37
    /**
38
     * @var int
39
     *
40
     * @ORM\Column(type="integer", unique=true)
41
     */
42
    protected $externalId;
43
44
    /**
45
     * @var string|null
46
     *
47
     * @ORM\Column(nullable=true, type="string", length=191)
48
     */
49
    protected $fileUrl;
50
51
    /**
52
     * @var string|null
53
     *
54
     * @ORM\Column(nullable=true, type="string", length=191)
55
     */
56
    protected $productNumber;
57
58
    /**
59
     * @var string|null
60
     *
61
     * @ORM\Column(nullable=true, type="text")
62
     */
63
    protected $productName;
64
65
    /**
66
     * @var int|null
67
     *
68
     * @ORM\Column(nullable=true, type="integer")
69
     */
70
    protected $quantity;
71
72
    /**
73
     * This number is excl vat.
74
     *
75
     * @var int|null
76
     *
77
     * @ORM\Column(nullable=true, type="integer")
78
     */
79
    protected $unitPrice;
80
81
    /**
82
     * This number is excl vat.
83
     *
84
     * @var int|null
85
     *
86
     * @ORM\Column(nullable=true, type="integer")
87
     */
88
    protected $totalPrice;
89
90
    /**
91
     * @var float|null
92
     *
93
     * @ORM\Column(nullable=true, type="decimal", precision=5, scale=2)
94
     */
95
    protected $vatPct;
96
97
    /**
98
     * @var string|null
99
     *
100
     * @ORM\Column(nullable=true, type="string", length=191)
101
     */
102
    protected $variant;
103
104
    /**
105
     * @var string|null
106
     *
107
     * @ORM\Column(nullable=true, type="text")
108
     */
109
    protected $xmlParams;
110
111
    /**
112
     * @var Order
113
     *
114
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
115
     * @ORM\ManyToOne(inversedBy="orderLines", targetEntity="Order")
116
     */
117
    protected $order;
118
119
    /**
120
     * @var ProductInterface|null
121
     *
122
     * @ORM\JoinColumn(onDelete="SET NULL")
123
     * @ORM\ManyToOne(targetEntity="Product", cascade={"persist"})
124
     */
125
    protected $product;
126 6
127
    // @todo implement withVat and withoutVat methods
128 6
129 3
    public function hydrate(array $data, bool $useConversions = false, $scalarsOnly = true)
130
    {
131
        if (is_null($this->order)) {
132 3
            throw new \RuntimeException('Cannot hydrate order line without an associated order');
133
        }
134 3
135 3
        if (isset($data['unitPrice'])) {
136
            $data['unitPrice'] = DandomainFoundation\createMoneyFromFloat($this->getCurrencyCode(), $data['unitPrice']);
0 ignored issues
show
Bug introduced by
The function createMoneyFromFloat was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
            $data['unitPrice'] = /** @scrutinizer ignore-call */ DandomainFoundation\createMoneyFromFloat($this->getCurrencyCode(), $data['unitPrice']);
Loading history...
137
        }
138 3
139 3
        if (isset($data['totalPrice'])) {
140
            $data['totalPrice'] = DandomainFoundation\createMoneyFromFloat($this->getCurrencyCode(), $data['totalPrice']);
141
        }
142 3
143 3
        parent::hydrate($data, $useConversions, $scalarsOnly);
144
    }
145
146
    /**
147
     * This method copies properties from $orderLine onto this order line.
148
     *
149
     * @param OrderLineInterface $orderLine
150
     */
151
    public function copyProperties(OrderLineInterface $orderLine): void
152
    {
153
        $this->setExternalId($orderLine->getExternalId());
154
        $this->setFileUrl($orderLine->getFileUrl());
155
        $this->setProductNumber($orderLine->getProductNumber());
156
        $this->setProductName($orderLine->getProductName());
157
        $this->setQuantity($orderLine->getQuantity());
158
        $this->setUnitPrice($orderLine->getUnitPrice());
159
        $this->setTotalPrice($orderLine->getTotalPrice());
160
        $this->setVatPct($orderLine->getVatPct());
161
        $this->setVariant($orderLine->getVariant());
162
        $this->setXmlParams($orderLine->getXmlParams());
163
        $this->setOrder($orderLine->getOrder());
164
        $this->setProduct($orderLine->getProduct());
165
    }
166
167
    /*
168
     * Helper methods
169
     */
170
    public function getUnitPriceInclVat(): ?Money
171
    {
172
        $unitPrice = $this->getUnitPrice();
173
        if (!$unitPrice) {
174
            return null;
175
        }
176
177
        $multiplier = BigDecimal::of('100')->plus($this->vatPct)->exactlyDividedBy('100');
0 ignored issues
show
Bug introduced by
The method exactlyDividedBy() does not exist on Brick\Math\BigRational. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        $multiplier = BigDecimal::of('100')->plus($this->vatPct)->/** @scrutinizer ignore-call */ exactlyDividedBy('100');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method exactlyDividedBy() does not exist on Brick\Math\BigInteger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        $multiplier = BigDecimal::of('100')->plus($this->vatPct)->/** @scrutinizer ignore-call */ exactlyDividedBy('100');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
179
        return $unitPrice->multiply((string) $multiplier);
180
    }
181
182
    public function getUnitPriceExclVat(): ?Money
183
    {
184
        return $this->getUnitPrice();
185 6
    }
186
187 6
    public function getTotalPriceInclVat(): ?Money
188
    {
189
        $totalPrice = $this->getTotalPrice();
190
        if (!$totalPrice) {
191
            return null;
192
        }
193
194 3
        $multiplier = BigDecimal::of('100')->plus($this->vatPct)->exactlyDividedBy('100');
195
196 3
        return $totalPrice->multiply((string) $multiplier);
197 3
    }
198
199
    public function getTotalPriceExclVat(): ?Money
200
    {
201
        return $this->getTotalPrice();
202
    }
203 21
204
    /**
205 21
     * @return int
206
     */
207
    public function getId(): int
208
    {
209
        return (int) $this->id;
210
    }
211
212 18
    /**
213
     * @param int $id
214 18
     *
215 18
     * @return OrderLineInterface
216
     */
217
    public function setId(int $id)
218
    {
219
        $this->id = $id;
220
221 3
        return $this;
222
    }
223 3
224
    /**
225
     * @return int
226
     */
227
    public function getExternalId(): int
228
    {
229
        return (int) $this->externalId;
230 3
    }
231
232 3
    /**
233 3
     * @param int $externalId
234
     *
235
     * @return OrderLineInterface
236
     */
237
    public function setExternalId(int $externalId)
238
    {
239 3
        $this->externalId = $externalId;
240
241 3
        return $this;
242
    }
243
244
    /**
245
     * @return null|string
246
     */
247
    public function getFileUrl()
248 3
    {
249
        return $this->fileUrl;
250 3
    }
251 3
252
    /**
253
     * @param null|string $fileUrl
254
     *
255
     * @return OrderLineInterface
256
     */
257 3
    public function setFileUrl($fileUrl)
258
    {
259 3
        $this->fileUrl = $fileUrl;
260
261
        return $this;
262
    }
263
264
    /**
265
     * @return string|null
266 3
     */
267
    public function getProductNumber()
268 3
    {
269 3
        return $this->productNumber;
270
    }
271
272
    /**
273
     * @param string|null $productNumber
274
     *
275 3
     * @return OrderLineInterface
276
     */
277 3
    public function setProductNumber($productNumber)
278
    {
279
        $this->productNumber = $productNumber;
280
281
        return $this;
282
    }
283
284 3
    /**
285
     * @return null|string
286 3
     */
287 3
    public function getProductName()
288
    {
289
        return $this->productName;
290
    }
291
292
    /**
293 3
     * @param null|string $productName
294
     *
295 3
     * @return OrderLineInterface
296
     */
297
    public function setProductName($productName)
298
    {
299
        $this->productName = $productName;
300
301
        return $this;
302 3
    }
303
304 3
    /**
305
     * @return int|null
306 3
     */
307
    public function getQuantity()
308
    {
309
        return $this->quantity;
310
    }
311
312 3
    /**
313
     * @param int|null $quantity
314 3
     *
315
     * @return OrderLineInterface
316
     */
317
    public function setQuantity($quantity)
318
    {
319
        $this->quantity = $quantity;
320
321 3
        return $this;
322
    }
323 3
324 3
    /**
325
     * @return Money|null
326
     */
327
    public function getTotalPrice()
328
    {
329
        return DandomainFoundation\createMoney($this->getCurrencyCode(), (int) $this->totalPrice);
0 ignored issues
show
Bug introduced by
The function createMoney was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

329
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->getCurrencyCode(), (int) $this->totalPrice);
Loading history...
330 3
    }
331
332 3
    /**
333
     * @param Money|null $totalPrice
334
     *
335
     * @return OrderLineInterface
336
     */
337
    public function setTotalPrice(Money $totalPrice = null)
338
    {
339 3
        $this->totalPrice = $totalPrice ? $totalPrice->getAmount() : $totalPrice;
340
341 3
        return $this;
342 3
    }
343
344
    /**
345
     * @return Money|null
346
     */
347
    public function getUnitPrice()
348 3
    {
349
        return DandomainFoundation\createMoney($this->getCurrencyCode(), (int) $this->unitPrice);
0 ignored issues
show
Bug introduced by
The function createMoney was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

349
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->getCurrencyCode(), (int) $this->unitPrice);
Loading history...
350 3
    }
351
352
    /**
353
     * @param Money|null $unitPrice
354
     *
355
     * @return OrderLineInterface
356
     */
357 3
    public function setUnitPrice(Money $unitPrice = null)
358
    {
359 3
        $this->unitPrice = $unitPrice ? $unitPrice->getAmount() : $unitPrice;
360 3
361
        return $this;
362
    }
363
364
    /**
365
     * @return float
366 3
     */
367
    public function getVatPct() : float
368 3
    {
369
        return (float)$this->vatPct;
370
    }
371
372
    /**
373
     * @param float|null $vatPct
374
     *
375 3
     * @return OrderLineInterface
376
     */
377 3
    public function setVatPct($vatPct)
378 3
    {
379
        $this->vatPct = $vatPct;
380
381
        return $this;
382
    }
383
384 6
    /**
385
     * @return null|string
386 6
     */
387
    public function getVariant()
388
    {
389
        return $this->variant;
390
    }
391
392
    /**
393 18
     * @param null|string $variant
394
     *
395 18
     * @return OrderLineInterface
396 18
     */
397
    public function setVariant($variant)
398
    {
399
        $this->variant = $variant;
400
401
        return $this;
402 3
    }
403
404 3
    /**
405
     * @return null|string
406
     */
407
    public function getXmlParams()
408
    {
409
        return $this->xmlParams;
410
    }
411 3
412
    /**
413 3
     * @param null|string $xmlParams
414 3
     *
415
     * @return OrderLineInterface
416
     */
417
    public function setXmlParams($xmlParams)
418
    {
419
        $this->xmlParams = $xmlParams;
420
421
        return $this;
422
    }
423
424
    /**
425
     * @return Order
426
     */
427
    public function getOrder(): ?Order
428
    {
429
        return $this->order;
430
    }
431
432
    /**
433
     * @param Order|null $order
434
     *
435
     * @return OrderLineInterface
436
     */
437
    public function setOrder(?Order $order)
438
    {
439
        $this->order = $order;
440
441
        return $this;
442
    }
443
444
    /**
445
     * @return ProductInterface|null
446
     */
447
    public function getProduct()
448
    {
449
        return $this->product;
450
    }
451
452
    /**
453
     * @param ProductInterface|null $product
454
     *
455
     * @return OrderLineInterface
456
     */
457
    public function setProduct(ProductInterface $product = null)
458
    {
459
        $this->product = $product;
460
461
        return $this;
462
    }
463
464
    protected function getCurrencyCode(): string
465
    {
466
        return $this->getOrder()->getCurrency()->getIsoCodeAlpha();
467
    }
468
}
469