Passed
Push — master ( 6ba141...e4b471 )
by Adrien
02:06
created

EntryTransactionDetail::createMoney()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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

165
                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...
166
                    $unstructuredBlockExists = true;
167
                    $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

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