Completed
Push — master ( f7c7ac...76c518 )
by Frederik
02:36
created

Record::addBalances()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0237

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 21
cts 23
cp 0.913
rs 8.439
cc 6
eloc 23
nc 5
nop 2
crap 6.0237
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 31
    public function __construct(Entry $entryDecoder, DateDecoderInterface $dateDecoder)
28
    {
29 31
        $this->entryDecoder = $entryDecoder;
30 31
        $this->dateDecoder = $dateDecoder;
31 31
    }
32
33
    /**
34
     * @param DTO\Record       $record
35
     * @param SimpleXMLElement $xmlRecord
36
     */
37 19
    public function addBalances(DTO\Record $record, SimpleXMLElement $xmlRecord)
38
    {
39 19
        $xmlBalances = $xmlRecord->Bal;
0 ignored issues
show
Bug introduced by
The property Bal 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...
40 19
        foreach ($xmlBalances as $xmlBalance) {
41 14
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
42 14
            $currency = (string)$xmlBalance->Amt['Ccy'];
43 14
            $date = (string)$xmlBalance->Dt->Dt;
44
45 14
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
46 9
                $amount = $amount * -1;
47
            }
48
49 14
            if (isset($xmlBalance->Tp)
50 14
                && isset($xmlBalance->Tp->CdOrPrtry)
51 14
                && (string) $xmlBalance->Tp->CdOrPrtry->Cd === 'OPBD'
52
            ) {
53 11
                $balance = DTO\Balance::opening(
54 11
                    new Money(
55
                        $amount,
56 11
                        new Currency($currency)
57
                    ),
58 11
                    $this->dateDecoder->decode($date)
59
                );
60
            } else {
61 13
                $balance = DTO\Balance::closing(
62 13
                    new Money(
63
                        $amount,
64 13
                        new Currency($currency)
65
                    ),
66 13
                    $this->dateDecoder->decode($date)
67
                );
68
            }
69
70 14
            $record->addBalance($balance);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Genkgo\Camt\DTO\Record as the method addBalance() does only exist in the following sub-classes of Genkgo\Camt\DTO\Record: Genkgo\Camt\Camt052\DTO\Report, Genkgo\Camt\Camt053\DTO\Statement. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
71
        }
72 19
    }
73
74
    /**
75
     * @param DTO\Record       $record
76
     * @param SimpleXMLElement $xmlRecord
77
     */
78 22
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord)
79
    {
80 22
        $index = 0;
81 22
        $xmlEntries = $xmlRecord->Ntry;
0 ignored issues
show
Bug introduced by
The property Ntry 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...
82 22
        foreach ($xmlEntries as $xmlEntry) {
83 21
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
84 21
            $currency    = (string)$xmlEntry->Amt['Ccy'];
85 21
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
86 21
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
87 21
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
88
89 21
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
90 12
                $amount = $amount * -1;
91
            }
92
93 21
            $entry = new DTO\Entry(
94
                $record,
95
                $index,
96 21
                new Money($amount, new Currency($currency)),
97 21
                $this->dateDecoder->decode($bookingDate),
98 21
                $this->dateDecoder->decode($valueDate),
99 21
                $additionalInfo
100
            );
101
102 21
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
103 1
                $entry->setReversalIndicator(true);
104
            }
105
106 21
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
107 1
                $entry->setReference((string) $xmlEntry->NtryRef);
108
            }
109
110 21
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
111 18
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
112
            }
113
114 21
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
115 11
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
116
            }
117
            
118 21
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
119 3
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
120
            }
121
122 21
            if (isset($xmlEntry->BkTxCd)) {
123 20
                $bankTransactionCode = new DTO\BankTransactionCode();
124
125 20
                if (isset($xmlEntry->BkTxCd->Prtry)) {
126 19
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
127 19
                        (string)$xmlEntry->BkTxCd->Prtry->Cd,
128 19
                        (string)$xmlEntry->BkTxCd->Prtry->Issr
129
                    );
130
131 19
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
132
                }
133
134 20
                $entry->setBankTransactionCode($bankTransactionCode);
135
            }
136
137 21
            if (isset($xmlEntry->Chrgs)) {
138
                $charges = new DTO\Charges();
139
140
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
141
                    $amount      = StringToUnits::convert((string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt);
142
                    $currency    = (string)$xmlEntry->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
143
144
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
145
                }
146
                
147
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
148
                if ($chargesRecords) {
149
                    foreach ($chargesRecords as $chargesRecord) {
150
                        
151
                        $chargesDetail = new DTO\ChargesRecord();
152
                        
153
                        if(isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
154
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
155
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
156
157
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
158
                                $amount = $amount * -1;
159
                            }
160
                            
161
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
162
                        }
163
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
164
                            $chargesDetail->setChargesIncluded­Indicator(true);
165
                        }
166
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
167
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
168
                        }
169
                        $charges->addRecord($chargesDetail);
170
                    }
171
                }
172
                $entry->setCharges($charges);
173
            }
174
            
175 21
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
176
177 21
            $record->addEntry($entry);
178 21
            $index++;
179
        }
180 22
    }
181
}
182