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\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\Exception\InvalidMessageException;
12
use Genkgo\Camt\Iban;
13
use SimpleXMLElement;
14
15
abstract class Message extends BaseMessageDecoder
16 8
{
17
    public function addRecords(DTO\Message $message, SimpleXMLElement $document): void
18 8
    {
19
        $reports = [];
20 8
21 8
        $xmlReports = $this->getRootElement($document)->Rpt;
22 8
        foreach ($xmlReports as $xmlReport) {
23 8
            $report = new Camt052DTO\Report(
24 8
                (string) $xmlReport->Id,
25 8
                $this->dateDecoder->decode((string) $xmlReport->CreDtTm),
26
                $this->getAccount($xmlReport)
0 ignored issues
show
Bug introduced by
It seems like $xmlReport can also be of type null; however, parameter $xmlRecord of Genkgo\Camt\Camt052\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 */ $xmlReport)
Loading history...
27
            );
28 8
29 3
            if (isset($xmlReport->RptPgntn)) {
30 3
                $report->setPagination(new DTO\Pagination(
31 3
                    (string) $xmlReport->RptPgntn->PgNb,
32
                    ('true' === (string) $xmlReport->RptPgntn->LastPgInd) ? true : false
33
                ));
34
            }
35 8
36 7
            if (isset($xmlReport->AddtlRptInf)) {
37
                $report->setAdditionalInformation((string) $xmlReport->AddtlRptInf);
38
            }
39 8
40 8
            $this->addCommonRecordInformation($report, $xmlReport);
41 8
            $this->recordDecoder->addBalances($report, $xmlReport);
42
            $this->recordDecoder->addEntries($report, $xmlReport);
43 8
44
            $reports[] = $report;
45
        }
46 8
47 8
        $message->setRecords($reports);
48
    }
49 8
50
    protected function getAccount(SimpleXMLElement $xmlRecord): Account
51 8
    {
52 3
        if (isset($xmlRecord->Acct->Id->IBAN)) {
53
            return new DTO\IbanAccount(new Iban((string) $xmlRecord->Acct->Id->IBAN));
54
        }
55 8
56
        if (isset($xmlRecord->Acct->Id->BBAN)) {
57
            return new DTO\BBANAccount((string) $xmlRecord->Acct->Id->BBAN);
58
        }
59 8
60
        if (isset($xmlRecord->Acct->Id->UPIC)) {
61
            return new DTO\UPICAccount((string) $xmlRecord->Acct->Id->UPIC);
62
        }
63 8
64 4
        if (isset($xmlRecord->Acct->Id->PrtryAcct)) {
65
            return new DTO\ProprietaryAccount((string) $xmlRecord->Acct->Id->PrtryAcct->Id);
66
        }
67 7
68 7
        if (isset($xmlRecord->Acct->Id->Othr)) {
69 7
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
70
            $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
71 7
72
            if (isset($xmlOtherIdentification->SchmeNm)) {
73
                if (isset($xmlOtherIdentification->SchmeNm->Cd)) {
74
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Cd);
75
                }
76
77
                if (isset($xmlOtherIdentification->SchmeNm->Prtry)) {
78
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Prtry);
79
                }
80
            }
81 7
82
            if (isset($xmlOtherIdentification->Issr)) {
83
                $otherAccount->setIssuer((string) $xmlOtherIdentification->Issr);
84
            }
85 7
86
            return $otherAccount;
87
        }
88
89
        throw new InvalidMessageException('Cannot decode account');
90
    }
91
}
92