Completed
Push — master ( aabf4d...606f02 )
by Frederik
9s
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 16
    public function __construct(Entry $entryDecoder)
20
    {
21 16
        $this->entryDecoder = $entryDecoder;
22 16
    }
23
24
    /**
25
     * @param DTO\Statement    $statement
26
     * @param SimpleXMLElement $xmlStatement
27
     */
28 11
    public function addBalances(DTO\Statement $statement, SimpleXMLElement $xmlStatement)
29
    {
30 11
        $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 11
        foreach ($xmlBalances as $xmlBalance) {
32 10
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
33 10
            $currency = (string)$xmlBalance->Amt['Ccy'];
34 10
            $date = (string)$xmlBalance->Dt->Dt;
35
36 10
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
37 1
                $amount = $amount * -1;
38 1
            }
39
40 10
            if (isset($xmlBalance->Tp)
41 10
                && isset($xmlBalance->Tp->CdOrPrtry)
42 10
                && (string) $xmlBalance->Tp->CdOrPrtry->Cd === 'OPBD'
43 10
            ) {
44 10
                $balance = DTO\Balance::opening(
45 10
                    new Money(
46 10
                        $amount,
47 10
                        new Currency($currency)
48 10
                    ),
49 10
                    new DateTimeImmutable($date)
50 10
                );
51 10
            } else {
52 9
                $balance = DTO\Balance::closing(
53 9
                    new Money(
54 9
                        $amount,
55 9
                        new Currency($currency)
56 9
                    ),
57 9
                    new DateTimeImmutable($date)
58 9
                );
59
            }
60
61 10
            $statement->addBalance($balance);
62 11
        }
63 11
    }
64
65
    /**
66
     * @param DTO\Statement    $statement
67
     * @param SimpleXMLElement $xmlStatement
68
     */
69 11
    public function addEntries(DTO\Statement $statement, SimpleXMLElement $xmlStatement)
70
    {
71 11
        $index = 0;
72 11
        $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...
73 11
        foreach ($xmlEntries as $xmlEntry) {
74 10
            $amount = StringToUnits::convert((string) $xmlEntry->Amt);
75 10
            $currency = (string)$xmlEntry->Amt['Ccy'];
76 10
            $bookingDate = (string)$xmlEntry->BookgDt->Dt;
77 10
            $valueDate = (string)$xmlEntry->ValDt->Dt;
78
79 10
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
80 2
                $amount = $amount * -1;
81 2
            }
82
83 10
            $entry = new DTO\Entry(
84 10
                $statement,
85 10
                $index,
86 10
                new Money($amount, new Currency($currency)),
87 10
                new DateTimeImmutable($bookingDate),
88 10
                new DateTimeImmutable($valueDate)
89 10
            );
90
91 10
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
92 1
                $entry->setReversalIndicator(true);
93 1
            }
94
95 10
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
96 1
                $entry->setReference((string) $xmlEntry->NtryRef);
97 1
            }
98
99 10
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
100 3
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
101 3
            }
102
103 10
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
104 1
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
105 1
            }
106
107 10
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
108
109 10
            $statement->addEntry($entry);
110 10
            $index++;
111 11
        }
112 11
    }
113
}
114