Completed
Push — master ( 76c518...45d8dc )
by Frederik
01:24
created

EntryTransactionDetail::addCharges()   C

Complexity

Conditions 13
Paths 5

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 13.0122

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 40
ccs 23
cts 24
cp 0.9583
rs 5.1234
cc 13
eloc 23
nc 5
nop 2
crap 13.0122

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