Completed
Push — master ( ee61c0...1ce428 )
by Adrien
03:11
created

Message::getAccount()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 15.5306

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