Completed
Pull Request — master (#52)
by Jean-Louis
01:55
created

EntryTransactionDetail::addRelatedParties()   C

Complexity

Conditions 11
Paths 83

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
ccs 20
cts 20
cp 1
rs 5.2653
cc 11
eloc 26
nc 83
nop 2
crap 11

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