Failed Conditions
Push — master ( 65cf51...40aca8 )
by Adrien
03:16
created

EntryTransactionDetail::addCharges()   C

Complexity

Conditions 13
Paths 5

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13.0139

Importance

Changes 0
Metric Value
cc 13
eloc 22
nc 5
nop 2
dl 0
loc 37
ccs 22
cts 23
cp 0.9565
crap 13.0139
rs 6.6166
c 0
b 0
f 0

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

181
                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...
182 10
                    $unstructuredBlockExists = true;
183 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

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