Failed Conditions
Pull Request — master (#92)
by
unknown
08:11
created

Message::getAccount()   C

Complexity

Conditions 14
Paths 135

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 53.4814

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 28
nc 135
nop 1
dl 0
loc 45
ccs 12
cts 29
cp 0.4138
crap 53.4814
rs 5.975
c 1
b 0
f 0

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\Iban;
12
use SimpleXMLElement;
13
14
class Message extends BaseMessageDecoder
15
{
16 1
    public function addRecords(DTO\Message $message, SimpleXMLElement $document): void
17
    {
18 1
        $notifications = [];
19
20 1
        $xmlNotifications = $this->getRootElement($document)->Ntfctn;
21 1
        foreach ($xmlNotifications as $xmlNotification) {
22 1
            $notification = new Camt054DTO\Notification(
23 1
                (string) $xmlNotification->Id,
24 1
                $this->dateDecoder->decode((string) $xmlNotification->CreDtTm),
25 1
                $this->getAccount($xmlNotification)
26
            );
27
28 1
            if (isset($xmlNotification->NtfctnPgntn)) {
29
                $notification->setPagination(new DTO\Pagination(
30
                    (string) $xmlNotification->NtfctnPgntn->PgNb,
31
                    ('true' === (string) $xmlNotification->NtfctnPgntn->LastPgInd) ? true : false
32
                ));
33
            }
34
35 1
            if (isset($xmlNotification->AddtlNtfctnInf)) {
36
                $notification->setAdditionalInformation((string) $xmlNotification->AddtlNtfctnInf);
37
            }
38
39 1
            $this->addCommonRecordInformation($notification, $xmlNotification);
40 1
            $this->recordDecoder->addEntries($notification, $xmlNotification);
41
42 1
            $notifications[] = $notification;
43
        }
44
45 1
        $message->setRecords($notifications);
46 1
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function getRootElement(SimpleXMLElement $document): SimpleXMLElement
52
    {
53 2
        return $document->BkToCstmrDbtCdtNtfctn;
54
    }
55
56 1
    protected function getAccount(SimpleXMLElement $xmlRecord): ?Account
57
    {
58 1
        $account = null;
59 1
        if (isset($xmlRecord->Acct->Id->IBAN)) {
60
            $account = new DTO\IbanAccount(new Iban((string)$xmlRecord->Acct->Id->IBAN));
61 1
        } elseif (isset($xmlRecord->Acct->Id->BBAN)) {
62
            $account = new DTO\BBANAccount((string)$xmlRecord->Acct->Id->BBAN);
63 1
        } elseif (isset($xmlRecord->Acct->Id->UPIC)) {
64
            $account = new DTO\UPICAccount((string)$xmlRecord->Acct->Id->UPIC);
65 1
        } elseif (isset($xmlRecord->Acct->Id->PrtryAcct)) {
66 1
            $account = new DTO\ProprietaryAccount((string)$xmlRecord->Acct->Id->PrtryAcct->Id);
67
        } elseif (isset($xmlRecord->Acct->Id->Othr)) {
68
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
69
            $account = new DTO\OtherAccount((string)$xmlOtherIdentification->Id);
70
71
            if (isset($xmlOtherIdentification->SchmeNm)) {
72
                if (isset($xmlOtherIdentification->SchmeNm->Cd)) {
73
                    $account->setSchemeName((string)$xmlOtherIdentification->SchmeNm->Cd);
74
                }
75
76
                if (isset($xmlOtherIdentification->SchmeNm->Prtry)) {
77
                    $account->setSchemeName((string)$xmlOtherIdentification->SchmeNm->Prtry);
78
                }
79
            }
80
81
            if (isset($xmlOtherIdentification->Issr)) {
82
                $account->setIssuer((string)$xmlOtherIdentification->Issr);
83
            }
84
85
        }
86
87 1
        if ($account instanceof DTO\Account) {
88 1
            if ($xmlRecord->Acct->Ownr) {
89
                $this->accountAddOwnerInfo($account, $xmlRecord->Acct->Ownr);
90
            }
91 1
            if ($xmlRecord->Acct->Svcr) {
92
                $this->accountAddServicerInfo($account, $xmlRecord->Acct->Svcr);
93
            }
94 1
            if ($xmlRecord->Acct->Ccy) {
95
                $account->setCurrency(new \Money\Currency((string)$xmlRecord->Acct->Ccy));
96
            }
97 1
            return $account;
98
        }
99
100
        return null;
101
    }
102
}
103