Failed Conditions
Push — master ( 65cf51...40aca8 )
by Adrien
03:16
created

Record::addEntries()   F

Complexity

Conditions 34
Paths 8961

Size

Total Lines 124
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 62.6924

Importance

Changes 0
Metric Value
cc 34
eloc 71
nc 8961
nop 2
dl 0
loc 124
ccs 51
cts 72
cp 0.7083
crap 62.6924
rs 0
c 0
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\Util\StringToUnits;
7
use Money\Money;
8
use Money\Currency;
9
use \SimpleXMLElement;
10
11
class Record
12
{
13
    /**
14
     * @var Entry
15
     */
16
    private $entryDecoder;
17
    /**
18
     * @var DateDecoderInterface
19
     */
20
    private $dateDecoder;
21
22
    /**
23
     * Record constructor.
24
     * @param Entry $entryDecoder
25
     * @param DateDecoderInterface $dateDecoder
26
     */
27 33
    public function __construct(Entry $entryDecoder, DateDecoderInterface $dateDecoder)
28
    {
29 33
        $this->entryDecoder = $entryDecoder;
30 33
        $this->dateDecoder = $dateDecoder;
31 33
    }
32
33
    /**
34
     * @param DTO\Record       $record
35
     * @param SimpleXMLElement $xmlRecord
36
     */
37 20
    public function addBalances(DTO\Record $record, SimpleXMLElement $xmlRecord)
38
    {
39 20
        $xmlBalances = $xmlRecord->Bal;
40 20
        foreach ($xmlBalances as $xmlBalance) {
41 15
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
42 15
            $currency = (string)$xmlBalance->Amt['Ccy'];
43 15
            $date = (string)$xmlBalance->Dt->Dt;
44
45 15
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
46 10
                $amount = $amount * -1;
47
            }
48
49 15
            if (isset($xmlBalance->Tp) && isset($xmlBalance->Tp->CdOrPrtry)) {
50 15
                $code = (string) $xmlBalance->Tp->CdOrPrtry->Cd;
51
52 15
                if (in_array($code, ['OPBD', 'PRCD'])) {
53 12
                    $record->addBalance(DTO\Balance::opening(
0 ignored issues
show
Bug introduced by
The method addBalance() does not exist on Genkgo\Camt\DTO\Record. It seems like you code against a sub-type of said class. However, the method does not exist in Genkgo\Camt\Camt054\DTO\Notification. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
                    $record->/** @scrutinizer ignore-call */ 
54
                             addBalance(DTO\Balance::opening(
Loading history...
54 12
                        new Money(
55 12
                            $amount,
56 12
                            new Currency($currency)
57
                        ),
58 12
                        $this->dateDecoder->decode($date)
59
                    ));
60 14
                } elseif ($code === 'CLBD') {
61 11
                    $record->addBalance(DTO\Balance::closing(
62 11
                        new Money(
63 11
                            $amount,
64 11
                            new Currency($currency)
65
                        ),
66 15
                        $this->dateDecoder->decode($date)
67
                    ));
68
                }
69
            }
70
        }
71 20
    }
72
73
    /**
74
     * @param DTO\Record       $record
75
     * @param SimpleXMLElement $xmlRecord
76
     */
77 24
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord)
78
    {
79 24
        $index = 0;
80 24
        $xmlEntries = $xmlRecord->Ntry;
81 24
        foreach ($xmlEntries as $xmlEntry) {
82 23
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
83 23
            $currency    = (string)$xmlEntry->Amt['Ccy'];
84 23
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
85 23
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
86 23
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
87
88 23
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
89 13
                $amount = $amount * -1;
90
            }
91
92 23
            $entry = new DTO\Entry(
93 23
                $record,
94 23
                $index,
95 23
                new Money($amount, new Currency($currency))
96
            );
97
98 23
            if ($bookingDate) {
99 22
                $entry->setBookingDate($this->dateDecoder->decode($bookingDate));
100
            }
101
102 23
            if ($valueDate) {
103 22
                $entry->setValueDate($this->dateDecoder->decode($valueDate));
104
            }
105
106 23
            $entry->setAdditionalInfo($additionalInfo);
107
108 23
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
109 1
                $entry->setReversalIndicator(true);
110
            }
111
112 23
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
113 1
                $entry->setReference((string) $xmlEntry->NtryRef);
114
            }
115
116 23
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
117 19
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
118
            }
119
120 23
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
121 12
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
122
            }
123
124 23
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
125 4
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
126
            }
127
128 23
            if (isset($xmlEntry->BkTxCd)) {
129 22
                $bankTransactionCode = new DTO\BankTransactionCode();
130
131 22
                if (isset($xmlEntry->BkTxCd->Prtry)) {
132 21
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
133 21
                        (string)$xmlEntry->BkTxCd->Prtry->Cd,
134 21
                        (string)$xmlEntry->BkTxCd->Prtry->Issr
135
                    );
136
137 21
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
138
                }
139
140 22
                if (isset($xmlEntry->BkTxCd->Domn)) {
141 18
                    $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
142 18
                        (string)$xmlEntry->BkTxCd->Domn->Cd
143
                    );
144
145 18
                    if (isset($xmlEntry->BkTxCd->Domn->Fmly)) {
146 18
                        $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
147 18
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->Cd,
148 18
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->SubFmlyCd
149
                        );
150
151 18
                        $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
152
                    }
153
154 18
                    $bankTransactionCode->setDomain($domainBankTransactionCode);
155
                }
156
157 22
                $entry->setBankTransactionCode($bankTransactionCode);
158
            }
159
160 23
            if (isset($xmlEntry->Chrgs)) {
161
                $charges = new DTO\Charges();
162
163
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
164
                    $amount      = StringToUnits::convert((string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt);
165
                    $currency    = (string)$xmlEntry->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
166
167
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
168
                }
169
170
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
171
                if ($chargesRecords) {
172
                    foreach ($chargesRecords as $chargesRecord) {
173
                        $chargesDetail = new DTO\ChargesRecord();
174
175
                        if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
176
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
177
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
178
179
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
180
                                $amount = $amount * -1;
181
                            }
182
183
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
184
                        }
185
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
186
                            $chargesDetail->setChargesIncluded­Indicator(true);
187
                        }
188
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
189
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
190
                        }
191
                        $charges->addRecord($chargesDetail);
192
                    }
193
                }
194
                $entry->setCharges($charges);
195
            }
196
197 23
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
198
199 23
            $record->addEntry($entry);
200 23
            $index++;
201
        }
202 24
    }
203
}
204