Completed
Push — master ( 38af03...989956 )
by Frederik
02:11
created

EntryTransactionDetail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 Date
16
     */
17
    private $dateDecoder;
18
19
    /**
20 22
     * EntryTransactionDetail constructor.
21 11
     * @param Date $dateDecoder
22 22
     */
23 8
    public function __construct(Date $dateDecoder)
24
    {
25
        $this->dateDecoder = $dateDecoder;
26 17
    }
27 17
28
    /**
29 17
     * @param DTO\EntryTransactionDetail $detail
30 17
     * @param SimpleXMLElement           $xmlDetail
31 17
     */
32 17
    public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
33 17
    {
34 17
        if (false === isset($xmlDetail->Refs)) {
35 17
            return;
36 17
        }
37 17
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 18
        $reference = new DTO\Reference();
40 17
41 17
        $reference->setMessageId(isset($refs->MsgId) ? (string) $refs->MsgId : null);
42
        $reference->setAccountServiceReference(isset($refs->AcctSvcrRef) ? (string) $refs->AcctSvcrRef : null);
43 17
        $reference->setPaymentInformationId(isset($refs->PmtInfId) ? (string) $refs->PmtInfId : null);
44 8
        $reference->setInstructionId(isset($refs->InstrId) ? (string) $refs->InstrId : null);
45 16
        $reference->setEndToEndId(isset($refs->EndToEndId) ? (string) $refs->EndToEndId : null);
46
        $reference->setTransactionId(isset($refs->TxId) ? (string) $refs->TxId : null);
47 8
        $reference->setMandateId(isset($refs->MndtId) ? (string) $refs->MndtId : null);
48 17
        $reference->setChequeNumber(isset($refs->ChqNb) ? (string) $refs->ChqNb : null);
49
        $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
        $reference->setMarketInfrastructureTransactionId(isset($refs->MktInfrstrctrTxId) ? (string) $refs->MktInfrstrctrTxId : null);
53
        $reference->setProcessingId(isset($refs->PrcgId) ? (string) $refs->PrcgId : null);
54
55
        foreach ($refs->Prtry as $xmlProprietary) {
56
            $type = isset($xmlProprietary->Tp) ? (string) $xmlProprietary->Tp : null;
57 22
            $subReference = isset($xmlProprietary->Ref) ? (string) $xmlProprietary->Ref : null;
58
59 22
            $reference->addProprietary(new DTO\ProprietaryReference($type, $subReference));
60 1
        }
61
62
        $detail->addReference($reference);
63 21
    }
64 21
65 21
    /**
66 21
     * @param DTO\EntryTransactionDetail $detail
67 21
     * @param SimpleXMLElement           $xmlDetail
68
     */
69 21
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
70 21
    {
71
        if (false === isset($xmlDetail->RltdPties)) {
72 21
            return;
73 18
        }
74 18
75 18
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
76
            if (isset($xmlRelatedParty->Cdtr)) {
77 18
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
78 18
                $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 21
81
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
82
            }
83
84
            if (isset($xmlRelatedParty->Dbtr)) {
85
                $xmlRelatedPartyType = $xmlRelatedParty->Dbtr;
86
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct;
87
                $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 21
        }
92 21
    }
93 21
94
    /**
95 21
     * @param DTO\EntryTransactionDetail $detail
96
     * @param $xmlRelatedPartyType
97 21
     * @param $relatedPartyType
98
     * @param $xmlRelatedPartyTypeAccount
99 21
     * @return DTO\RelatedParty
100
     */
101
    protected function addRelatedParty(DTO\EntryTransactionDetail $detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount)
102
    {
103
        if (isset($xmlRelatedPartyType->PstlAdr)) {
104
            $relatedPartyType->setAddress(DTOFactory\Address::createFromXml($xmlRelatedPartyType->PstlAdr));
105
        }
106 20
107
        $relatedParty = new DTO\RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount));
108 20
109 12
        $detail->addRelatedParty($relatedParty);
110
111
        return $relatedParty;
112 16
    }
113 16
114 16
    /**
115 16
     * @param DTO\EntryTransactionDetail $detail
116 16
     * @param SimpleXMLElement           $xmlDetail
117 16
     */
118
    public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
119 16
    {
120 16
        if (false === isset($xmlDetail->RltdAgts)) {
121 16
            return;
122 16
        }
123 16
124 16
        foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) {
125 16
            if (isset($xmlRelatedAgent->CdtrAgt)) {
126
                $agent = new DTO\CreditorAgent((string)$xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->CdtrAgt->FinInstnId->BIC);
127
                $relatedAgent =  new DTO\RelatedAgent($agent);
128
                $detail->addRelatedAgent($relatedAgent);
129
            }
130
131 23
            if (isset($xmlRelatedAgent->DbtrAgt)) {
132
                $agent = new DTO\DebtorAgent((string)$xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->DbtrAgt->FinInstnId->BIC);
133 23
                $relatedAgent =  new DTO\RelatedAgent($agent);
134 11
                $detail->addRelatedAgent($relatedAgent);
135
            }
136
        }
137 18
    }
138 9
139 9
    /**
140 9
     * @param DTO\EntryTransactionDetail $detail
141 9
     * @param SimpleXMLElement           $xmlDetail
142
     */
143 9
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
144
    {
145
        if (false === isset($xmlDetail->RmtInf)) {
146 9
            return;
147 9
        }
148 9
149 9
        if (isset($xmlDetail->RmtInf->Ustrd)) {
150 9
            $remittanceInformation = DTO\RemittanceInformation::fromUnstructured(
151 9
                (string)$xmlDetail->RmtInf->Ustrd
152 9
            );
153 9
            $detail->setRemittanceInformation($remittanceInformation);
154 9
155 9
            return;
156 9
        }
157 9
158
        if (isset($xmlDetail->RmtInf->Strd)
159
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf)
160
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref)
161
        ) {
162
            $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured(
163 22
                (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref
164
            );
165 22
            $remittanceInformation = new DTO\RemittanceInformation();
166 21
            $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
167
            $detail->setRemittanceInformation($remittanceInformation);
168
        }
169 1
    }
170 1
    
171 1
    /**
172 1
     * @param DTO\EntryTransactionDetail $detail
173 1
     * @param SimpleXMLElement           $xmlDetail
174 1
     */
175
    public function addRelatedDates(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
176
    {
177
        if (false === isset($xmlDetail->RltdDts)) {
178
            return;
179
        }
180
181
        if (isset($xmlDetail->RltdDts->AccptncDtTm)) {
182
            $RelatedDates = DTO\RelatedDates::fromUnstructured(
183
                $this->dateDecoder->decode((string) $xmlDetail->RltdDts->AccptncDtTm )
184 22
            );
185
            $detail->setRelatedDates($RelatedDates);
186 22
            return;
187 1
        }
188 1
    }
189 1
    
190 1
    
191 1
192 1
    /**
193 22
     * @param DTO\EntryTransactionDetail $detail
194
     * @param SimpleXMLElement           $xmlDetail
195
     */
196
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
197
    {
198
        if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) {
199 22
            $remittanceInformation = DTO\ReturnInformation::fromUnstructured(
200
                (string)$xmlDetail->RtrInf->Rsn->Cd,
201 22
                (string)$xmlDetail->RtrInf->AddtlInf
202 1
            );
203 1
            $detail->setReturnInformation($remittanceInformation);
204 1
        }
205 1
    }
206 1
207 22
    /**
208
     * @param DTO\EntryTransactionDetail $detail
209
     * @param SimpleXMLElement           $xmlDetail
210
     */
211
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
212
    {
213 20
        if (isset($xmlDetail->AddtlTxInf)) {
214
            $additionalInformation = new DTO\AdditionalTransactionInformation(
215 20
                (string) $xmlDetail->AddtlTxInf
216
            );
217 20
            $detail->setAdditionalTransactionInformation($additionalInformation);
218 18
        }
219
    }
220 18
221 18
    /**
222 18
     * @param DTO\EntryTransactionDetail $detail
223 18
     * @param SimpleXMLElement           $xmlDetail
224 18
     */
225
    public function addBankTransactionCode(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
226 18
    {
227 18
        $bankTransactionCode = new DTO\BankTransactionCode();
228 18
229
        if (isset($xmlDetail->BkTxCd)) {
230 20
            $bankTransactionCode = new DTO\BankTransactionCode();
231 20
232
            if (isset($xmlDetail->BkTxCd->Prtry)) {
233
                $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
234
                    (string)$xmlDetail->BkTxCd->Prtry->Cd,
235
                    (string)$xmlDetail->BkTxCd->Prtry->Issr
236
                );
237 22
238
                $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
239 22
            }
240 4
        }
241
242 4
        $detail->setBankTransactionCode($bankTransactionCode);
243 4
    }
244 4
    
245
    /**
246 4
     * @param DTO\EntryTransactionDetail $detail
247 4
     * @param SimpleXMLElement           $xmlDetail
248
     */
249 4
    public function addCharges(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
250 4
    {
251 4
            if (isset($xmlDetail->Chrgs)) {
252
                $charges = new DTO\Charges();
253 4
254
                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 4
258
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
259 4
                }
260 4
                
261 4
                $chargesRecords = $xmlDetail->Chrgs->Rcrd;
262
                if ($chargesRecords) {
263 4
                    foreach ($chargesRecords as $chargesRecord) {
264 4
                        
265 4
                        $chargesDetail = new DTO\ChargesRecord();
266
                        
267
                        if(isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
268 4
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
269 4
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
270 4
271 4
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
272 4
                                $amount = $amount * -1;
273 4
                            }
274 4
                            
275 4
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
276 22
                        }
277
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
278
                            $chargesDetail->setChargesIncluded­Indicator(true);
279
                        }
280
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
281
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
282
                        }
283 20
                        $charges->addRecord($chargesDetail);
284
                    }
285 20
                }
286 6
                $detail->setCharges($charges);
287
            }        
288 6
    }    
289 6
290
    /**
291 6
     * @param DTO\EntryTransactionDetail $detail
292 6
     * @param SimpleXMLElement           $xmlDetail
293 6
     * @param SimpleXMLElement           $CdtDbtInd
294
     */
295 6
    public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
296 6
    {
297 6
        if (isset($xmlDetail->AmtDtls)) {
298 6
            $amountDetails = new DTO\AmountDetails();
299 6
300 6
            if (isset($xmlDetail->AmtDtls->TxAmt) && isset($xmlDetail->AmtDtls->TxAmt->Amt)) {
301 6
                $amount = StringToUnits::convert((string) $xmlDetail->AmtDtls->TxAmt->Amt);
302 6
                
303 20
                if ((string) $CdtDbtInd === 'DBIT') {
304
                    $amount = $amount * -1;
305
                }
306
307
                $money = new Money(
308
                    $amount,
309
                    new Currency((string) $xmlDetail->AmtDtls->TxAmt->Amt['Ccy'])
310 20
                );
311
                $amountDetails->setAmount($money);
312 20
            }
313 13
            $detail->setAmountDetails($amountDetails);
314
        }
315 13
    }
316
    
317 13
    /**
318 6
     * @param DTO\EntryTransactionDetail $detail
319 6
     * @param SimpleXMLElement           $xmlDetail
320
     * @param SimpleXMLElement           $CdtDbtInd
321 13
     */
322 13
    public function addAmount(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
323 13
    {
324 13
        if (isset($xmlDetail->Amt)) {
325 13
            $amountDetails = new DTO\Amount();
326
327 13
                $amount = StringToUnits::convert((string) $xmlDetail->Amt);
328 13
329 20
                if ((string) $CdtDbtInd === 'DBIT') {
330
                    $amount = $amount * -1;
331
                }
332
333
                $money = new Money(
334
                    $amount,
335
                    new Currency((string) $xmlDetail->Amt['Ccy'])
336
                );
337
                $amountDetails->setAmount($money);
338
339
            $detail->setAmount($amountDetails);
340
        }
341
    }
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