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

Message::getAccount()   C

Complexity

Conditions 14
Paths 135

Size

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