Completed
Pull Request — master (#16)
by Yann
02:20
created

Message::getAccount()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 12.1743

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 25
ccs 8
cts 18
cp 0.4444
rs 8.439
cc 6
eloc 13
nc 11
nop 1
crap 12.1743
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 12
    public function __construct(Statement $statementDecoder)
18
    {
19 12
        $this->statementDecoder = $statementDecoder;
20 12
    }
21
22
    /**
23
     * @param DTO\Message      $message
24
     * @param SimpleXMLElement $document
25
     */
26 10
    public function addGroupHeader(DTO\Message $message, SimpleXMLElement $document)
27
    {
28 10
        $xmlGroupHeader = $document->BkToCstmrStmt->GrpHdr;
29 10
        $groupHeader = new DTO\GroupHeader(
30 10
            (string)$xmlGroupHeader->MsgId,
31 10
            new DateTimeImmutable((string)$xmlGroupHeader->CreDtTm)
32 10
        );
33
34 10
        $message->setGroupHeader($groupHeader);
35 10
    }
36
37
    /**
38
     * @param DTO\Message      $message
39
     * @param SimpleXMLElement $document
40
     */
41 10
    public function addStatements(DTO\Message $message, SimpleXMLElement $document)
42 9
    {
43 10
        $statements = [];
44
45 10
        $xmlStatements = $document->BkToCstmrStmt->Stmt;
46 10
        foreach ($xmlStatements as $xmlStatement) {
47 10
            $statement = new DTO\Statement(
48 10
                (string) $xmlStatement->Id,
49 10
                new DateTimeImmutable((string)$xmlStatement->CreDtTm),
50 10
                $this->getAccount($xmlStatement)
51 10
            );
52
53 10
            $this->statementDecoder->addBalances($statement, $xmlStatement);
54 10
            $this->statementDecoder->addEntries($statement, $xmlStatement);
55
56 10
            $statements[] = $statement;
57 10
        }
58
59 10
        $message->setStatements($statements);
60 10
    }
61
62
    /**
63
     * @param SimpleXMLElement $xmlStatement
64
     *
65
     * @return DTO\Account
66
     */
67 10
    private function getAccount(SimpleXMLElement $xmlStatement)
68
    {
69 10
        if (isset($xmlStatement->Acct->Id->IBAN)) {
70 10
            return new DTO\IbanAccount(new Iban((string) $xmlStatement->Acct->Id->IBAN));
71
        }
72
73
        $xmlOtherIdentification = $xmlStatement->Acct->Id->Othr;
74 9
        $otherAccount = new DTO\OtherAccount((string) $xmlOtherIdentification->Id);
75
76 9
        if (isset($otherIdentification->SchmeNm)) {
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...
77 9
            if (isset($otherIdentification->SchmeNm->Cd)) {
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...
78
                $otherAccount->setSchemeName((string) $xmlOtherIdentification->SchmeNm->Cd);
79 9
            }
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
        if (isset($otherIdentification->Issr)) {
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...
87
            $otherAccount->setIssuer($xmlOtherIdentification->Issr);
88
        }
89
90
        return $otherAccount;
91 7
    }
92
}
93