Price   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 46
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sum() 0 6 1
A fromDecimal() 0 3 1
A multiply() 0 4 1
A add() 0 4 1
A __construct() 0 5 1
A getWithVat() 0 3 1
1
<?php
2
3
namespace Simara\Cart\Domain;
4
5
use Litipk\BigNumbers\Decimal;
6
7
final class Price
8
{
9
    private const DECIMALS = 2;
10
11
    private Decimal $withVat;
12
13 33
    public function __construct(string $withVat)
14
    {
15 33
        $withHigherPrecision = Decimal::fromString($withVat, self::DECIMALS + 1);
16 33
        $truncated = $withHigherPrecision->floor(self::DECIMALS);
17 33
        $this->withVat = $truncated;
18 33
    }
19
20
    /**
21
     * @param self[] $prices
22
     * @return self
23
     */
24 18
    public static function sum(array $prices): self
25
    {
26 18
        return array_reduce(
27 18
            $prices,
28 18
            fn(self $carry, self $price) => $carry->add($price),
29 18
            new self('0')
30
        );
31
    }
32
33 7
    public function getWithVat(): string
34
    {
35 7
        return (string) $this->withVat->floor(self::DECIMALS);
36
    }
37
38 16
    public function add(self $adder): self
39
    {
40 16
        $withVat = $this->withVat->add($adder->withVat);
41 16
        return self::fromDecimal($withVat);
42
    }
43
44 16
    public function multiply(int $multiplier): self
45
    {
46 16
        $withVat = $this->withVat->mul(Decimal::fromInteger($multiplier));
47 16
        return self::fromDecimal($withVat);
48
    }
49
50 18
    private static function fromDecimal(Decimal $withVat): self
51
    {
52 18
        return new self($withVat->floor(self::DECIMALS)->__toString());
53
    }
54
}
55