Message::getAccount()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 16.4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 20
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 40
ccs 12
cts 20
cp 0.6
crap 16.4
rs 7.6666

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
declare(strict_types=1);
4
5
namespace Genkgo\Camt\Camt054\Decoder;
6
7
use Genkgo\Camt\Camt054\DTO as Camt054DTO;
8
use Genkgo\Camt\Decoder\Message as BaseMessageDecoder;
9
use Genkgo\Camt\DTO;
10
use Genkgo\Camt\DTO\Account;
11
use Genkgo\Camt\Exception\InvalidMessageException;
12
use Genkgo\Camt\Iban;
13
use SimpleXMLElement;
14
15
class Message extends BaseMessageDecoder
16 5
{
17
    public function addRecords(DTO\Message $message, SimpleXMLElement $document): void
18 5
    {
19
        $notifications = [];
20 5
21 5
        $xmlNotifications = $this->getRootElement($document)->Ntfctn;
22 5
        foreach ($xmlNotifications as $xmlNotification) {
23 5
            $notification = new Camt054DTO\Notification(
24 5
                (string) $xmlNotification->Id,
25 5
                $this->dateDecoder->decode((string) $xmlNotification->CreDtTm),
26
                $this->getAccount($xmlNotification)
0 ignored issues
show
Bug introduced by
It seems like $xmlNotification can also be of type null; however, parameter $xmlRecord of Genkgo\Camt\Camt054\Decoder\Message::getAccount() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

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

26
                $this->getAccount(/** @scrutinizer ignore-type */ $xmlNotification)
Loading history...
27
            );
28 5
29 4
            if (isset($xmlNotification->NtfctnPgntn)) {
30 4
                $notification->setPagination(new DTO\Pagination(
31 4
                    (string) $xmlNotification->NtfctnPgntn->PgNb,
32
                    ('true' === (string) $xmlNotification->NtfctnPgntn->LastPgInd) ? true : false
33
                ));
34
            }
35 5
36 4
            if (isset($xmlNotification->AddtlNtfctnInf)) {
37
                $notification->setAdditionalInformation((string) $xmlNotification->AddtlNtfctnInf);
38
            }
39 5
40 5
            $this->addCommonRecordInformation($notification, $xmlNotification);
41
            $this->recordDecoder->addEntries($notification, $xmlNotification);
42 5
43
            $notifications[] = $notification;
44
        }
45 5
46 5
        $message->setRecords($notifications);
47
    }
48
49
    /**
50
     * @inheritDoc
51 6
     */
52
    public function getRootElement(SimpleXMLElement $document): SimpleXMLElement
53 6
    {
54
        return $document->BkToCstmrDbtCdtNtfctn;
55
    }
56 5
57
    protected function getAccount(SimpleXMLElement $xmlRecord): Account
58 5
    {
59 4
        if (isset($xmlRecord->Acct->Id->IBAN)) {
60
            return new DTO\IbanAccount(new Iban((string) $xmlRecord->Acct->Id->IBAN));
61
        }
62 5
63
        if (isset($xmlRecord->Acct->Id->BBAN)) {
64
            return new DTO\BBANAccount((string) $xmlRecord->Acct->Id->BBAN);
65
        }
66 5
67
        if (isset($xmlRecord->Acct->Id->UPIC)) {
68
            return new DTO\UPICAccount((string) $xmlRecord->Acct->Id->UPIC);
69
        }
70 5
71 1
        if (isset($xmlRecord->Acct->Id->PrtryAcct)) {
72
            return new DTO\ProprietaryAccount((string) $xmlRecord->Acct->Id->PrtryAcct->Id);
73
        }
74 4
75 4
        if (isset($xmlRecord->Acct->Id->Othr)) {
76 4
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
77
            $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
78 4
79
            if (isset($xmlOtherIdentification->SchmeNm)) {
80
                if (isset($xmlOtherIdentification->SchmeNm->Cd)) {
81
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Cd);
82
                }
83
84
                if (isset($xmlOtherIdentification->SchmeNm->Prtry)) {
85
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Prtry);
86
                }
87
            }
88 4
89
            if (isset($xmlOtherIdentification->Issr)) {
90
                $otherAccount->setIssuer((string) $xmlOtherIdentification->Issr);
91
            }
92 4
93
            return $otherAccount;
94
        }
95
96
        throw new InvalidMessageException('Cannot decode account');
97
    }
98
}
99