Failed Conditions
Push — master ( c3df43...3c8a24 )
by Adrien
02:34
created

EntryTransactionDetail::addRemittanceInformation()   C

Complexity

Conditions 14
Paths 5

Size

Total Lines 81
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 38
nc 5
nop 2
dl 0
loc 81
ccs 39
cts 39
cp 1
crap 14
rs 6.2666
c 1
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\Decoder\Factory\DTO as DTOFactory;
8
use Genkgo\Camt\DTO;
9
use Genkgo\Camt\DTO\RelatedParty;
10
use Genkgo\Camt\Util\StringToUnits;
11
use Money\Currency;
12
use Money\Money;
13
use SimpleXMLElement;
14
15
abstract class EntryTransactionDetail
16
{
17
    /**
18
     * @var DateDecoderInterface
19
     */
20
    private $dateDecoder;
21
22
    /**
23
     * EntryTransactionDetail constructor.
24
     */
25 40
    public function __construct(DateDecoderInterface $dateDecoder)
26
    {
27 40
        $this->dateDecoder = $dateDecoder;
28 40
    }
29
30 24
    public function addReference(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
31
    {
32 24
        if (false === isset($xmlDetail->Refs)) {
33 8
            return;
34
        }
35
36 19
        $refs = $xmlDetail->Refs;
37 19
        $reference = new DTO\Reference();
38
39 19
        $reference->setMessageId(isset($refs->MsgId) ? (string) $refs->MsgId : null);
40 19
        $reference->setAccountServicerReference(isset($refs->AcctSvcrRef) ? (string) $refs->AcctSvcrRef : null);
41 19
        $reference->setPaymentInformationId(isset($refs->PmtInfId) ? (string) $refs->PmtInfId : null);
42 19
        $reference->setInstructionId(isset($refs->InstrId) ? (string) $refs->InstrId : null);
43 19
        $reference->setEndToEndId(isset($refs->EndToEndId) ? (string) $refs->EndToEndId : null);
44 19
        $reference->setTransactionId(isset($refs->TxId) ? (string) $refs->TxId : null);
45 19
        $reference->setMandateId(isset($refs->MndtId) ? (string) $refs->MndtId : null);
46 19
        $reference->setChequeNumber(isset($refs->ChqNb) ? (string) $refs->ChqNb : null);
47 19
        $reference->setClearingSystemReference(isset($refs->ClrSysRef) ? (string) $refs->ClrSysRef : null);
48 19
        $reference->setAccountOwnerTransactionId(isset($refs->AcctOwnrTxId) ? (string) $refs->AcctOwnrTxId : null);
49 19
        $reference->setAccountServicerTransactionId(isset($refs->AcctSvcrTxId) ? (string) $refs->AcctSvcrTxId : null);
50 19
        $reference->setMarketInfrastructureTransactionId(isset($refs->MktInfrstrctrTxId) ? (string) $refs->MktInfrstrctrTxId : null);
51 19
        $reference->setProcessingId(isset($refs->PrcgId) ? (string) $refs->PrcgId : null);
52
53 19
        foreach ($refs->Prtry as $xmlProprietary) {
54 9
            $type = isset($xmlProprietary->Tp) ? (string) $xmlProprietary->Tp : null;
55 9
            $subReference = isset($xmlProprietary->Ref) ? (string) $xmlProprietary->Ref : null;
56
57 9
            $reference->addProprietary(new DTO\ProprietaryReference($type, $subReference));
58
        }
59
60 19
        $detail->setReference($reference);
61 19
    }
62
63 24
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
64
    {
65 24
        if (false === isset($xmlDetail->RltdPties)) {
66 1
            return;
67
        }
68
69
        /** @var SimpleXMLElement $xmlRelatedParty */
70 23
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
71 23
            if (isset($xmlRelatedParty->Cdtr)) {
72 23
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
73 23
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct;
74 23
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
75 23
                $relatedPartyType = new DTO\Creditor($xmlRelatedPartyName);
76
77 23
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
78
            }
79
80 23
            if (isset($xmlRelatedParty->UltmtCdtr)) {
81 5
                $xmlRelatedPartyType = $xmlRelatedParty->UltmtCdtr;
82 5
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
83 5
                $relatedPartyType = new DTO\UltimateCreditor($xmlRelatedPartyName);
84
85 5
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType);
86
            }
87
88 23
            if (isset($xmlRelatedParty->Dbtr)) {
89 20
                $xmlRelatedPartyType = $xmlRelatedParty->Dbtr;
90 20
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct;
91 20
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
92 20
                $relatedPartyType = $debtor = new DTO\Debtor($xmlRelatedPartyName);
0 ignored issues
show
Unused Code introduced by
The assignment to $debtor is dead and can be removed.
Loading history...
93
94 20
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
95
            }
96
97 23
            if (isset($xmlRelatedParty->UltmtDbtr)) {
98 5
                $xmlRelatedPartyType = $xmlRelatedParty->UltmtDbtr;
99 5
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
100 5
                $relatedPartyType = new DTO\UltimateDebtor($xmlRelatedPartyName);
101
102 5
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType);
103
            }
104
        }
105 23
    }
106
107 23
    protected function addRelatedParty(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlRelatedPartyType, DTO\RelatedPartyTypeInterface $relatedPartyType, ?SimpleXMLElement $xmlRelatedPartyTypeAccount = null): RelatedParty
108
    {
109 23
        if (isset($xmlRelatedPartyType->PstlAdr)) {
110 23
            $relatedPartyType->setAddress(DTOFactory\Address::createFromXml($xmlRelatedPartyType->PstlAdr));
111
        }
112
113 23
        $relatedParty = new RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount));
114
115 23
        $detail->addRelatedParty($relatedParty);
116
117 23
        return $relatedParty;
118
    }
119
120 22
    public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
121
    {
122 22
        if (false === isset($xmlDetail->RltdAgts)) {
123 14
            return;
124
        }
125
126 17
        foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) {
127 17
            if (isset($xmlRelatedAgent->CdtrAgt)) {
128 17
                $agent = new DTO\CreditorAgent((string) $xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string) $xmlRelatedAgent->CdtrAgt->FinInstnId->BIC);
129 17
                $relatedAgent = new DTO\RelatedAgent($agent);
130 17
                $detail->addRelatedAgent($relatedAgent);
131
            }
132
133 17
            if (isset($xmlRelatedAgent->DbtrAgt)) {
134 17
                $agent = new DTO\DebtorAgent((string) $xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string) $xmlRelatedAgent->DbtrAgt->FinInstnId->BIC);
135 17
                $relatedAgent = new DTO\RelatedAgent($agent);
136 17
                $detail->addRelatedAgent($relatedAgent);
137
            }
138
        }
139 17
    }
140
141 25
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
142
    {
143 25
        if (false === isset($xmlDetail->RmtInf)) {
144 8
            return;
145
        }
146
147 20
        $remittanceInformation = new DTO\RemittanceInformation();
148 20
        $unstructuredBlockExists = false;
149
150
        // Unstructured blocks
151 20
        $xmlDetailsUnstructuredBlocks = $xmlDetail->RmtInf->Ustrd;
152 20
        if ($xmlDetailsUnstructuredBlocks !== null) {
153 20
            foreach ($xmlDetailsUnstructuredBlocks as $xmlDetailsUnstructuredBlock) {
154 10
                $unstructuredRemittanceInformation = new DTO\UnstructuredRemittanceInformation(
155 10
                    (string) $xmlDetailsUnstructuredBlock
156
                );
157
158 10
                $remittanceInformation->addUnstructuredBlock($unstructuredRemittanceInformation);
159
160
                // Legacy : use the very first unstructured block
161 10
                if ($remittanceInformation->getMessage() === null) {
0 ignored issues
show
Deprecated Code introduced by
The function Genkgo\Camt\DTO\Remittan...formation::getMessage() has been deprecated: Use getStructuredBlocks method instead ( Ignorable by Annotation )

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

161
                if (/** @scrutinizer ignore-deprecated */ $remittanceInformation->getMessage() === null) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
162 10
                    $unstructuredBlockExists = true;
163 10
                    $remittanceInformation->setMessage(
0 ignored issues
show
Deprecated Code introduced by
The function Genkgo\Camt\DTO\Remittan...formation::setMessage() has been deprecated: Use addStructuredBlock method instead ( Ignorable by Annotation )

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

163
                    /** @scrutinizer ignore-deprecated */ $remittanceInformation->setMessage(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
164 10
                        (string) $xmlDetailsUnstructuredBlock
165
                    );
166
                }
167
            }
168
        }
169
170
        // Strutcured blocks
171 20
        $xmlDetailsStructuredBlocks = $xmlDetail->RmtInf->Strd;
172 20
        if ($xmlDetailsStructuredBlocks !== null) {
173 20
            foreach ($xmlDetailsStructuredBlocks as $xmlDetailsStructuredBlock) {
174 14
                $structuredRemittanceInformation = new DTO\StructuredRemittanceInformation();
175
176 14
                if (isset($xmlDetailsStructuredBlock->AddtlRmtInf)) {
177 4
                    $structuredRemittanceInformation->setAdditionalRemittanceInformation(
178 4
                        (string) $xmlDetailsStructuredBlock->AddtlRmtInf
179
                    );
180
                }
181
182 14
                if (isset($xmlDetailsStructuredBlock->CdtrRefInf)) {
183 14
                    $creditorReferenceInformation = new DTO\CreditorReferenceInformation();
184
185 14
                    if (isset($xmlDetailsStructuredBlock->CdtrRefInf->Ref)) {
186 14
                        $creditorReferenceInformation->setRef(
187 14
                            (string) $xmlDetailsStructuredBlock->CdtrRefInf->Ref
188
                        );
189
                    }
190
191 14
                    if (isset($xmlDetailsStructuredBlock->CdtrRefInf->Tp, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Prtry)
192
193
                         ) {
194 4
                        $creditorReferenceInformation->setProprietary(
195 4
                            (string) $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Prtry
196
                        );
197
                    }
198
199 14
                    if (isset($xmlDetailsStructuredBlock->CdtrRefInf->Tp, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Cd)
200
201
                         ) {
202 13
                        $creditorReferenceInformation->setCode(
203 13
                            (string) $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Cd
204
                        );
205
                    }
206
207 14
                    $structuredRemittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
208
209
                    // Legacy : do not overwrite message if already defined above
210
                    // and no creditor reference is already defined
211 14
                    if (false === $unstructuredBlockExists
212 14
                        && $remittanceInformation->getCreditorReferenceInformation() === null) {
213 14
                        $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
214
                    }
215
                }
216
217 14
                $remittanceInformation->addStructuredBlock($structuredRemittanceInformation);
218
            }
219
        }
220
221 20
        $detail->setRemittanceInformation($remittanceInformation);
222 20
    }
223
224 24
    public function addRelatedDates(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
225
    {
226 24
        if (false === isset($xmlDetail->RltdDts)) {
227 23
            return;
228
        }
229
230 1
        if (isset($xmlDetail->RltdDts->AccptncDtTm)) {
231 1
            $RelatedDates = DTO\RelatedDates::fromUnstructured(
232 1
                $this->dateDecoder->decode((string) $xmlDetail->RltdDts->AccptncDtTm)
233
            );
234 1
            $detail->setRelatedDates($RelatedDates);
235
236 1
            return;
237
        }
238
    }
239
240 24
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
241
    {
242 24
        if (isset($xmlDetail->RtrInf, $xmlDetail->RtrInf->Rsn->Cd)) {
243 1
            $remittanceInformation = DTO\ReturnInformation::fromUnstructured(
244 1
                (string) $xmlDetail->RtrInf->Rsn->Cd,
245 1
                (string) $xmlDetail->RtrInf->AddtlInf
246
            );
247 1
            $detail->setReturnInformation($remittanceInformation);
248
        }
249 24
    }
250
251 24
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
252
    {
253 24
        if (isset($xmlDetail->AddtlTxInf)) {
254 1
            $additionalInformation = new DTO\AdditionalTransactionInformation(
255 1
                (string) $xmlDetail->AddtlTxInf
256
            );
257 1
            $detail->setAdditionalTransactionInformation($additionalInformation);
258
        }
259 24
    }
260
261 22
    public function addBankTransactionCode(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
262
    {
263 22
        $bankTransactionCode = new DTO\BankTransactionCode();
264
265 22
        if (isset($xmlDetail->BkTxCd)) {
266 20
            $bankTransactionCode = new DTO\BankTransactionCode();
267
268 20
            if (isset($xmlDetail->BkTxCd->Prtry)) {
269 20
                $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
270 20
                    (string) $xmlDetail->BkTxCd->Prtry->Cd,
271 20
                    (string) $xmlDetail->BkTxCd->Prtry->Issr
272
                );
273
274 20
                $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
275
            }
276
277 20
            if (isset($xmlDetail->BkTxCd->Domn)) {
278 7
                $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
279 7
                    (string) $xmlDetail->BkTxCd->Domn->Cd
280
                );
281
282 7
                if (isset($xmlDetail->BkTxCd->Domn->Fmly)) {
283 7
                    $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
284 7
                        (string) $xmlDetail->BkTxCd->Domn->Fmly->Cd,
285 7
                        (string) $xmlDetail->BkTxCd->Domn->Fmly->SubFmlyCd
286
                    );
287
288 7
                    $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
289
                }
290
291 7
                $bankTransactionCode->setDomain($domainBankTransactionCode);
292
            }
293
        }
294
295 22
        $detail->setBankTransactionCode($bankTransactionCode);
296 22
    }
297
298 24
    public function addCharges(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
299
    {
300 24
        if (isset($xmlDetail->Chrgs)) {
301 4
            $charges = new DTO\Charges();
302
303 4
            if (isset($xmlDetail->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt) {
304 4
                $amount = StringToUnits::convert((string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt);
305 4
                $currency = (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
306
307 4
                $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
308
            }
309
310 4
            $chargesRecords = $xmlDetail->Chrgs->Rcrd;
311 4
            if ($chargesRecords !== null) {
312
313
                /** @var SimpleXMLElement $chargesRecord */
314 4
                foreach ($chargesRecords as $chargesRecord) {
315 4
                    $chargesDetail = new DTO\ChargesRecord();
316
317 4
                    if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
318 4
                        $amount = StringToUnits::convert((string) $chargesRecord->Amt);
319 4
                        $currency = (string) $chargesRecord->Amt['Ccy'];
320
321 4
                        if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
322 4
                            $amount = $amount * -1;
323
                        }
324
325 4
                        $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
326
                    }
327 4
                    if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
328
                        $chargesDetail->setChargesIncludedIndicator(true);
329
                    }
330 4
                    if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
331 4
                        $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
332
                    }
333 4
                    $charges->addRecord($chargesDetail);
334
                }
335
            }
336 4
            $detail->setCharges($charges);
337
        }
338 24
    }
339
340 23
    public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, SimpleXMLElement $CdtDbtInd): void
341
    {
342 23
        if (isset($xmlDetail->AmtDtls, $xmlDetail->AmtDtls->TxAmt, $xmlDetail->AmtDtls->TxAmt->Amt)) {
343 8
            $money = $this->createMoney($xmlDetail->AmtDtls->TxAmt->Amt, $CdtDbtInd);
344 8
            $detail->setAmountDetails($money);
345
        }
346 23
    }
347
348 23
    public function addAmount(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, SimpleXMLElement $CdtDbtInd): void
349
    {
350 23
        if (isset($xmlDetail->Amt)) {
351 15
            $money = $this->createMoney($xmlDetail->Amt, $CdtDbtInd);
352 15
            $detail->setAmount($money);
353
        }
354 23
    }
355
356 16
    private function createMoney(SimpleXMLElement $xmlAmount, SimpleXMLElement $CdtDbtInd): Money
357
    {
358 16
        $amount = StringToUnits::convert((string) $xmlAmount);
359
360 16
        if ((string) $CdtDbtInd === 'DBIT') {
361 9
            $amount = $amount * -1;
362
        }
363
364 16
        return new Money(
365 16
            $amount,
366 16
            new Currency((string) $xmlAmount['Ccy'])
367
        );
368
    }
369
370
    abstract public function getRelatedPartyAccount(?SimpleXMLElement $xmlRelatedPartyTypeAccount): ?DTO\Account;
371
}
372