Completed
Pull Request — master (#14)
by Yann
02:46
created

Statement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Genkgo\Camt\Camt053\Decoder;
4
5
use Genkgo\Camt\Camt053\DTO;
6
use Genkgo\Camt\Util\StringToUnits;
7
use Money\Money;
8
use Money\Currency;
9
use \SimpleXMLElement;
10
use \DateTimeImmutable;
11
12
class Statement
13
{
14
    /**
15
     * @var Entry
16
     */
17
    private $entryDecoder;
18
19 10
    public function __construct(Entry $entryDecoder)
20
    {
21 10
        $this->entryDecoder = $entryDecoder;
22 10
    }
23
24
    /**
25
     * @param DTO\Statement    $statement
26
     * @param SimpleXMLElement $xmlStatement
27
     */
28 9
    public function addBalances(DTO\Statement $statement, SimpleXMLElement $xmlStatement)
29
    {
30 9
        $xmlBalances = $xmlStatement->Bal;
0 ignored issues
show
Bug introduced by
The property Bal 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...
31 9
        foreach ($xmlBalances as $xmlBalance) {
32 9
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
33 9
            $currency = (string)$xmlBalance->Amt['Ccy'];
34 9
            $date = (string)$xmlBalance->Dt->Dt;
35
36 9
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
37
                $amount = $amount * -1;
38
            }
39
40 9
            if ((string) $xmlBalance->Tp->CdOrPrtry->Cd === 'OPBD') {
41 9
                $balance = DTO\Balance::opening(
42 9
                    new Money(
43 9
                        $amount,
44 9
                        new Currency($currency)
45 9
                    ),
46 9
                    new DateTimeImmutable($date)
47 9
                );
48 9
            } else {
49 9
                $balance = DTO\Balance::closing(
50 9
                    new Money(
51 9
                        $amount,
52 9
                        new Currency($currency)
53 9
                    ),
54 9
                    new DateTimeImmutable($date)
55 9
                );
56
            }
57
58 9
            $statement->addBalance($balance);
59 9
        }
60 9
    }
61
62
    /**
63
     * @param DTO\Statement    $statement
64
     * @param SimpleXMLElement $xmlStatement
65
     */
66 9
    public function addEntries(DTO\Statement $statement, SimpleXMLElement $xmlStatement)
67
    {
68 9
        $index = 0;
69 9
        $xmlEntries = $xmlStatement->Ntry;
0 ignored issues
show
Bug introduced by
The property Ntry 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...
70 9
        foreach ($xmlEntries as $xmlEntry) {
71 9
            $amount = StringToUnits::convert((string) $xmlEntry->Amt);
72 9
            $currency = (string)$xmlEntry->Amt['Ccy'];
73 9
            $bookingDate = (string)$xmlEntry->BookgDt->Dt;
74 9
            $valueDate = (string)$xmlEntry->ValDt->Dt;
75
76 9
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
77 1
                $amount = $amount * -1;
78 1
            }
79
80 9
            $entry = new DTO\Entry(
81 9
                $statement,
82 9
                $index,
83 9
                new Money($amount, new Currency($currency)),
84 9
                new DateTimeImmutable($bookingDate),
85 9
                new DateTimeImmutable($valueDate)
86 9
            );
87
88 9
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
89
                $entry->setReversalIndicator(true);
90
            }
91
92 9
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
93
                $entry->setReference((string) $xmlEntry->NtryRef);
94
            }
95
96 9
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
97 2
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
98 2
            }
99
100 9
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
101
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
102
            }
103
104 9
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
105
106 9
            $statement->addEntry($entry);
107 9
            $index++;
108 9
        }
109 9
    }
110
}
111