GetStatementOfAccount   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 5
dl 0
loc 89
ccs 0
cts 42
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRawMt940() 0 11 3
A createModelFromRawMt940() 0 6 1
A addFromArray() 0 34 5
A createModelFromArray() 0 6 1
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 string $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->setStructuredDescription($trx['description']['description']);
79
                    $transaction->setBankCode($trx['description']['bank_code']);
80
                    $transaction->setAccountNumber($trx['description']['account_number']);
81
                    $transaction->setName($trx['description']['name']);
82
                    $statementModel->addTransaction($transaction);
83
                }
84
            }
85
        }
86
87
        return $statementOfAccount;
88
    }
89
90
    /**
91
     * Creates a StatementOfAccount model from array.
92
     *
93
     * @param array $array
94
     * @return StatementOfAccount
95
     */
96
    protected static function createModelFromArray(array $array)
97
    {
98
        $soa = static::addFromArray($array, new StatementOfAccount());
99
100
        return $soa;
101
    }
102
}
103