Completed
Push — master ( a5ff2b...bf9940 )
by Yann
05:35
created

Message::addRecords()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 25
cts 25
cp 1
rs 8.439
cc 5
eloc 19
nc 5
nop 2
crap 5
1
<?php
2
3
namespace Genkgo\Camt\Camt052\Decoder;
4
5
use Genkgo\Camt\Decoder\Message as BaseMessageDecoder;
6
use Genkgo\Camt\DTO;
7
use Genkgo\Camt\Camt052\DTO as Camt052DTO;
8
use \SimpleXMLElement;
9
use \DateTimeImmutable;
10
use Genkgo\Camt\Iban;
11
12
abstract class Message extends BaseMessageDecoder
13
{
14
    /**
15
     * @param DTO\Message      $message
16
     * @param SimpleXMLElement $document
17
     */
18 8
    public function addRecords(DTO\Message $message, SimpleXMLElement $document)
19
    {
20 8
        $reports = [];
21
22 8
        $xmlReports = $this->getRootElement($document)->Rpt;
0 ignored issues
show
Bug introduced by
The property Rpt does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
23 8
        foreach ($xmlReports as $xmlReport) {
24 8
            $report = new Camt052DTO\Report(
25 8
                (string) $xmlReport->Id,
26 8
                new DateTimeImmutable((string)$xmlReport->CreDtTm),
27 8
                $this->getAccount($xmlReport)
0 ignored issues
show
Bug introduced by
It seems like $this->getAccount($xmlReport) targeting Genkgo\Camt\Camt052\Decoder\Message::getAccount() can also be of type null; however, Genkgo\Camt\DTO\Record::__construct() does only seem to accept object<Genkgo\Camt\DTO\Account>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
28 8
            );
29
30 8
            if (isset($xmlReport->RptPgntn)) {
31 3
                $report->setPagination(new DTO\Pagination(
32 3
                    (string) $xmlReport->RptPgntn->PgNb,
33 3
                    ('true' === (string) $xmlReport->RptPgntn->LastPgInd) ? true : false
34 3
                ));
35 3
            }
36
37 8
            if (isset($xmlReport->AddtlRptInf)) {
38 7
                $report->setAdditionalInformation((string) $xmlReport->AddtlRptInf);
39 7
            }
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 8
        }
47
48 8
        $message->setRecords($reports);
49 8
    }
50
51
    /**
52
     * @param SimpleXMLElement $xmlRecord
53
     *
54
     * @return DTO\Account
55
     */
56 8
    protected function getAccount(SimpleXMLElement $xmlRecord)
57
    {
58 8
        if (isset($xmlRecord->Acct->Id->IBAN)) {
59 3
            return new DTO\IbanAccount(new Iban((string) $xmlRecord->Acct->Id->IBAN));
60
        }
61
62 8
        if (isset($xmlRecord->Acct->Id->BBAN)) {
63
            return new DTO\BBANAccount((string) $xmlRecord->Acct->Id->BBAN);
64
        }
65
66 8
        if (isset($xmlRecord->Acct->Id->UPIC)) {
67
            return new DTO\UPICAccount((string) $xmlRecord->Acct->Id->UPIC);
68 7
        }
69
70 8
        if (isset($xmlRecord->Acct->Id->PrtryAcct)) {
71 8
            return new DTO\ProprietaryAccount((string) $xmlRecord->Acct->Id->PrtryAcct->Id);
72 7
        }
73
74 7
        if (isset($xmlRecord->Acct->Id->Othr)) {
75 7
            $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
76 7
            $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
77
78 7
            if (isset($xmlOtherIdentification->SchmeNm)) {
79
                if (isset($xmlOtherIdentification->SchmeNm->Cd)) {
80 7
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Cd);
81
                }
82
83
                if (isset($xmlOtherIdentification->SchmeNm->Prtry)) {
84
                    $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Prtry);
85
                }
86
            }
87
88 7
            if (isset($xmlOtherIdentification->Issr)) {
89
                $otherAccount->setIssuer($xmlOtherIdentification->Issr);
90
            }
91
92 7
            return $otherAccount;
93
        }
94
    }
95
}
96