Completed
Push — master ( 0d5cb2...ca808c )
by Frederik
8s
created

Message::getAccount()   D

Complexity

Conditions 10
Paths 15

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 14.6656

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
ccs 16
cts 25
cp 0.64
rs 4.8196
cc 10
eloc 20
nc 15
nop 1
crap 14.6656

How to fix   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\Camt054\Decoder;
4
5
use Genkgo\Camt\Decoder\Message as BaseMessageDecoder;
6
use Genkgo\Camt\Camt054\DTO as Camt054DTO;
7
use Genkgo\Camt\DTO;
8
use \SimpleXMLElement;
9
use \DateTimeImmutable;
10
use Genkgo\Camt\Iban;
11
12
class Message extends BaseMessageDecoder
13
{
14
    /**
15
     * @param DTO\Message      $message
16
     * @param SimpleXMLElement $document
17
     */
18 4
    public function addRecords(DTO\Message $message, SimpleXMLElement $document)
19
    {
20 4
        $reports = [];
21
22 4
        $xmlReports = $this->getRootElement($document)->Ntfctn;
0 ignored issues
show
Bug introduced by
The property Ntfctn 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...
23 4
        foreach ($xmlReports as $xmlReport) {
24 4
            $report = new Camt054DTO\Notification(
25 4
                (string) $xmlReport->Id,
26 4
                new DateTimeImmutable((string)$xmlReport->CreDtTm),
27 4
                $this->getAccount($xmlReport)
0 ignored issues
show
Bug introduced by
It seems like $this->getAccount($xmlReport) targeting Genkgo\Camt\Camt054\Decoder\Message::getAccount() can also be of type null; however, Genkgo\Camt\DTO\Record::__construct() does only seem to accept object<Genkgo\Camt\DTO\Account>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
28 4
            );
29
30 4
            $this->recordDecoder->addEntries($report, $xmlReport);
31
32 4
            $reports[] = $report;
33 4
        }
34
35 4
        $message->setRecords($reports);
36 4
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function getRootElement(SimpleXMLElement $document)
42
    {
43 5
        return $document->BkToCstmrDbtCdtNtfctn; 
44
    }
45
46
    /**
47
     * @param SimpleXMLElement $xmlRecord
48
     *
49
     * @return DTO\Account
50
     */
51 4
    protected function getAccount(SimpleXMLElement $xmlRecord)
52
    {
53 4
        if (isset($xmlRecord->Acct->Id->IBAN)) {
54
            return new DTO\IbanAccount(new Iban((string) $xmlRecord->Acct->Id->IBAN));
55
        }
56
57 4
        if (isset($xmlRecord->Acct->Id->BBAN)) {
58
            return new DTO\BBANAccount((string) $xmlRecord->Acct->Id->BBAN);
59
        }
60
61 4
        if (isset($xmlRecord->Acct->Id->UPIC)) {
62
            return new DTO\UPICAccount((string) $xmlRecord->Acct->Id->UPIC);
63
        }
64
65 4
        if (isset($xmlRecord->Acct->Id->PrtryAcct)) {
66 1
            return new DTO\ProprietaryAccount((string) $xmlRecord->Acct->Id->PrtryAcct->Id);
67
        }
68
69 3
        if (isset($xmlRecord->Acct->Id->Othr)) {
70 3
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
71 3
            $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
72
73 3
            if (isset($xmlOtherIdentification->SchmeNm)) {
74 3
                if (isset($xmlOtherIdentification->SchmeNm->Cd)) {
75 3
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Cd);
76 3
                }
77
78
                if (isset($otherIdentification->SchmeNm->Prtry)) {
0 ignored issues
show
Bug introduced by
The variable $otherIdentification does not exist. Did you mean $xmlOtherIdentification?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
79 3
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Prtry);
80
                }
81
            }
82
83 3
            if (isset($xmlOtherIdentification->Issr)) {
84
                $otherAccount->setIssuer($xmlOtherIdentification->Issr);
85
            }
86
87 3
            return $otherAccount;
88
        }
89
    }
90
}
91