Completed
Push — master ( 606f02...b5ae25 )
by Frederik
9s
created

Message::getAccount()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.1174

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 25
ccs 11
cts 18
cp 0.6111
rs 8.439
cc 6
eloc 13
nc 11
nop 1
crap 8.1174
1
<?php
2
3
namespace Genkgo\Camt\Camt053\Decoder;
4
5
use Genkgo\Camt\Camt053\DTO;
6
use DateTimeImmutable;
7
use SimpleXMLElement;
8
use Genkgo\Camt\Iban;
9
10
class Message
11
{
12
    /**
13
     * @var Statement
14
     */
15
    private $statementDecoder;
16
17 13
    public function __construct(Statement $statementDecoder)
18
    {
19 13
        $this->statementDecoder = $statementDecoder;
20 13
    }
21
22
    /**
23
     * @param DTO\Message      $message
24
     * @param SimpleXMLElement $document
25
     */
26 11
    public function addGroupHeader(DTO\Message $message, SimpleXMLElement $document)
27
    {
28 11
        $xmlGroupHeader = $document->BkToCstmrStmt->GrpHdr;
29 11
        $groupHeader = new DTO\GroupHeader(
30 11
            (string)$xmlGroupHeader->MsgId,
31 11
            new DateTimeImmutable((string)$xmlGroupHeader->CreDtTm)
32 11
        );
33
34 11
        $message->setGroupHeader($groupHeader);
35 11
    }
36
37
    /**
38
     * @param DTO\Message      $message
39
     * @param SimpleXMLElement $document
40
     */
41 11
    public function addStatements(DTO\Message $message, SimpleXMLElement $document)
42 10
    {
43 11
        $statements = [];
44
45 11
        $xmlStatements = $document->BkToCstmrStmt->Stmt;
46 11
        foreach ($xmlStatements as $xmlStatement) {
47 11
            $statement = new DTO\Statement(
48 11
                (string) $xmlStatement->Id,
49 11
                new DateTimeImmutable((string)$xmlStatement->CreDtTm),
50 11
                $this->getAccount($xmlStatement)
51 11
            );
52
53 11
            $this->statementDecoder->addBalances($statement, $xmlStatement);
54 11
            $this->statementDecoder->addEntries($statement, $xmlStatement);
55
56 11
            $statements[] = $statement;
57 11
        }
58
59 11
        $message->setStatements($statements);
60 11
    }
61
62
    /**
63
     * @param SimpleXMLElement $xmlStatement
64
     *
65
     * @return DTO\Account
66
     */
67 11
    private function getAccount(SimpleXMLElement $xmlStatement)
68
    {
69 11
        if (isset($xmlStatement->Acct->Id->IBAN)) {
70 9
            return new DTO\IbanAccount(new Iban((string) $xmlStatement->Acct->Id->IBAN));
71
        }
72
73 7
        $xmlOtherIdentification = $xmlStatement->Acct->Id->Othr;
74 10
        $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
75
76 10
        if (isset($xmlOtherIdentification->SchmeNm)) {
77 10
            if (isset($xmlOtherIdentification->SchmeNm->Cd)) {
78
                $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Cd);
79 10
            }
80
81
            if (isset($otherIdentification->SchmeNm->Prtry)) {
0 ignored issues
show
Bug introduced by
The variable $otherIdentification does not exist. Did you mean $xmlOtherIdentification?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
82
                $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Prtry);
83
            }
84
        }
85
86 7
        if (isset($xmlOtherIdentification->Issr)) {
87
            $otherAccount->setIssuer($xmlOtherIdentification->Issr);
88
        }
89
90 7
        return $otherAccount;
91 8
    }
92
}
93