Failed Conditions
Pull Request — master (#92)
by
unknown
08:11
created

EntryTransactionDetail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 17
    public function __construct(DateDecoderInterface $dateDecoder)
26
    {
27 17
        $this->dateDecoder = $dateDecoder;
28 17
    }
29
30 2
    public function addReference(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
31
    {
32 2
        if (false === isset($xmlDetail->Refs)) {
33 1
            return;
34
        }
35
36 1
        $refs = $xmlDetail->Refs;
37 1
        $reference = new DTO\Reference();
38
39 1
        $reference->setMessageId(isset($refs->MsgId) ? (string) $refs->MsgId : null);
40 1
        $reference->setAccountServicerReference(isset($refs->AcctSvcrRef) ? (string) $refs->AcctSvcrRef : null);
41 1
        $reference->setPaymentInformationId(isset($refs->PmtInfId) ? (string) $refs->PmtInfId : null);
42 1
        $reference->setInstructionId(isset($refs->InstrId) ? (string) $refs->InstrId : null);
43 1
        $reference->setEndToEndId(isset($refs->EndToEndId) ? (string) $refs->EndToEndId : null);
44 1
        $reference->setTransactionId(isset($refs->TxId) ? (string) $refs->TxId : null);
45 1
        $reference->setMandateId(isset($refs->MndtId) ? (string) $refs->MndtId : null);
46 1
        $reference->setChequeNumber(isset($refs->ChqNb) ? (string) $refs->ChqNb : null);
47 1
        $reference->setClearingSystemReference(isset($refs->ClrSysRef) ? (string) $refs->ClrSysRef : null);
48 1
        $reference->setAccountOwnerTransactionId(isset($refs->AcctOwnrTxId) ? (string) $refs->AcctOwnrTxId : null);
49 1
        $reference->setAccountServicerTransactionId(isset($refs->AcctSvcrTxId) ? (string) $refs->AcctSvcrTxId : null);
50 1
        $reference->setMarketInfrastructureTransactionId(isset($refs->MktInfrstrctrTxId) ? (string) $refs->MktInfrstrctrTxId : null);
51 1
        $reference->setProcessingId(isset($refs->PrcgId) ? (string) $refs->PrcgId : null);
52
53 1
        foreach ($refs->Prtry as $xmlProprietary) {
54
            $type = isset($xmlProprietary->Tp) ? (string) $xmlProprietary->Tp : null;
55
            $subReference = isset($xmlProprietary->Ref) ? (string) $xmlProprietary->Ref : null;
56
57
            $reference->addProprietary(new DTO\ProprietaryReference($type, $subReference));
58
        }
59
60 1
        $detail->setReference($reference);
61 1
    }
62
63 1
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
64
    {
65 1
        if (false === isset($xmlDetail->RltdPties)) {
66 1
            return;
67
        }
68
69
        /** @var SimpleXMLElement $xmlRelatedParty */
70
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
71
            if (isset($xmlRelatedParty->Cdtr)) {
72
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
73
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct;
74
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
75
                $relatedPartyType = new DTO\Creditor($xmlRelatedPartyName);
76
77
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
78
            }
79
80
            if (isset($xmlRelatedParty->UltmtCdtr)) {
81
                $xmlRelatedPartyType = $xmlRelatedParty->UltmtCdtr;
82
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
83
                $relatedPartyType = new DTO\UltimateCreditor($xmlRelatedPartyName);
84
85
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType);
86
            }
87
88
            if (isset($xmlRelatedParty->Dbtr)) {
89
                $xmlRelatedPartyType = $xmlRelatedParty->Dbtr;
90
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct;
91
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
92
                $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
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
95
            }
96
97
            if (isset($xmlRelatedParty->UltmtDbtr)) {
98
                $xmlRelatedPartyType = $xmlRelatedParty->UltmtDbtr;
99
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '';
100
                $relatedPartyType = new DTO\UltimateDebtor($xmlRelatedPartyName);
101
102
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType);
103
            }
104
        }
105
    }
106
107
    protected function addRelatedParty(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlRelatedPartyType, DTO\RelatedPartyTypeInterface $relatedPartyType, ?SimpleXMLElement $xmlRelatedPartyTypeAccount = null): RelatedParty
108
    {
109
        if (isset($xmlRelatedPartyType->PstlAdr)) {
110
            $relatedPartyType->setAddress(DTOFactory\Address::createFromXml($xmlRelatedPartyType->PstlAdr));
111
        }
112
        if ($xmlRelatedPartyType->Id->PrvtId->Othr->Id) {
113
            $relatedPartyType->setId((string)$xmlRelatedPartyType->Id->PrvtId->Othr->Id);
114
        }
115
        if ($xmlRelatedPartyType->Id->PrvtId->Othr->SchmeNm->Cd) {
116
            $relatedPartyType->setTypeName((string)$xmlRelatedPartyType->Id->PrvtId->Othr->SchmeNm->Cd);
117
        }
118
119
        $relatedParty = new RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount));
120
121
        $detail->addRelatedParty($relatedParty);
122
123
        return $relatedParty;
124
    }
125
126
    public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
127
    {
128
        if (false === isset($xmlDetail->RltdAgts)) {
129
            return;
130
        }
131
132
        foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) {
133
            if (isset($xmlRelatedAgent->CdtrAgt)) {
134
                $agent = new DTO\CreditorAgent((string) $xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string) $xmlRelatedAgent->CdtrAgt->FinInstnId->BIC);
135
                $relatedAgent = new DTO\RelatedAgent($agent);
136
                $detail->addRelatedAgent($relatedAgent);
137
            }
138
139
            if (isset($xmlRelatedAgent->DbtrAgt)) {
140
                $agent = new DTO\DebtorAgent((string) $xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string) $xmlRelatedAgent->DbtrAgt->FinInstnId->BIC);
141
                $relatedAgent = new DTO\RelatedAgent($agent);
142
                $detail->addRelatedAgent($relatedAgent);
143
            }
144
        }
145
    }
146
147 3
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
148
    {
149 3
        if (false === isset($xmlDetail->RmtInf)) {
150 1
            return;
151
        }
152
153 2
        $remittanceInformation = new DTO\RemittanceInformation();
154 2
        $unstructuredBlockExists = false;
155
156
        // Unstructured blocks
157 2
        $xmlDetailsUnstructuredBlocks = $xmlDetail->RmtInf->Ustrd;
158 2
        if ($xmlDetailsUnstructuredBlocks !== null) {
159 2
            foreach ($xmlDetailsUnstructuredBlocks as $xmlDetailsUnstructuredBlock) {
160 1
                $unstructuredRemittanceInformation = new DTO\UnstructuredRemittanceInformation(
161 1
                    (string) $xmlDetailsUnstructuredBlock
162
                );
163
164 1
                $remittanceInformation->addUnstructuredBlock($unstructuredRemittanceInformation);
165
166
                // Legacy : use the very first unstructured block
167 1
                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 1
                    $unstructuredBlockExists = true;
169 1
                    $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 1
                        (string) $xmlDetailsUnstructuredBlock
171
                    );
172
                }
173
            }
174
        }
175
176
        // Strutcured blocks
177 2
        $xmlDetailsStructuredBlocks = $xmlDetail->RmtInf->Strd;
178 2
        if ($xmlDetailsStructuredBlocks !== null) {
179 2
            foreach ($xmlDetailsStructuredBlocks as $xmlDetailsStructuredBlock) {
180 1
                $structuredRemittanceInformation = new DTO\StructuredRemittanceInformation();
181
182 1
                if (isset($xmlDetailsStructuredBlock->AddtlRmtInf)) {
183
                    $structuredRemittanceInformation->setAdditionalRemittanceInformation(
184
                        (string) $xmlDetailsStructuredBlock->AddtlRmtInf
185
                    );
186
                }
187
188 1
                if (isset($xmlDetailsStructuredBlock->CdtrRefInf)) {
189 1
                    $creditorReferenceInformation = new DTO\CreditorReferenceInformation();
190
191 1
                    if (isset($xmlDetailsStructuredBlock->CdtrRefInf->Ref)) {
192 1
                        $creditorReferenceInformation->setRef(
193 1
                            (string) $xmlDetailsStructuredBlock->CdtrRefInf->Ref
194
                        );
195
                    }
196
197 1
                    if (isset($xmlDetailsStructuredBlock->CdtrRefInf->Tp, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Prtry)
198
199
                         ) {
200
                        $creditorReferenceInformation->setProprietary(
201
                            (string) $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Prtry
202
                        );
203
                    }
204
205 1
                    if (isset($xmlDetailsStructuredBlock->CdtrRefInf->Tp, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry, $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Cd)
206
207
                         ) {
208
                        $creditorReferenceInformation->setCode(
209
                            (string) $xmlDetailsStructuredBlock->CdtrRefInf->Tp->CdOrPrtry->Cd
210
                        );
211
                    }
212
213 1
                    $structuredRemittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
214
215
                    // Legacy : do not overwrite message if already defined above
216
                    // and no creditor reference is already defined
217 1
                    if (false === $unstructuredBlockExists
218 1
                        && $remittanceInformation->getCreditorReferenceInformation() === null) {
219 1
                        $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
220
                    }
221
                }
222
223 1
                $remittanceInformation->addStructuredBlock($structuredRemittanceInformation);
224
            }
225
        }
226
227 2
        $detail->setRemittanceInformation($remittanceInformation);
228 2
    }
229
230 2
    public function addRelatedDates(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
231
    {
232 2
        if (false === isset($xmlDetail->RltdDts)) {
233 1
            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 2
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
247
    {
248 2
        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 2
    }
256
257 2
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
258
    {
259 2
        if (isset($xmlDetail->AddtlTxInf)) {
260 1
            $additionalInformation = new DTO\AdditionalTransactionInformation(
261 1
                (string) $xmlDetail->AddtlTxInf
262
            );
263 1
            $detail->setAdditionalTransactionInformation($additionalInformation);
264
        }
265 2
    }
266
267
    public function addBankTransactionCode(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
268
    {
269
        $bankTransactionCode = new DTO\BankTransactionCode();
270
271
        if (isset($xmlDetail->BkTxCd)) {
272
            $bankTransactionCode = new DTO\BankTransactionCode();
273
274
            if (isset($xmlDetail->BkTxCd->Prtry)) {
275
                $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
276
                    (string) $xmlDetail->BkTxCd->Prtry->Cd,
277
                    (string) $xmlDetail->BkTxCd->Prtry->Issr
278
                );
279
280
                $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
281
            }
282
283
            if (isset($xmlDetail->BkTxCd->Domn)) {
284
                $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
285
                    (string) $xmlDetail->BkTxCd->Domn->Cd
286
                );
287
288
                if (isset($xmlDetail->BkTxCd->Domn->Fmly)) {
289
                    $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
290
                        (string) $xmlDetail->BkTxCd->Domn->Fmly->Cd,
291
                        (string) $xmlDetail->BkTxCd->Domn->Fmly->SubFmlyCd
292
                    );
293
294
                    $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
295
                }
296
297
                $bankTransactionCode->setDomain($domainBankTransactionCode);
298
            }
299
        }
300
301
        $detail->setBankTransactionCode($bankTransactionCode);
302
    }
303
304 2
    public function addCharges(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail): void
305
    {
306 2
        if (isset($xmlDetail->Chrgs)) {
307 1
            $charges = new DTO\Charges();
308
309 1
            if (isset($xmlDetail->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt) {
310 1
                $amount = StringToUnits::convert((string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt);
311 1
                $currency = (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
312
313 1
                $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
314
            }
315
316 1
            $chargesRecords = $xmlDetail->Chrgs->Rcrd;
317 1
            if ($chargesRecords !== null) {
318
319
                /** @var SimpleXMLElement $chargesRecord */
320 1
                foreach ($chargesRecords as $chargesRecord) {
321 1
                    $chargesDetail = new DTO\ChargesRecord();
322
323 1
                    if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
324 1
                        $amount = StringToUnits::convert((string) $chargesRecord->Amt);
325 1
                        $currency = (string) $chargesRecord->Amt['Ccy'];
326
327 1
                        if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
328 1
                            $amount = $amount * -1;
329
                        }
330
331 1
                        $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
332
                    }
333 1
                    if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
334
                        $chargesDetail->setChargesIncludedIndicator(true);
335
                    }
336 1
                    if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
337 1
                        $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
338
                    }
339 1
                    $charges->addRecord($chargesDetail);
340
                }
341
            }
342 1
            $detail->setCharges($charges);
343
        }
344 2
    }
345
346 1
    public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, SimpleXMLElement $CdtDbtInd): void
347
    {
348 1
        if (isset($xmlDetail->AmtDtls, $xmlDetail->AmtDtls->TxAmt, $xmlDetail->AmtDtls->TxAmt->Amt)) {
349 1
            $money = $this->createMoney($xmlDetail->AmtDtls->TxAmt->Amt, $CdtDbtInd);
350 1
            $detail->setAmountDetails($money);
351
        }
352 1
    }
353
354 1
    public function addAmount(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, SimpleXMLElement $CdtDbtInd): void
355
    {
356 1
        if (isset($xmlDetail->Amt)) {
357 1
            $money = $this->createMoney($xmlDetail->Amt, $CdtDbtInd);
358 1
            $detail->setAmount($money);
359
        }
360 1
    }
361
362 2
    private function createMoney(SimpleXMLElement $xmlAmount, SimpleXMLElement $CdtDbtInd): Money
363
    {
364 2
        $amount = StringToUnits::convert((string) $xmlAmount);
365
366 2
        if ((string) $CdtDbtInd === 'DBIT') {
367 2
            $amount = $amount * -1;
368
        }
369
370 2
        return new Money(
371 2
            $amount,
372 2
            new Currency((string) $xmlAmount['Ccy'])
373
        );
374
    }
375
376
    abstract public function getRelatedPartyAccount(?SimpleXMLElement $xmlRelatedPartyTypeAccount): ?DTO\Account;
377
}
378