Failed Conditions
Pull Request — master (#92)
by
unknown
03:25
created

Message::addRecords()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 33
ccs 20
cts 20
cp 1
crap 5
rs 9.3222
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 8
    public function addRecords(DTO\Message $message, SimpleXMLElement $document): void
17
    {
18 8
        $reports = [];
19
20 8
        $xmlReports = $this->getRootElement($document)->Rpt;
21 8
        foreach ($xmlReports as $xmlReport) {
22 8
            $report = new Camt052DTO\Report(
23 8
                (string)$xmlReport->Id,
24 8
                $this->dateDecoder->decode((string)$xmlReport->CreDtTm),
25 8
                $this->getAccount($xmlReport)
26
            );
27
28 8
            if (isset($xmlReport->RptPgntn)) {
29 3
                $report->setPagination(
30 3
                    new DTO\Pagination(
31 3
                        (string)$xmlReport->RptPgntn->PgNb,
32 3
                        ('true' === (string)$xmlReport->RptPgntn->LastPgInd) ? true : false
33
                    )
34
                );
35
            }
36
37 8
            if (isset($xmlReport->AddtlRptInf)) {
38 7
                $report->setAdditionalInformation((string)$xmlReport->AddtlRptInf);
39
            }
40
41 8
            $this->addCommonRecordInformation($report, $xmlReport);
42 8
            $this->recordDecoder->addBalances($report, $xmlReport);
43 8
            $this->recordDecoder->addEntries($report, $xmlReport);
44
45 8
            $reports[] = $report;
46
        }
47
48 8
        $message->setRecords($reports);
49 8
    }
50
51 8
    protected function getAccount(SimpleXMLElement $xmlRecord): ?Account
52
    {
53 8
        $account = null;
54 8
        if (isset($xmlRecord->Acct->Id->IBAN)) {
55 3
            $account = new DTO\IbanAccount(new Iban((string)$xmlRecord->Acct->Id->IBAN));
56 8
        } elseif (isset($xmlRecord->Acct->Id->BBAN)) {
57
            $account = new DTO\BBANAccount((string)$xmlRecord->Acct->Id->BBAN);
58 8
        } elseif (isset($xmlRecord->Acct->Id->UPIC)) {
59
            $account = new DTO\UPICAccount((string)$xmlRecord->Acct->Id->UPIC);
60 8
        } elseif (isset($xmlRecord->Acct->Id->PrtryAcct)) {
61 4
            $account = new DTO\ProprietaryAccount((string)$xmlRecord->Acct->Id->PrtryAcct->Id);
62 7
        } elseif (isset($xmlRecord->Acct->Id->Othr)) {
63 7
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
64 7
            $account = new DTO\OtherAccount((string)$xmlOtherIdentification->Id);
65
66 7
            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 7
            if (isset($xmlOtherIdentification->Issr)) {
77
                $account->setIssuer((string)$xmlOtherIdentification->Issr);
78
            }
79
        }
80
81 8
        if ($account instanceof DTO\Account) {
82 8
            if ($xmlRecord->Acct->Ownr) {
83 7
                $this->accountAddOwnerInfo($account, $xmlRecord->Acct->Ownr);
84
            }
85 8
            if ($xmlRecord->Acct->Svcr) {
86 7
                $this->accountAddServicerInfo($account, $xmlRecord->Acct->Svcr);
87
            }
88 8
            if ($xmlRecord->Acct->Ccy) {
89
                $account->setCurrency(new \Money\Currency((string)$xmlRecord->Acct->Ccy));
90
            }
91 8
            return $account;
92
        }
93
94
        return null;
95
    }
96
}
97