Failed Conditions
Push — master ( 825113...ddc2dd )
by Adrien
02:43
created

EntryTransactionDetail::addReference()   D

Complexity

Conditions 18
Paths 6

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 18

Importance

Changes 0
Metric Value
cc 18
eloc 22
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 31
rs 4.8666
ccs 23
cts 23
cp 1
crap 18

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

184
                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...
185 10
                    $unstructuredBlockExists = true;
186 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

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