Passed
Push — master ( 45011d...825113 )
by Adrien
02:28
created

Record::addEntries()   F

Complexity

Conditions 34
Paths 8961

Size

Total Lines 126
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 63.9193

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 34
eloc 71
nc 8961
nop 2
dl 0
loc 126
ccs 50
cts 71
cp 0.7042
crap 63.9193
rs 0
c 3
b 0
f 0

How to fix   Long Method    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 Genkgo\Camt\DTO\RecordWithBalances;
7
use Genkgo\Camt\Util\StringToUnits;
8
use Money\Money;
9
use Money\Currency;
10
use \SimpleXMLElement;
11
12
class Record
13
{
14
    /**
15
     * @var Entry
16
     */
17
    private $entryDecoder;
18
    /**
19
     * @var DateDecoderInterface
20
     */
21
    private $dateDecoder;
22
23
    /**
24
     * Record constructor.
25
     * @param Entry $entryDecoder
26
     * @param DateDecoderInterface $dateDecoder
27
     */
28 33
    public function __construct(Entry $entryDecoder, DateDecoderInterface $dateDecoder)
29
    {
30 33
        $this->entryDecoder = $entryDecoder;
31 33
        $this->dateDecoder = $dateDecoder;
32 33
    }
33
34
    /**
35
     * @param RecordWithBalances $record
36
     * @param SimpleXMLElement $xmlRecord
37
     */
38 20
    public function addBalances(RecordWithBalances $record, SimpleXMLElement $xmlRecord)
39
    {
40 20
        $xmlBalances = $xmlRecord->Bal;
41 20
        foreach ($xmlBalances as $xmlBalance) {
42 15
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
43 15
            $currency = (string)$xmlBalance->Amt['Ccy'];
44 15
            $date = (string)$xmlBalance->Dt->Dt;
45
46 15
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
47 10
                $amount = $amount * -1;
48
            }
49
50 15
            if (isset($xmlBalance->Tp) && isset($xmlBalance->Tp->CdOrPrtry)) {
51 15
                $code = (string) $xmlBalance->Tp->CdOrPrtry->Cd;
52
53 15
                if (in_array($code, ['OPBD', 'PRCD'])) {
54 12
                    $record->addBalance(DTO\Balance::opening(
55 12
                        new Money(
56 12
                            $amount,
57 12
                            new Currency($currency)
58
                        ),
59 12
                        $this->dateDecoder->decode($date)
60
                    ));
61 14
                } elseif ($code === 'CLBD') {
62 11
                    $record->addBalance(DTO\Balance::closing(
63 11
                        new Money(
64 11
                            $amount,
65 11
                            new Currency($currency)
66
                        ),
67 11
                        $this->dateDecoder->decode($date)
68
                    ));
69
                }
70
            }
71
        }
72 20
    }
73
74
    /**
75
     * @param DTO\Record       $record
76
     * @param SimpleXMLElement $xmlRecord
77
     */
78 24
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord)
79
    {
80 24
        $index = 0;
81 24
        $xmlEntries = $xmlRecord->Ntry;
82 24
        foreach ($xmlEntries as $xmlEntry) {
83 23
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
84 23
            $currency    = (string)$xmlEntry->Amt['Ccy'];
85 23
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
86 23
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
87 23
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
88
89 23
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
90 13
                $amount = $amount * -1;
91
            }
92
93 23
            $entry = new DTO\Entry(
94 23
                $record,
95
                $index,
96 23
                new Money($amount, new Currency($currency))
97
            );
98
99 23
            if ($bookingDate) {
100 22
                $entry->setBookingDate($this->dateDecoder->decode($bookingDate));
101
            }
102
103 23
            if ($valueDate) {
104 22
                $entry->setValueDate($this->dateDecoder->decode($valueDate));
105
            }
106
107 23
            $entry->setAdditionalInfo($additionalInfo);
108
109 23
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
110 1
                $entry->setReversalIndicator(true);
111
            }
112
113 23
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
114 1
                $entry->setReference((string) $xmlEntry->NtryRef);
115
            }
116
117 23
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
118 19
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
119
            }
120
121 23
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
122 12
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
123
            }
124
125 23
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
126 4
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
127
            }
128
129 23
            if (isset($xmlEntry->BkTxCd)) {
130 22
                $bankTransactionCode = new DTO\BankTransactionCode();
131
132 22
                if (isset($xmlEntry->BkTxCd->Prtry)) {
133 21
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
134 21
                        (string)$xmlEntry->BkTxCd->Prtry->Cd,
135 21
                        (string)$xmlEntry->BkTxCd->Prtry->Issr
136
                    );
137
138 21
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
139
                }
140
141 22
                if (isset($xmlEntry->BkTxCd->Domn)) {
142 18
                    $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
143 18
                        (string)$xmlEntry->BkTxCd->Domn->Cd
144
                    );
145
146 18
                    if (isset($xmlEntry->BkTxCd->Domn->Fmly)) {
147 18
                        $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
148 18
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->Cd,
149 18
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->SubFmlyCd
150
                        );
151
152 18
                        $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
153
                    }
154
155 18
                    $bankTransactionCode->setDomain($domainBankTransactionCode);
156
                }
157
158 22
                $entry->setBankTransactionCode($bankTransactionCode);
159
            }
160
161 23
            if (isset($xmlEntry->Chrgs)) {
162
                $charges = new DTO\Charges();
163
164
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
165
                    $amount      = StringToUnits::convert((string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt);
166
                    $currency    = (string)$xmlEntry->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
167
168
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
169
                }
170
171
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
172
                if ($chargesRecords) {
173
174
                    /** @var SimpleXMLElement $chargesRecord */
175
                    foreach ($chargesRecords as $chargesRecord) {
176
                        $chargesDetail = new DTO\ChargesRecord();
177
178
                        if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
179
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
180
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
181
182
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
183
                                $amount = $amount * -1;
184
                            }
185
186
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
187
                        }
188
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
189
                            $chargesDetail->setChargesIncludedIndicator(true);
190
                        }
191
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
192
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
193
                        }
194
                        $charges->addRecord($chargesDetail);
195
                    }
196
                }
197
                $entry->setCharges($charges);
198
            }
199
200 23
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
201
202 23
            $record->addEntry($entry);
203 23
            $index++;
204
        }
205 24
    }
206
}
207