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

Message::addRecords()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 30
ccs 18
cts 18
cp 1
crap 5
rs 9.3888
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 5
    public function addRecords(DTO\Message $message, SimpleXMLElement $document): void
17
    {
18 5
        $notifications = [];
19
20 5
        $xmlNotifications = $this->getRootElement($document)->Ntfctn;
21 5
        foreach ($xmlNotifications as $xmlNotification) {
22 5
            $notification = new Camt054DTO\Notification(
23 5
                (string) $xmlNotification->Id,
24 5
                $this->dateDecoder->decode((string) $xmlNotification->CreDtTm),
25 5
                $this->getAccount($xmlNotification)
26
            );
27
28 5
            if (isset($xmlNotification->NtfctnPgntn)) {
29 4
                $notification->setPagination(new DTO\Pagination(
30 4
                    (string) $xmlNotification->NtfctnPgntn->PgNb,
31 4
                    ('true' === (string) $xmlNotification->NtfctnPgntn->LastPgInd) ? true : false
32
                ));
33
            }
34
35 5
            if (isset($xmlNotification->AddtlNtfctnInf)) {
36 4
                $notification->setAdditionalInformation((string) $xmlNotification->AddtlNtfctnInf);
37
            }
38
39 5
            $this->addCommonRecordInformation($notification, $xmlNotification);
40 5
            $this->recordDecoder->addEntries($notification, $xmlNotification);
41
42 5
            $notifications[] = $notification;
43
        }
44
45 5
        $message->setRecords($notifications);
46 5
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function getRootElement(SimpleXMLElement $document): SimpleXMLElement
52
    {
53 6
        return $document->BkToCstmrDbtCdtNtfctn;
54
    }
55
56 5
    protected function getAccount(SimpleXMLElement $xmlRecord): ?Account
57
    {
58 5
        $account = null;
59 5
        if (isset($xmlRecord->Acct->Id->IBAN)) {
60 4
            $account = new DTO\IbanAccount(new Iban((string)$xmlRecord->Acct->Id->IBAN));
61 5
        } elseif (isset($xmlRecord->Acct->Id->BBAN)) {
62
            $account = new DTO\BBANAccount((string)$xmlRecord->Acct->Id->BBAN);
63 5
        } elseif (isset($xmlRecord->Acct->Id->UPIC)) {
64
            $account = new DTO\UPICAccount((string)$xmlRecord->Acct->Id->UPIC);
65 5
        } elseif (isset($xmlRecord->Acct->Id->PrtryAcct)) {
66 1
            $account = new DTO\ProprietaryAccount((string)$xmlRecord->Acct->Id->PrtryAcct->Id);
67 4
        } elseif (isset($xmlRecord->Acct->Id->Othr)) {
68 4
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
69 4
            $account = new DTO\OtherAccount((string)$xmlOtherIdentification->Id);
70
71 4
            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 4
            if (isset($xmlOtherIdentification->Issr)) {
82
                $account->setIssuer((string)$xmlOtherIdentification->Issr);
83
            }
84
85
        }
86
87 5
        if ($account instanceof DTO\Account) {
88 5
            if ($xmlRecord->Acct->Ownr) {
89 4
                $this->accountAddOwnerInfo($account, $xmlRecord->Acct->Ownr);
90
            }
91 5
            if ($xmlRecord->Acct->Svcr) {
92 4
                $this->accountAddServicerInfo($account, $xmlRecord->Acct->Svcr);
93
            }
94 5
            if ($xmlRecord->Acct->Ccy) {
95
                $account->setCurrency(new \Money\Currency((string)$xmlRecord->Acct->Ccy));
96
            }
97 5
            return $account;
98
        }
99
100
        return null;
101
    }
102
}
103