Completed
Push — master ( b2bf11...c2b2ec )
by Markus
24s
created

GetStatementOfAccount::addFromArray()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 28
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 7
nop 2
crap 30
1
<?php
2
3
namespace Fhp\Response;
4
5
use Fhp\Model\StatementOfAccount\Statement;
6
use Fhp\Model\StatementOfAccount\StatementOfAccount;
7
use Fhp\Model\StatementOfAccount\Transaction;
8
use Fhp\Parser\MT940;
9
10
/**
11
 * Class GetStatementOfAccount
12
 * @package Fhp\Response
13
 */
14
class GetStatementOfAccount extends Response
15
{
16
    const SEG_ACCOUNT_INFORMATION = 'HIKAZ';
17
18
    /**
19
     * Gets the raw MT940 string from response.
20
     *
21
     * @return string
22
     */
23
    public function getRawMt940()
24
    {
25
        $seg = $this->findSegment(static::SEG_ACCOUNT_INFORMATION);
26
        if (is_string($seg)) {
27
            if (preg_match('/@(\d+)@(.+)/ms', $seg, $m)) {
28
                return $m[2];
29
            }
30
        }
31
32
        return '';
33
    }
34
35
    /**
36
     * Creates StatementOfAccount object from raw MT940 string.
37
     *
38
     * @param $rawMt940
39
     * @return StatementOfAccount
40
     */
41
    public static function createModelFromRawMt940($rawMt940)
42
    {
43
        $parser = new MT940($rawMt940);
44
45
        return static::createModelFromArray($parser->parse(MT940::TARGET_ARRAY));
0 ignored issues
show
Bug introduced by
It seems like $parser->parse(\Fhp\Parser\MT940::TARGET_ARRAY) targeting Fhp\Parser\MT940::parse() can also be of type null; however, Fhp\Response\GetStatemen...:createModelFromArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46
    }
47
48
    /**
49
     * Adds statements to an existing StatementOfAccount object.
50
     *
51
     * @param array $array
52
     * @param StatementOfAccount $statementOfAccount
53
     * @return StatementOfAccount
54
     */
55
    protected static function addFromArray(array $array, StatementOfAccount $statementOfAccount)
56
    {
57
        foreach ($array as $date => $statement) {
58
            if ($statementOfAccount->hasStatementForDate($date)) {
59
                $statementModel = $statementOfAccount->getStatementForDate($date);
60
            } else {
61
                $statementModel = new Statement();
62
                $statementModel->setDate(new \DateTime($date));
63
                $statementModel->setStartBalance((float) $statement['start_balance']['amount']);
64
                $statementModel->setCreditDebit($statement['start_balance']['credit_debit']);
65
                $statementOfAccount->addStatement($statementModel);
66
            }
67
68
            if (isset($statement['transactions'])) {
69
                foreach ($statement['transactions'] as $trx) {
70
                    $transaction = new Transaction();
71
                    $transaction->setBookingDate(new \DateTime($trx['booking_date']));
72
                    $transaction->setValutaDate(new \DateTime($trx['valuta_date']));
73
                    $transaction->setCreditDebit($trx['credit_debit']);
74
                    $transaction->setAmount($trx['amount']);
75
                    $transaction->setBookingText($trx['description']['booking_text']);
76
                    $transaction->setDescription1($trx['description']['description_1']);
77
                    $transaction->setDescription2($trx['description']['description_2']);
78
                    $transaction->setBankCode($trx['description']['bank_code']);
79
                    $transaction->setAccountNumber($trx['description']['account_number']);
80
                    $transaction->setName($trx['description']['name']);
81
                    $statementModel->addTransaction($transaction);
82
                }
83
            }
84
        }
85
86
        return $statementOfAccount;
87
    }
88
89
    /**
90
     * Creates a StatementOfAccount model from array.
91
     *
92
     * @param array $array
93
     * @return StatementOfAccount
94
     */
95
    protected static function createModelFromArray(array $array)
96
    {
97
        $soa = static::addFromArray($array, new StatementOfAccount());
98
99
        return $soa;
100
    }
101
}
102