Completed
Push — master ( b5ae25...349033 )
by Frederik
02:37
created

Record::addBalances()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
ccs 31
cts 31
cp 1
rs 8.439
cc 6
eloc 23
nc 5
nop 2
crap 6
1
<?php
2
3
namespace Genkgo\Camt\Decoder;
4
5
use Genkgo\Camt\DTO;
6
use Genkgo\Camt\Util\StringToUnits;
7
use Money\Money;
8
use Money\Currency;
9
use \SimpleXMLElement;
10
use \DateTimeImmutable;
11
12
class Record
13
{
14
    /**
15
     * @var Entry
16
     */
17
    private $entryDecoder;
18
19 22
    public function __construct(Entry $entryDecoder)
20
    {
21 22
        $this->entryDecoder = $entryDecoder;
22 22
    }
23
24
    /**
25
     * @param DTO\Record       $record
26
     * @param SimpleXMLElement $xmlRecord
27
     */
28 15
    public function addBalances(DTO\Record $record, SimpleXMLElement $xmlRecord)
29
    {
30 15
        $xmlBalances = $xmlRecord->Bal;
0 ignored issues
show
Bug introduced by
The property Bal does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31 15
        foreach ($xmlBalances as $xmlBalance) {
32 11
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
33 11
            $currency = (string)$xmlBalance->Amt['Ccy'];
34 11
            $date = (string)$xmlBalance->Dt->Dt;
35
36 11
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
37 9
                $amount = $amount * -1;
38 9
            }
39
40 11
            if (isset($xmlBalance->Tp)
41 11
                && isset($xmlBalance->Tp->CdOrPrtry)
42 11
                && (string) $xmlBalance->Tp->CdOrPrtry->Cd === 'OPBD'
43 11
            ) {
44 11
                $balance = DTO\Balance::opening(
45 11
                    new Money(
46 11
                        $amount,
47 11
                        new Currency($currency)
48 11
                    ),
49 11
                    new DateTimeImmutable($date)
50 11
                );
51 11
            } else {
52 10
                $balance = DTO\Balance::closing(
53 10
                    new Money(
54 10
                        $amount,
55 10
                        new Currency($currency)
56 10
                    ),
57 10
                    new DateTimeImmutable($date)
58 10
                );
59
            }
60
61 11
            $record->addBalance($balance);
62 15
        }
63 15
    }
64
65
    /**
66
     * @param DTO\Record       $record
67
     * @param SimpleXMLElement $xmlRecord
68
     */
69 15
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord)
70
    {
71 15
        $index = 0;
72 15
        $xmlEntries = $xmlRecord->Ntry;
0 ignored issues
show
Bug introduced by
The property Ntry does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73 15
        foreach ($xmlEntries as $xmlEntry) {
74 14
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
75 14
            $currency    = (string)$xmlEntry->Amt['Ccy'];
76 14
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
77 14
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
78
79 14
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
80 5
                $amount = $amount * -1;
81 5
            }
82
83 14
            $entry = new DTO\Entry(
84 14
                $record,
85 14
                $index,
86 14
                new Money($amount, new Currency($currency)),
87 14
                new DateTimeImmutable($bookingDate),
88 14
                new DateTimeImmutable($valueDate)
89 14
            );
90
91 14
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
92 1
                $entry->setReversalIndicator(true);
93 1
            }
94
95 14
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
96 1
                $entry->setReference((string) $xmlEntry->NtryRef);
97 1
            }
98
99 14
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
100 11
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
101 11
            }
102
103 14
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
104 1
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
105 1
            }
106
107 14
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
108
109 14
            $record->addEntry($entry);
110 14
            $index++;
111 15
        }
112 15
    }
113
}
114