Failed Conditions
Pull Request — master (#92)
by
unknown
02:39
created

EntryTransactionDetail::addAmountDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 23
        if ($Id = data_get($xmlRelatedPartyType, 'Id.PrvtId.Othr.Id')) {
113
            $relatedPartyType->setId((string) $Id);
114
        }
115 23
        if ($Cd = data_get($xmlRelatedPartyType, 'Id.PrvtId.Othr.SchmeNm.Cd')) {
116
            $relatedPartyType->setTypeName((string) $Cd);
117
        }
118
119 23
        $relatedParty = new RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount));
120
121 23
        $detail->addRelatedParty($relatedParty);
122
123 23
        return $relatedParty;
124
    }
125
126 22
    public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
127
    {
128 22
        if (false === isset($xmlDetail->RltdAgts)) {
129 17
            return;
130
        }
131
132 17
        foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) {
133 17
            if (isset($xmlRelatedAgent->CdtrAgt)) {
134 17
                $agent = new DTO\CreditorAgent((string) $xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string) $xmlRelatedAgent->CdtrAgt->FinInstnId->BIC);
135 17
                $relatedAgent = new DTO\RelatedAgent($agent);
136 17
                $detail->addRelatedAgent($relatedAgent);
137
            }
138
139 17
            if (isset($xmlRelatedAgent->DbtrAgt)) {
140 17
                $agent = new DTO\DebtorAgent((string) $xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string) $xmlRelatedAgent->DbtrAgt->FinInstnId->BIC);
141 17
                $relatedAgent = new DTO\RelatedAgent($agent);
142 17
                $detail->addRelatedAgent($relatedAgent);
143
            }
144
        }
145 17
    }
146
147 25
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
148
    {
149 25
        if (false === isset($xmlDetail->RmtInf)) {
150 8
            return;
151
        }
152
153 20
        $remittanceInformation = new DTO\RemittanceInformation();
154 20
        $unstructuredBlockExists = false;
155
156
        // Unstructured blocks
157 20
        $xmlDetailsUnstructuredBlocks = $xmlDetail->RmtInf->Ustrd;
158 20
        if ($xmlDetailsUnstructuredBlocks !== null) {
159 20
            foreach ($xmlDetailsUnstructuredBlocks as $xmlDetailsUnstructuredBlock) {
160 10
                $unstructuredRemittanceInformation = new DTO\UnstructuredRemittanceInformation(
161 10
                    (string) $xmlDetailsUnstructuredBlock
162
                );
163
164 10
                $remittanceInformation->addUnstructuredBlock($unstructuredRemittanceInformation);
165
166
                // Legacy : use the very first unstructured block
167 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

167
                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...
168 10
                    $unstructuredBlockExists = true;
169 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

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