Accountant::sum()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Assimtech\Fiat;
6
7
use InvalidArgumentException;
8
9
class Accountant
10
{
11
    /**
12
     * @throws InvalidArgumentException
13
     */
14 4
    protected function checkCurrenciesMatch(
15
        Money $money1,
16
        Money $money2
17
    ): void {
18 4
        if ($money1->getCurrency()->getCode() !== $money2->getCurrency()->getCode()) {
19 1
            throw new InvalidArgumentException(sprintf(
20 1
                'Cannot work with monies of differing currencies (%s, %s)',
21 1
                $money1,
22 1
                $money2
23
            ));
24
        }
25 3
    }
26
27
    /**
28
     * $money1 + $money2
29
     */
30 3
    public function add(
31
        Money $money1,
32
        Money $money2
33
    ): Money {
34 3
        $this->checkCurrenciesMatch($money1, $money2);
35
36 2
        return new Money(
37 2
            $money1->getAmount() + $money2->getAmount(),
38 2
            $money1->getCurrency()
39
        );
40
    }
41
42
    /**
43
     * $money1 - $money2
44
     */
45 1
    public function subtract(
46
        Money $money1,
47
        Money $money2
48
    ): Money {
49 1
        $this->checkCurrenciesMatch($money1, $money2);
50
51 1
        return new Money(
52 1
            $money1->getAmount() - $money2->getAmount(),
53 1
            $money1->getCurrency()
54
        );
55
    }
56
57
    /**
58
     * $money * $fraction
59
     *
60
     * @param float|integer $fraction
61
     */
62 1
    public function multiply(
63
        Money $money,
64
        $fraction
65
    ): Money {
66 1
        return new Money(
67 1
            $money->getAmount() * $fraction,
68 1
            $money->getCurrency()
69
        );
70
    }
71
72
    /**
73
     * $money / $fraction
74
     *
75
     * @param float|integer $fraction
76
     */
77 1
    public function divide(
78
        Money $money,
79
        $fraction
80
    ): Money {
81 1
        return new Money(
82 1
            $money->getAmount() / $fraction,
83 1
            $money->getCurrency()
84
        );
85
    }
86
87
    /**
88
     * @param array<Money> $monies
89
     */
90 2
    public function sum(
91
        array $monies
92
    ): ?Money {
93 2
        $totalMoney = null;
94
95 2
        foreach ($monies as $money) {
96 2
            if (!$money instanceof Money) {
97 1
                throw new InvalidArgumentException('$monies must be an array of Money');
98
            }
99
100 1
            if ($totalMoney === null) {
101 1
                $totalMoney = clone $money;
102
103 1
                continue;
104
            }
105
106 1
            $totalMoney = $this->add($totalMoney, $money);
107
        }
108
109 1
        return $totalMoney;
110
    }
111
}
112