Completed
Push — master ( 262ce1...670d7e )
by Frederik
01:40
created

EntryTransactionDetail   F

Complexity

Total Complexity 77

Size/Duplication

Total Lines 373
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 30

Test Coverage

Coverage 98.9%

Importance

Changes 0
Metric Value
wmc 77
lcom 1
cbo 30
dl 0
loc 373
ccs 179
cts 181
cp 0.989
rs 1.3043
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D addReferences() 0 32 18
A addAdditionalTransactionInformation() 0 9 2
C addRelatedParties() 0 42 11
A addRelatedParty() 0 12 2
B addRelatedAgents() 0 20 5
B addRemittanceInformation() 0 27 6
A addRelatedDates() 0 14 3
A addReturnInformation() 0 10 3
B addBankTransactionCode() 0 36 5
C addCharges() 0 40 13
B addAmountDetails() 0 21 5
A addAmount() 0 20 3
getRelatedPartyAccount() 0 1 ?

How to fix   Complexity   

Complex Class

Complex classes like EntryTransactionDetail often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EntryTransactionDetail, and based on these observations, apply Extract Interface, too.

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 36
    public function __construct(DateDecoderInterface $dateDecoder)
24
    {
25 36
        $this->dateDecoder = $dateDecoder;
26 36
    }
27
28
    /**
29
     * @param DTO\EntryTransactionDetail $detail
30
     * @param SimpleXMLElement           $xmlDetail
31
     */
32 22
    public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
33
    {
34 22
        if (false === isset($xmlDetail->Refs)) {
35 8
            return;
36
        }
37
38 17
        $refs = $xmlDetail->Refs;
0 ignored issues
show
Bug introduced by
The property Refs does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
39 17
        $reference = new DTO\Reference();
40
41 17
        $reference->setMessageId(isset($refs->MsgId) ? (string) $refs->MsgId : null);
42 17
        $reference->setAccountServiceReference(isset($refs->AcctSvcrRef) ? (string) $refs->AcctSvcrRef : null);
43 17
        $reference->setPaymentInformationId(isset($refs->PmtInfId) ? (string) $refs->PmtInfId : null);
44 17
        $reference->setInstructionId(isset($refs->InstrId) ? (string) $refs->InstrId : null);
45 17
        $reference->setEndToEndId(isset($refs->EndToEndId) ? (string) $refs->EndToEndId : null);
46 17
        $reference->setTransactionId(isset($refs->TxId) ? (string) $refs->TxId : null);
47 17
        $reference->setMandateId(isset($refs->MndtId) ? (string) $refs->MndtId : null);
48 17
        $reference->setChequeNumber(isset($refs->ChqNb) ? (string) $refs->ChqNb : null);
49 17
        $reference->setClearingSystemReference(isset($refs->ClrSysRef) ? (string) $refs->ClrSysRef : null);
50 17
        $reference->setAccountOwnerTransactionId(isset($refs->AcctOwnrTxId) ? (string) $refs->AcctOwnrTxId : null);
51 17
        $reference->setAccountServicerTransactionId(isset($refs->AcctSvcrTxId) ? (string) $refs->AcctSvcrTxId : null);
52 17
        $reference->setMarketInfrastructureTransactionId(isset($refs->MktInfrstrctrTxId) ? (string) $refs->MktInfrstrctrTxId : null);
53 17
        $reference->setProcessingId(isset($refs->PrcgId) ? (string) $refs->PrcgId : null);
54
55 17
        foreach ($refs->Prtry as $xmlProprietary) {
56 8
            $type = isset($xmlProprietary->Tp) ? (string) $xmlProprietary->Tp : null;
57 8
            $subReference = isset($xmlProprietary->Ref) ? (string) $xmlProprietary->Ref : null;
58
59 8
            $reference->addProprietary(new DTO\ProprietaryReference($type, $subReference));
60
        }
61
62 17
        $detail->addReference($reference);
63 17
    }
64
65
    /**
66
     * @param DTO\EntryTransactionDetail $detail
67
     * @param SimpleXMLElement           $xmlDetail
68
     */
69 22
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
70
    {
71 22
        if (false === isset($xmlDetail->RltdPties)) {
72 1
            return;
73
        }
74
75 21
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
76 21
            if (isset($xmlRelatedParty->Cdtr)) {
77 21
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
78 21
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct;
79 21
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '' ;
80 21
                $relatedPartyType = $creditor = new DTO\Creditor($xmlRelatedPartyName);
0 ignored issues
show
Unused Code introduced by
$creditor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
82 21
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
83
            }
84
85 21
            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);
0 ignored issues
show
Unused Code introduced by
$creditor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
90 5
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType);
91
            }
92
93 21
            if (isset($xmlRelatedParty->Dbtr)) {
94 18
                $xmlRelatedPartyType = $xmlRelatedParty->Dbtr;
95 18
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct;
96 18
                $xmlRelatedPartyName = (isset($xmlRelatedPartyType->Nm)) ? (string) $xmlRelatedPartyType->Nm : '' ;
97 18
                $relatedPartyType = $debtor = new DTO\Debtor($xmlRelatedPartyName);
0 ignored issues
show
Unused Code introduced by
$debtor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
99 18
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
100
            }
101
102 21
            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);
0 ignored issues
show
Unused Code introduced by
$creditor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
107 21
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType);
108
            }
109
        }
110 21
    }
111
112
    /**
113
     * @param DTO\EntryTransactionDetail $detail
114
     * @param $xmlRelatedPartyType
115
     * @param $relatedPartyType
116
     * @param $xmlRelatedPartyTypeAccount
117
     * @return DTO\RelatedParty
118
     */
119 21
    protected function addRelatedParty(DTO\EntryTransactionDetail $detail, $xmlRelatedPartyType, DTO\RelatedPartyTypeInterface $relatedPartyType, $xmlRelatedPartyTypeAccount = null)
120
    {
121 21
        if (isset($xmlRelatedPartyType->PstlAdr)) {
122 21
            $relatedPartyType->setAddress(DTOFactory\Address::createFromXml($xmlRelatedPartyType->PstlAdr));
123
        }
124
125 21
        $relatedParty = new DTO\RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount));
126
127 21
        $detail->addRelatedParty($relatedParty);
128
129 21
        return $relatedParty;
130
    }
131
132
    /**
133
     * @param DTO\EntryTransactionDetail $detail
134
     * @param SimpleXMLElement           $xmlDetail
135
     */
136 20
    public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
137
    {
138 20
        if (false === isset($xmlDetail->RltdAgts)) {
139 12
            return;
140
        }
141
142 16
        foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) {
143 16
            if (isset($xmlRelatedAgent->CdtrAgt)) {
144 16
                $agent = new DTO\CreditorAgent((string)$xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->CdtrAgt->FinInstnId->BIC);
145 16
                $relatedAgent =  new DTO\RelatedAgent($agent);
146 16
                $detail->addRelatedAgent($relatedAgent);
147
            }
148
149 16
            if (isset($xmlRelatedAgent->DbtrAgt)) {
150 16
                $agent = new DTO\DebtorAgent((string)$xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->DbtrAgt->FinInstnId->BIC);
151 16
                $relatedAgent =  new DTO\RelatedAgent($agent);
152 16
                $detail->addRelatedAgent($relatedAgent);
153
            }
154
        }
155 16
    }
156
157
    /**
158
     * @param DTO\EntryTransactionDetail $detail
159
     * @param SimpleXMLElement           $xmlDetail
160
     */
161 23
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
162
    {
163 23
        if (false === isset($xmlDetail->RmtInf)) {
164 11
            return;
165
        }
166
167 18
        if (isset($xmlDetail->RmtInf->Ustrd)) {
168 9
            $remittanceInformation = DTO\RemittanceInformation::fromUnstructured(
169 9
                (string)$xmlDetail->RmtInf->Ustrd
170
            );
171 9
            $detail->setRemittanceInformation($remittanceInformation);
172
173 9
            return;
174
        }
175
176 9
        if (isset($xmlDetail->RmtInf->Strd)
177 9
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf)
178 9
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref)
179
        ) {
180 9
            $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured(
181 9
                (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref
182
            );
183 9
            $remittanceInformation = new DTO\RemittanceInformation();
184 9
            $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
185 9
            $detail->setRemittanceInformation($remittanceInformation);
186
        }
187 9
    }
188
189
    /**
190
     * @param DTO\EntryTransactionDetail $detail
191
     * @param SimpleXMLElement           $xmlDetail
192
     */
193 22
    public function addRelatedDates(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
194
    {
195 22
        if (false === isset($xmlDetail->RltdDts)) {
196 21
            return;
197
        }
198
199 1
        if (isset($xmlDetail->RltdDts->AccptncDtTm)) {
200 1
            $RelatedDates = DTO\RelatedDates::fromUnstructured(
201 1
                $this->dateDecoder->decode((string) $xmlDetail->RltdDts->AccptncDtTm )
202
            );
203 1
            $detail->setRelatedDates($RelatedDates);
204 1
            return;
205
        }
206
    }
207
208
209
210
    /**
211
     * @param DTO\EntryTransactionDetail $detail
212
     * @param SimpleXMLElement           $xmlDetail
213
     */
214 22
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
215
    {
216 22
        if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) {
217 1
            $remittanceInformation = DTO\ReturnInformation::fromUnstructured(
218 1
                (string)$xmlDetail->RtrInf->Rsn->Cd,
219 1
                (string)$xmlDetail->RtrInf->AddtlInf
220
            );
221 1
            $detail->setReturnInformation($remittanceInformation);
222
        }
223 22
    }
224
225
    /**
226
     * @param DTO\EntryTransactionDetail $detail
227
     * @param SimpleXMLElement           $xmlDetail
228
     */
229 22
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
230
    {
231 22
        if (isset($xmlDetail->AddtlTxInf)) {
232 1
            $additionalInformation = new DTO\AdditionalTransactionInformation(
233 1
                (string) $xmlDetail->AddtlTxInf
234
            );
235 1
            $detail->setAdditionalTransactionInformation($additionalInformation);
236
        }
237 22
    }
238
239
    /**
240
     * @param DTO\EntryTransactionDetail $detail
241
     * @param SimpleXMLElement           $xmlDetail
242
     */
243 20
    public function addBankTransactionCode(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
244
    {
245 20
        $bankTransactionCode = new DTO\BankTransactionCode();
246
247 20
        if (isset($xmlDetail->BkTxCd)) {
248 18
            $bankTransactionCode = new DTO\BankTransactionCode();
249
250 18
            if (isset($xmlDetail->BkTxCd->Prtry)) {
251 18
                $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
252 18
                    (string)$xmlDetail->BkTxCd->Prtry->Cd,
253 18
                    (string)$xmlDetail->BkTxCd->Prtry->Issr
254
                );
255
256 18
                $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
257
            }
258
259 18
            if (isset($xmlDetail->BkTxCd->Domn)) {
260 6
                $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
261 6
                    (string)$xmlDetail->BkTxCd->Domn->Cd
262
                );
263
264 6
                if (isset($xmlDetail->BkTxCd->Domn->Fmly)) {
265 6
                    $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
266 6
                        (string)$xmlDetail->BkTxCd->Domn->Fmly->Cd,
267 6
                        (string)$xmlDetail->BkTxCd->Domn->Fmly->SubFmlyCd
268
                    );
269
270 6
                    $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
271
                }
272
273 6
                $bankTransactionCode->setDomain($domainBankTransactionCode);
274
            }
275
        }
276
277 20
        $detail->setBankTransactionCode($bankTransactionCode);
278 20
    }
279
280
    /**
281
     * @param DTO\EntryTransactionDetail $detail
282
     * @param SimpleXMLElement           $xmlDetail
283
     */
284 22
    public function addCharges(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
285
    {
286 22
            if (isset($xmlDetail->Chrgs)) {
287 4
                $charges = new DTO\Charges();
288
289 4
                if (isset($xmlDetail->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt) {
290 4
                    $amount      = StringToUnits::convert((string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt);
291 4
                    $currency    = (string)$xmlDetail->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
292
293 4
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
294
                }
295
296 4
                $chargesRecords = $xmlDetail->Chrgs->Rcrd;
297 4
                if ($chargesRecords) {
298 4
                    foreach ($chargesRecords as $chargesRecord) {
299
300 4
                        $chargesDetail = new DTO\ChargesRecord();
301
302 4
                        if(isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
303 4
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
304 4
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
305
306 4
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
307 4
                                $amount = $amount * -1;
308
                            }
309
310 4
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
311
                        }
312 4
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
313
                            $chargesDetail->setChargesIncluded­Indicator(true);
314
                        }
315 4
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
316 4
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
317
                        }
318 4
                        $charges->addRecord($chargesDetail);
319
                    }
320
                }
321 4
                $detail->setCharges($charges);
322
            }
323 22
    }
324
325
    /**
326
     * @param DTO\EntryTransactionDetail $detail
327
     * @param SimpleXMLElement           $xmlDetail
328
     * @param SimpleXMLElement           $CdtDbtInd
329
     */
330 20
    public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
331
    {
332 20
        if (isset($xmlDetail->AmtDtls)) {
333 6
            $amountDetails = new DTO\AmountDetails();
334
335 6
            if (isset($xmlDetail->AmtDtls->TxAmt) && isset($xmlDetail->AmtDtls->TxAmt->Amt)) {
336 6
                $amount = StringToUnits::convert((string) $xmlDetail->AmtDtls->TxAmt->Amt);
337
338 6
                if ((string) $CdtDbtInd === 'DBIT') {
339 6
                    $amount = $amount * -1;
340
                }
341
342 6
                $money = new Money(
343 6
                    $amount,
344 6
                    new Currency((string) $xmlDetail->AmtDtls->TxAmt->Amt['Ccy'])
345
                );
346 6
                $amountDetails->setAmount($money);
347
            }
348 6
            $detail->setAmountDetails($amountDetails);
349
        }
350 20
    }
351
352
    /**
353
     * @param DTO\EntryTransactionDetail $detail
354
     * @param SimpleXMLElement           $xmlDetail
355
     * @param SimpleXMLElement           $CdtDbtInd
356
     */
357 20
    public function addAmount(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
358
    {
359 20
        if (isset($xmlDetail->Amt)) {
360 13
            $amountDetails = new DTO\Amount();
361
362 13
                $amount = StringToUnits::convert((string) $xmlDetail->Amt);
363
364 13
                if ((string) $CdtDbtInd === 'DBIT') {
365 6
                    $amount = $amount * -1;
366
                }
367
368 13
                $money = new Money(
369 13
                    $amount,
370 13
                    new Currency((string) $xmlDetail->Amt['Ccy'])
371
                );
372 13
                $amountDetails->setAmount($money);
373
374 13
            $detail->setAmount($amountDetails);
375
        }
376 20
    }
377
378
    /**
379
     * @param SimpleXMLElement $xmlDetail
0 ignored issues
show
Bug introduced by
There is no parameter named $xmlDetail. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
380
     *
381
     * @return DTO\Account|null
382
     */
383
    abstract public function getRelatedPartyAccount(SimpleXMLElement $xmlRelatedPartyTypeAccount = null);
384
}
385