MoneyFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Genkgo\Camt\Util;
6
7
use Money\Currencies\ISOCurrencies;
8
use Money\Currency;
9
use Money\Money;
10
use Money\Parser\DecimalMoneyParser;
11
use SimpleXMLElement;
12
13
final class MoneyFactory
14
{
15
    private DecimalMoneyParser $decimalMoneyParser;
16
17
    public function __construct()
18
    {
19
        $this->decimalMoneyParser = new DecimalMoneyParser(new ISOCurrencies());
20
    }
21
22
    public function create(SimpleXMLElement $xmlAmount, ?SimpleXMLElement $CdtDbtInd): Money
23
    {
24
        $amount = (string) $xmlAmount;
25
26
        if ((string) $CdtDbtInd === 'DBIT') {
27
            $amount = (string) ((float) $amount * -1);
28
        }
29
30
        /** @psalm-var non-empty-string $currency */
31
        $currency = (string) $xmlAmount['Ccy'];
32
33
        return $this->decimalMoneyParser->parse(
34
            $amount,
35
            new Currency($currency)
36
        );
37
    }
38
}
39