Failed Conditions
Pull Request — master (#138)
by Adrien
01:51
created

Record::readStatus()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
ccs 2
cts 2
cp 1
crap 4
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\MoneyFactory;
10
use SimpleXMLElement;
11
12
class Record
13
{
14
    private Entry $entryDecoder;
15
16
    private DateDecoderInterface $dateDecoder;
17
18
    private MoneyFactory $moneyFactory;
19
20
    /**
21
     * Record constructor.
22
     */
23
    public function __construct(Entry $entryDecoder, DateDecoderInterface $dateDecoder)
24
    {
25
        $this->entryDecoder = $entryDecoder;
26
        $this->dateDecoder = $dateDecoder;
27
        $this->moneyFactory = new MoneyFactory();
28
    }
29 33
30
    public function addBalances(RecordWithBalances $record, SimpleXMLElement $xmlRecord): void
31 33
    {
32 33
        $xmlBalances = $xmlRecord->Bal;
33 33
        foreach ($xmlBalances as $xmlBalance) {
34
            $money = $this->moneyFactory->create($xmlBalance->Amt, $xmlBalance->CdtDbtInd);
35 20
            $date = $this->dateDecoder->decode((string) $xmlBalance->Dt->Dt);
36
37 20
            if (!isset($xmlBalance->Tp, $xmlBalance->Tp->CdOrPrtry)) {
38 20
                continue;
39 15
            }
40 15
            $code = (string) $xmlBalance->Tp->CdOrPrtry->Cd;
41 15
42
            switch ($code) {
43 15
                case 'OPBD':
44 10
                case 'PRCD':
45
                    $record->addBalance(DTO\Balance::opening(
46
                        $money,
47 15
                        $date
48 15
                    ));
49
50 15
                    break;
51 12
                case 'OPAV':
52 12
                    $record->addBalance(DTO\Balance::openingAvailable(
53
                        $money,
54 12
                        $date
55
                    ));
56 12
57
                    break;
58 14
                case 'CLBD':
59 11
                    $record->addBalance(DTO\Balance::closing(
60 11
                        $money,
61
                        $date
62 11
                    ));
63
64 11
                    break;
65
                case 'CLAV':
66
                    $record->addBalance(DTO\Balance::closingAvailable(
67
                        $money,
68
                        $date
69 20
                    ));
70
71 24
                    break;
72
                default:
73 24
                    break;
74 24
            }
75 24
        }
76 23
    }
77 23
78 23
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord): void
79 23
    {
80 23
        $index = 0;
81
        $xmlEntries = $xmlRecord->Ntry;
82 23
        foreach ($xmlEntries as $xmlEntry) {
83 13
            $money = $this->moneyFactory->create($xmlEntry->Amt, $xmlEntry->CdtDbtInd);
84
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
85
            $valueDate = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
86 23
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
87
88
            $entry = new DTO\Entry(
89 23
                $record,
90
                $index,
91
                $money
92 23
            );
93 22
94
            if ($bookingDate) {
95
                $entry->setBookingDate($this->dateDecoder->decode($bookingDate));
96 23
            }
97 22
98
            if ($valueDate) {
99
                $entry->setValueDate($this->dateDecoder->decode($valueDate));
100 23
            }
101
102 23
            $entry->setAdditionalInfo($additionalInfo);
103 1
104
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
105
                $entry->setReversalIndicator(true);
106 23
            }
107 1
108
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
109
                $entry->setReference((string) $xmlEntry->NtryRef);
110 23
            }
111 19
112
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
113
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
114 23
            }
115 12
116
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
117
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
118 23
            }
119 4
120
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
121
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
122 23
            }
123 22
124
            $entry->setStatus($this->readStatus($xmlEntry));
0 ignored issues
show
Bug introduced by
It seems like $xmlEntry can also be of type null; however, parameter $xmlEntry of Genkgo\Camt\Decoder\Record::readStatus() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
            $entry->setStatus($this->readStatus(/** @scrutinizer ignore-type */ $xmlEntry));
Loading history...
125 22
126 21
            if (isset($xmlEntry->BkTxCd)) {
127 21
                $bankTransactionCode = new DTO\BankTransactionCode();
128 21
129
                if (isset($xmlEntry->BkTxCd->Prtry)) {
130
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
131 21
                        (string) $xmlEntry->BkTxCd->Prtry->Cd,
132
                        (string) $xmlEntry->BkTxCd->Prtry->Issr
133
                    );
134 22
135 18
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
136 18
                }
137
138
                if (isset($xmlEntry->BkTxCd->Domn)) {
139 18
                    $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
140 18
                        (string) $xmlEntry->BkTxCd->Domn->Cd
141 18
                    );
142 18
143
                    if (isset($xmlEntry->BkTxCd->Domn->Fmly)) {
144
                        $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
145 18
                            (string) $xmlEntry->BkTxCd->Domn->Fmly->Cd,
146
                            (string) $xmlEntry->BkTxCd->Domn->Fmly->SubFmlyCd
147
                        );
148 18
149
                        $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
150
                    }
151 22
152
                    $bankTransactionCode->setDomain($domainBankTransactionCode);
153
                }
154 23
155
                $entry->setBankTransactionCode($bankTransactionCode);
156
            }
157
158
            if (isset($xmlEntry->Chrgs)) {
159
                $charges = new DTO\Charges();
160
161
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
162
                    $money = $this->moneyFactory->create($xmlEntry->Chrgs->TtlChrgsAndTaxAmt, null);
163
                    $charges->setTotalChargesAndTaxAmount($money);
164
                }
165
166
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
167
                if ($chargesRecords) {
168
                    /** @var SimpleXMLElement $chargesRecord */
169
                    foreach ($chargesRecords as $chargesRecord) {
170
                        $chargesDetail = new DTO\ChargesRecord();
171
172
                        if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
173
                            $money = $this->moneyFactory->create($chargesRecord->Amt, $chargesRecord->CdtDbtInd);
174
175
                            $chargesDetail->setAmount($money);
176
                        }
177
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
178
                            $chargesDetail->setChargesIncludedIndicator(true);
179
                        }
180
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
181
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
182
                        }
183
                        $charges->addRecord($chargesDetail);
184
                    }
185
                }
186
                $entry->setCharges($charges);
187
            }
188
189
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
0 ignored issues
show
Bug introduced by
It seems like $xmlEntry can also be of type null; however, parameter $xmlEntry of Genkgo\Camt\Decoder\Entry::addTransactionDetails() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

189
            $this->entryDecoder->addTransactionDetails($entry, /** @scrutinizer ignore-type */ $xmlEntry);
Loading history...
190
191
            $record->addEntry($entry);
192
            ++$index;
193 23
        }
194
    }
195 23
196 23
    private function readStatus(SimpleXMLElement $xmlEntry): ?string
197
    {
198 24
        $xmlStatus = $xmlEntry->Sts;
199
200
        // CAMT v08 uses substructure, so we check for its existence or fallback to the element itself to keep compatibility with CAMT v04
201
        return (string) $xmlStatus?->Cd
202
            ?: (string) $xmlStatus?->Prtry
203
                ?: (string) $xmlStatus
204
                    ?: null;
205
    }
206
}
207