Record::addEntries()   F
last analyzed

Complexity

Conditions 34
Paths 8961

Size

Total Lines 119
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 74.8297

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 34
eloc 67
nc 8961
nop 2
dl 0
loc 119
ccs 43
cts 64
cp 0.6719
crap 74.8297
rs 0
c 4
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
            if (isset($xmlEntry->CdtDbtInd) && in_array((string) $xmlEntry->CdtDbtInd, ['CRDT', 'DBIT'], true)) {
125 22
                $entry->setCreditDebitIndicator((string) $xmlEntry->CdtDbtInd);
126 21
            }
127 21
128 21
            $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

128
            $entry->setStatus($this->readStatus(/** @scrutinizer ignore-type */ $xmlEntry));
Loading history...
129
130
            if (isset($xmlEntry->BkTxCd)) {
131 21
                $bankTransactionCode = new DTO\BankTransactionCode();
132
133
                if (isset($xmlEntry->BkTxCd->Prtry)) {
134 22
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
135 18
                        (string) $xmlEntry->BkTxCd->Prtry->Cd,
136 18
                        (string) $xmlEntry->BkTxCd->Prtry->Issr
137
                    );
138
139 18
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
140 18
                }
141 18
142 18
                if (isset($xmlEntry->BkTxCd->Domn)) {
143
                    $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
144
                        (string) $xmlEntry->BkTxCd->Domn->Cd
145 18
                    );
146
147
                    if (isset($xmlEntry->BkTxCd->Domn->Fmly)) {
148 18
                        $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
149
                            (string) $xmlEntry->BkTxCd->Domn->Fmly->Cd,
150
                            (string) $xmlEntry->BkTxCd->Domn->Fmly->SubFmlyCd
151 22
                        );
152
153
                        $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
154 23
                    }
155
156
                    $bankTransactionCode->setDomain($domainBankTransactionCode);
157
                }
158
159
                $entry->setBankTransactionCode($bankTransactionCode);
160
            }
161
162
            if (isset($xmlEntry->Chrgs)) {
163
                $charges = new DTO\Charges();
164
165
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
166
                    $money = $this->moneyFactory->create($xmlEntry->Chrgs->TtlChrgsAndTaxAmt, null);
167
                    $charges->setTotalChargesAndTaxAmount($money);
168
                }
169
170
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
171
                if ($chargesRecords) {
172
                    /** @var SimpleXMLElement $chargesRecord */
173
                    foreach ($chargesRecords as $chargesRecord) {
174
                        $chargesDetail = new DTO\ChargesRecord();
175
176
                        if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
177
                            $money = $this->moneyFactory->create($chargesRecord->Amt, $chargesRecord->CdtDbtInd);
178
179
                            $chargesDetail->setAmount($money);
180
                        }
181
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
182
                            $chargesDetail->setChargesIncludedIndicator(true);
183
                        }
184
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
185
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
186
                        }
187
                        $charges->addRecord($chargesDetail);
188
                    }
189
                }
190
                $entry->setCharges($charges);
191
            }
192
193 23
            $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

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