Passed
Push — master ( 5b6c6f...489807 )
by Adrien
02:20
created

Record::addBalances()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 30
ccs 22
cts 22
cp 1
crap 7
rs 8.6506
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Genkgo\Camt\Decoder;
6
7
use Genkgo\Camt\DTO;
8
use Genkgo\Camt\DTO\RecordWithBalances;
9
use Genkgo\Camt\Util\StringToUnits;
10
use Money\Money;
11
use Money\Currency;
12
use \SimpleXMLElement;
13
14
class Record
15
{
16
    /**
17
     * @var Entry
18
     */
19
    private $entryDecoder;
20
    /**
21
     * @var DateDecoderInterface
22
     */
23
    private $dateDecoder;
24
25
    /**
26
     * Record constructor.
27
     * @param Entry $entryDecoder
28
     * @param DateDecoderInterface $dateDecoder
29
     */
30 33
    public function __construct(Entry $entryDecoder, DateDecoderInterface $dateDecoder)
31
    {
32 33
        $this->entryDecoder = $entryDecoder;
33 33
        $this->dateDecoder = $dateDecoder;
34 33
    }
35
36
    /**
37
     * @param RecordWithBalances $record
38
     * @param SimpleXMLElement $xmlRecord
39
     */
40 20
    public function addBalances(RecordWithBalances $record, SimpleXMLElement $xmlRecord): void
41
    {
42 20
        $xmlBalances = $xmlRecord->Bal;
43 20
        foreach ($xmlBalances as $xmlBalance) {
44 15
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
45 15
            $currency = (string)$xmlBalance->Amt['Ccy'];
46 15
            $date = (string)$xmlBalance->Dt->Dt;
47
48 15
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
49 10
                $amount = $amount * -1;
50
            }
51
52 15
            if (isset($xmlBalance->Tp) && isset($xmlBalance->Tp->CdOrPrtry)) {
53 15
                $code = (string) $xmlBalance->Tp->CdOrPrtry->Cd;
54
55 15
                if (in_array($code, ['OPBD', 'PRCD'])) {
56 12
                    $record->addBalance(DTO\Balance::opening(
57 12
                        new Money(
58 12
                            $amount,
59 12
                            new Currency($currency)
60
                        ),
61 12
                        $this->dateDecoder->decode($date)
62
                    ));
63 14
                } elseif ($code === 'CLBD') {
64 11
                    $record->addBalance(DTO\Balance::closing(
65 11
                        new Money(
66 11
                            $amount,
67 11
                            new Currency($currency)
68
                        ),
69 11
                        $this->dateDecoder->decode($date)
70
                    ));
71
                }
72
            }
73
        }
74 20
    }
75
76
    /**
77
     * @param DTO\Record       $record
78
     * @param SimpleXMLElement $xmlRecord
79
     */
80 24
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord): void
81
    {
82 24
        $index = 0;
83 24
        $xmlEntries = $xmlRecord->Ntry;
84 24
        foreach ($xmlEntries as $xmlEntry) {
85 23
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
86 23
            $currency    = (string)$xmlEntry->Amt['Ccy'];
87 23
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
88 23
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
89 23
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
90
91 23
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
92 13
                $amount = $amount * -1;
93
            }
94
95 23
            $entry = new DTO\Entry(
96 23
                $record,
97 23
                $index,
98 23
                new Money($amount, new Currency($currency))
99
            );
100
101 23
            if ($bookingDate) {
102 22
                $entry->setBookingDate($this->dateDecoder->decode($bookingDate));
103
            }
104
105 23
            if ($valueDate) {
106 22
                $entry->setValueDate($this->dateDecoder->decode($valueDate));
107
            }
108
109 23
            $entry->setAdditionalInfo($additionalInfo);
110
111 23
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
112 1
                $entry->setReversalIndicator(true);
113
            }
114
115 23
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
116 1
                $entry->setReference((string) $xmlEntry->NtryRef);
117
            }
118
119 23
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
120 19
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
121
            }
122
123 23
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
124 12
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
125
            }
126
127 23
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
128 4
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
129
            }
130
131 23
            if (isset($xmlEntry->BkTxCd)) {
132 22
                $bankTransactionCode = new DTO\BankTransactionCode();
133
134 22
                if (isset($xmlEntry->BkTxCd->Prtry)) {
135 21
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
136 21
                        (string)$xmlEntry->BkTxCd->Prtry->Cd,
137 21
                        (string)$xmlEntry->BkTxCd->Prtry->Issr
138
                    );
139
140 21
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
141
                }
142
143 22
                if (isset($xmlEntry->BkTxCd->Domn)) {
144 18
                    $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
145 18
                        (string)$xmlEntry->BkTxCd->Domn->Cd
146
                    );
147
148 18
                    if (isset($xmlEntry->BkTxCd->Domn->Fmly)) {
149 18
                        $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
150 18
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->Cd,
151 18
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->SubFmlyCd
152
                        );
153
154 18
                        $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
155
                    }
156
157 18
                    $bankTransactionCode->setDomain($domainBankTransactionCode);
158
                }
159
160 22
                $entry->setBankTransactionCode($bankTransactionCode);
161
            }
162
163 23
            if (isset($xmlEntry->Chrgs)) {
164
                $charges = new DTO\Charges();
165
166
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
167
                    $amount      = StringToUnits::convert((string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt);
168
                    $currency    = (string)$xmlEntry->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
169
170
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
171
                }
172
173
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
174
                if ($chargesRecords) {
175
176
                    /** @var SimpleXMLElement $chargesRecord */
177
                    foreach ($chargesRecords as $chargesRecord) {
178
                        $chargesDetail = new DTO\ChargesRecord();
179
180
                        if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
181
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
182
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
183
184
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
185
                                $amount = $amount * -1;
186
                            }
187
188
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
189
                        }
190
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
191
                            $chargesDetail->setChargesIncludedIndicator(true);
192
                        }
193
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
194
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
195
                        }
196
                        $charges->addRecord($chargesDetail);
197
                    }
198
                }
199
                $entry->setCharges($charges);
200
            }
201
202 23
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
203
204 23
            $record->addEntry($entry);
205 23
            $index++;
206
        }
207 24
    }
208
}
209