Completed
Push — master ( 45d8dc...fe9038 )
by Frederik
02:32
created

EntryTransactionDetail::addRelatedParty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 4
crap 2
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
260 20
        $detail->setBankTransactionCode($bankTransactionCode);
261 20
    }
262
    
263
    /**
264
     * @param DTO\EntryTransactionDetail $detail
265
     * @param SimpleXMLElement           $xmlDetail
266
     */
267 22
    public function addCharges(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
268
    {
269 22
            if (isset($xmlDetail->Chrgs)) {
270 4
                $charges = new DTO\Charges();
271
272 4
                if (isset($xmlDetail->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt) {
273 4
                    $amount      = StringToUnits::convert((string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt);
274 4
                    $currency    = (string)$xmlDetail->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
275
276 4
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
277
                }
278
                
279 4
                $chargesRecords = $xmlDetail->Chrgs->Rcrd;
280 4
                if ($chargesRecords) {
281 4
                    foreach ($chargesRecords as $chargesRecord) {
282
                        
283 4
                        $chargesDetail = new DTO\ChargesRecord();
284
                        
285 4
                        if(isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
286 4
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
287 4
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
288
289 4
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
290 4
                                $amount = $amount * -1;
291
                            }
292
                            
293 4
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
294
                        }
295 4
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
296
                            $chargesDetail->setChargesIncluded­Indicator(true);
297
                        }
298 4
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
299 4
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
300
                        }
301 4
                        $charges->addRecord($chargesDetail);
302
                    }
303
                }
304 4
                $detail->setCharges($charges);
305
            }        
306 22
    }    
307
308
    /**
309
     * @param DTO\EntryTransactionDetail $detail
310
     * @param SimpleXMLElement           $xmlDetail
311
     * @param SimpleXMLElement           $CdtDbtInd
312
     */
313 20
    public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
314
    {
315 20
        if (isset($xmlDetail->AmtDtls)) {
316 6
            $amountDetails = new DTO\AmountDetails();
317
318 6
            if (isset($xmlDetail->AmtDtls->TxAmt) && isset($xmlDetail->AmtDtls->TxAmt->Amt)) {
319 6
                $amount = StringToUnits::convert((string) $xmlDetail->AmtDtls->TxAmt->Amt);
320
                
321 6
                if ((string) $CdtDbtInd === 'DBIT') {
322 6
                    $amount = $amount * -1;
323
                }
324
325 6
                $money = new Money(
326 6
                    $amount,
327 6
                    new Currency((string) $xmlDetail->AmtDtls->TxAmt->Amt['Ccy'])
328
                );
329 6
                $amountDetails->setAmount($money);
330
            }
331 6
            $detail->setAmountDetails($amountDetails);
332
        }
333 20
    }
334
    
335
    /**
336
     * @param DTO\EntryTransactionDetail $detail
337
     * @param SimpleXMLElement           $xmlDetail
338
     * @param SimpleXMLElement           $CdtDbtInd
339
     */
340 20
    public function addAmount(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
341
    {
342 20
        if (isset($xmlDetail->Amt)) {
343 13
            $amountDetails = new DTO\Amount();
344
345 13
                $amount = StringToUnits::convert((string) $xmlDetail->Amt);
346
347 13
                if ((string) $CdtDbtInd === 'DBIT') {
348 6
                    $amount = $amount * -1;
349
                }
350
351 13
                $money = new Money(
352 13
                    $amount,
353 13
                    new Currency((string) $xmlDetail->Amt['Ccy'])
354
                );
355 13
                $amountDetails->setAmount($money);
356
357 13
            $detail->setAmount($amountDetails);
358
        }
359 20
    }
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