Completed
Pull Request — master (#51)
by Jean-Louis
01:32
created

EntryTransactionDetail::addRelatedParties()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

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