Failed Conditions
Push — master ( 65cf51...40aca8 )
by Adrien
03:16
created

Message::getRootElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Genkgo\Camt\Camt053\Decoder;
4
5
use Genkgo\Camt\Decoder\Message as BaseMessageDecoder;
6
use Genkgo\Camt\Camt053\DTO as Camt053DTO;
7
use Genkgo\Camt\DTO;
8
use SimpleXMLElement;
9
use Genkgo\Camt\Iban;
10
11
class Message extends BaseMessageDecoder
12
{
13
    /**
14
     * @param DTO\Message      $message
15
     * @param SimpleXMLElement $document
16
     */
17 12
    public function addRecords(DTO\Message $message, SimpleXMLElement $document)
18
    {
19 12
        $statements = [];
20
21 12
        $xmlStatements = $this->getRootElement($document)->Stmt;
22 12
        foreach ($xmlStatements as $xmlStatement) {
23 12
            $statement = new Camt053DTO\Statement(
24 12
                (string) $xmlStatement->Id,
25 12
                $this->dateDecoder->decode((string)$xmlStatement->CreDtTm),
26 12
                $this->getAccount($xmlStatement)
27
            );
28
29 12
            if (isset($xmlStatement->StmtPgntn)) {
30 6
                $statement->setPagination(new DTO\Pagination(
31 6
                    (string) $xmlStatement->StmtPgntn->PgNb,
32 6
                    ('true' === (string) $xmlStatement->StmtPgntn->LastPgInd) ? true : false
33
                ));
34
            }
35
36 12
            if (isset($xmlStatement->AddtlStmtInf)) {
37 11
                $statement->setAdditionalInformation((string) $xmlStatement->AddtlStmtInf);
38
            }
39
40 12
            $this->addCommonRecordInformation($statement, $xmlStatement);
41 12
            $this->recordDecoder->addBalances($statement, $xmlStatement);
42 12
            $this->recordDecoder->addEntries($statement, $xmlStatement);
43
44 12
            $statements[] = $statement;
45
        }
46
47 12
        $message->setRecords($statements);
48 12
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 13
    public function getRootElement(SimpleXMLElement $document)
54
    {
55 13
        return $document->BkToCstmrStmt;
56
    }
57
58
    /**
59
     * @param SimpleXMLElement $xmlRecord
60
     *
61
     * @return DTO\Account
62
     */
63 12
    protected function getAccount(SimpleXMLElement $xmlRecord)
64
    {
65 12
        if (isset($xmlRecord->Acct->Id->IBAN)) {
66 10
            return new DTO\IbanAccount(new Iban((string) $xmlRecord->Acct->Id->IBAN));
67
        }
68
69 7
        $xmlOtherIdentification = $xmlRecord->Acct->Id->Othr;
70 7
        $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
71
72 7
        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
82 7
        if (isset($xmlOtherIdentification->Issr)) {
83
            $otherAccount->setIssuer($xmlOtherIdentification->Issr);
84
        }
85
86 7
        return $otherAccount;
87
    }
88
}
89