Passed
Push — master ( 2c0c97...e77480 )
by Robin
01:12
created

Rabo   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 186
rs 9.44
c 0
b 0
f 0
wmc 37

9 Methods

Rating   Name   Duplication   Size   Complexity  
A parseTransactionAccount() 0 17 5
A parseTransactionEntryTimestamp() 0 8 3
A sanitizeAccount() 0 8 3
A parseTransactionValueTimestamp() 0 8 3
A parseStatementBank() 0 3 1
A isApplicable() 0 3 1
A parseTransactionType() 0 31 4
B parseTransactionAccountName() 0 25 7
B sanitizeDescription() 0 21 10
1
<?php
2
3
namespace Kingsquare\Parser\Banking\Mt940\Engine;
4
5
use Kingsquare\Parser\Banking\Mt940\Engine;
6
use Kingsquare\Banking\Transaction\Type;
7
8
/**
9
 * @author Kingsquare ([email protected])
10
 * @license http://opensource.org/licenses/MIT MIT
11
 */
12
class Rabo extends Engine
13
{
14
    /**
15
     * returns the name of the bank.
16
     *
17
     * @return string
18
     */
19
    protected function parseStatementBank()
20
    {
21
        return 'Rabo';
22
    }
23
24
    /**
25
     * Overloaded: Rabo has different way of storing account info.
26
     *
27
     * @inheritdoc
28
     */
29
    protected function parseTransactionAccount()
30
    {
31
        $results = [];
32
        // SEPA MT940 Structured
33
        if (preg_match('/^:61:.*\n(.*?)(\n|\:8)/im', $this->getCurrentTransactionData(), $results)
34
            && !empty($results[1])
35
        ) {
36
            return $this->sanitizeAccount($results[1]);
37
        }
38
39
        if (preg_match('/^:61:.{26}(.{16})/m', $this->getCurrentTransactionData(), $results)
40
            && !empty($results[1])
41
        ) {
42
            return $this->sanitizeAccount($results[1]);
43
        }
44
45
        return '';
46
    }
47
48
    /**
49
     * Overloaded: Rabo has different way of storing account name.
50
     *
51
     * @inheritdoc
52
     */
53
    protected function parseTransactionAccountName()
54
    {
55
        $results = [];
56
        // SEPA MT940 Structured
57
        if (preg_match('#/NAME/(.+?)\n?/(REMI|ADDR|ISDT|CSID)/#ms', $this->getCurrentTransactionData(), $results)) {
58
            $accountName = trim($results[1]);
59
            if (!empty($accountName)) {
60
                return $this->sanitizeAccountName($accountName);
61
            }
62
        }
63
64
        if (preg_match('/^:61:.*? (.+)/m', $this->getCurrentTransactionData(), $results)) {
65
            $accountName = trim($results[1]);
66
            if (!empty($accountName)) {
67
                return $this->sanitizeAccountName($accountName);
68
            }
69
        }
70
71
        if (preg_match('/(.*) Betaalautomaat/', $this->parseTransactionDescription(), $results)) {
72
            $accountName = trim($results[1]);
73
            if (!empty($accountName)) {
74
                return $this->sanitizeAccountName($accountName);
75
            }
76
        }
77
        return '';
78
    }
79
80
    /**
81
     * Overloaded: Rabo has different way of storing transaction value timestamps (ymd).
82
     *
83
     * @inheritdoc
84
     */
85
    protected function parseTransactionEntryTimestamp()
86
    {
87
        $results = [];
88
        if (preg_match('/^:60F:[C|D]([\d]{6})/m', $this->getCurrentStatementData(), $results) && !empty($results[1])) {
89
            return $this->sanitizeTimestamp($results[1]);
90
        }
91
92
        return 0;
93
    }
94
95
    /**
96
     * Overloaded: Rabo has different way of storing transaction value timestamps (ymd).
97
     *
98
     * @inheritdoc
99
     */
100
    protected function parseTransactionValueTimestamp()
101
    {
102
        $results = [];
103
        if (preg_match('/^:61:([\d]{6})[C|D]/', $this->getCurrentTransactionData(), $results) && !empty($results[1])) {
104
            return $this->sanitizeTimestamp($results[1]);
105
        }
106
107
        return 0;
108
    }
109
110
    /**
111
     * Overloaded: Rabo uses longer strings for accountnumbers.
112
     *
113
     * @inheritdoc
114
     */
115
    protected function sanitizeAccount($string)
116
    {
117
        $account = parent::sanitizeAccount($string);
118
        if (strlen($account) > 20 && strpos($account, '80000') === 0) {
119
            $account = substr($account, 5);
120
        }
121
122
        return $account;
123
    }
124
125
    /**
126
     * Overloaded: Rabo encapsulates the description with /REMI/ for SEPA.
127
     *
128
     * @inheritdoc
129
     */
130
    protected function sanitizeDescription($string)
131
    {
132
        $description = parent::sanitizeDescription($string);
133
        if (strpos($description, '/REMI/') !== false
134
            && preg_match('#/REMI/(.*?)(/((PURP|ISDT|CSID|RTRN)/)|$)#s', $description, $results) && !empty($results[1])
135
        ) {
136
            return $results[1];
137
        }
138
        if (strpos($description, '/EREF/') !== false
139
            && preg_match('#/EREF/(.*?)/(ORDP)/#s', $description, $results) && !empty($results[1])
140
        ) {
141
            return $results[1];
142
        }
143
144
        if (strpos($description, '/PREF/') !== false
145
            && preg_match('#/PREF/(.*)/?#s', $description, $results) && !empty($results[1])
146
        ) {
147
            return $results[1];
148
        }
149
150
        return $description;
151
    }
152
153
    /**
154
     * Overloaded: Is applicable if first line has :940:.
155
     *
156
     * @inheritdoc
157
     */
158
    public static function isApplicable($string)
159
    {
160
        return strpos(strtok($string, "\r\n\t"), ':940:') !== false;
161
    }
162
163
    /**
164
     * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
165
     * @return int
166
     */
167
    protected function parseTransactionType()
168
    {
169
        static $map = [
170
            102 => Type::SEPA_TRANSFER,  // "Betaalopdracht IDEAL"
171
            541 => Type::SEPA_TRANSFER,
172
            544 => Type::SEPA_TRANSFER,
173
            547 => Type::SEPA_TRANSFER,
174
            504 => Type::SAVINGS_TRANSFER,
175
            691 => Type::SAVINGS_TRANSFER,
176
            64 => Type::SEPA_DIRECTDEBIT,
177
            93 => Type::BANK_COSTS,
178
            12 => Type::PAYMENT_TERMINAL,
179
            13 => Type::PAYMENT_TERMINAL,
180
            30 => Type::PAYMENT_TERMINAL,
181
            29 => Type::ATM_WITHDRAWAL,
182
            31 => Type::ATM_WITHDRAWAL,
183
            79 => Type::UNKNOWN,
184
            'MSC' => Type::BANK_INTEREST,
185
        ];
186
187
        $code = $this->parseTransactionCode();
188
        if ($code === 404) {
0 ignored issues
show
introduced by
The condition $code === 404 is always false.
Loading history...
189
            return (stripos($this->getCurrentTransactionData(),
190
                    'eurobetaling') !== false) ? Type::SEPA_TRANSFER : Type::TRANSFER;
191
        }
192
193
        if (array_key_exists($code, $map)) {
194
            return $map[$code];
195
        }
196
197
        throw new \RuntimeException("Don't know code $code for this bank");
198
    }
199
200
}
201