Rabo   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 74
dl 0
loc 192
rs 9.36
c 6
b 0
f 0
wmc 38

9 Methods

Rating   Name   Duplication   Size   Complexity  
A parseTransactionAccount() 0 17 5
A parseStatementBank() 0 3 1
A parseTransactionEntryTimestamp() 0 8 3
A sanitizeAccount() 0 8 3
A parseTransactionValueTimestamp() 0 8 3
B parseTransactionAccountName() 0 31 8
B sanitizeDescription() 0 21 10
A isApplicable() 0 3 1
A parseTransactionType() 0 31 4
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
        
78
        if (preg_match('#/NAME/(.+?)/#ms',$this->getCurrentTransactionData(), $results)) // Last chance test to see if we get something from the AM04 or something
79
        {
80
          $accountName = trim($results[1]);
81
          return $this->sanitizeAccountName($accountName);
82
        }
83
        return '';
84
    }
85
86
    /**
87
     * Overloaded: Rabo has different way of storing transaction value timestamps (ymd).
88
     *
89
     * @inheritdoc
90
     */
91
    protected function parseTransactionEntryTimestamp()
92
    {
93
        $results = [];
94
        if (preg_match('/^:60F:[C|D]([\d]{6})/m', $this->getCurrentStatementData(), $results) && !empty($results[1])) {
95
            return $this->sanitizeTimestamp($results[1]);
96
        }
97
98
        return 0;
99
    }
100
101
    /**
102
     * Overloaded: Rabo has different way of storing transaction value timestamps (ymd).
103
     *
104
     * @inheritdoc
105
     */
106
    protected function parseTransactionValueTimestamp()
107
    {
108
        $results = [];
109
        if (preg_match('/^:61:([\d]{6})[C|D]/', $this->getCurrentTransactionData(), $results) && !empty($results[1])) {
110
            return $this->sanitizeTimestamp($results[1]);
111
        }
112
113
        return 0;
114
    }
115
116
    /**
117
     * Overloaded: Rabo uses longer strings for accountnumbers.
118
     *
119
     * @inheritdoc
120
     */
121
    protected function sanitizeAccount($string)
122
    {
123
        $account = parent::sanitizeAccount($string);
124
        if (strlen($account) > 20 && strpos($account, '80000') === 0) {
125
            $account = substr($account, 5);
126
        }
127
128
        return $account;
129
    }
130
131
    /**
132
     * Overloaded: Rabo encapsulates the description with /REMI/ for SEPA.
133
     *
134
     * @inheritdoc
135
     */
136
    protected function sanitizeDescription($string)
137
    {
138
        $description = parent::sanitizeDescription($string);
139
        if (strpos($description, '/REMI/') !== false
140
            && preg_match('#/REMI/(.*?)(/((PURP|ISDT|CSID|RTRN)/)|$)#s', $description, $results) && !empty($results[1])
141
        ) {
142
            return $results[1];
143
        }
144
        if (strpos($description, '/EREF/') !== false
145
            && preg_match('#/EREF/(.*?)/(ORDP)/#s', $description, $results) && !empty($results[1])
146
        ) {
147
            return $results[1];
148
        }
149
150
        if (strpos($description, '/PREF/') !== false
151
            && preg_match('#/PREF/(.*)/?#s', $description, $results) && !empty($results[1])
152
        ) {
153
            return $results[1];
154
        }
155
156
        return $description;
157
    }
158
159
    /**
160
     * Overloaded: Is applicable if first line has :940:.
161
     *
162
     * @inheritdoc
163
     */
164
    public static function isApplicable($string)
165
    {
166
        return strpos(strtok($string, "\r\n\t"), ':940:') !== false;
167
    }
168
169
    /**
170
     * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
171
     * @return int
172
     */
173
    protected function parseTransactionType()
174
    {
175
        static $map = [
176
            102 => Type::SEPA_TRANSFER,  // "Betaalopdracht IDEAL"
177
            541 => Type::SEPA_TRANSFER,
178
            544 => Type::SEPA_TRANSFER,
179
            547 => Type::SEPA_TRANSFER,
180
            504 => Type::SAVINGS_TRANSFER,
181
            691 => Type::SAVINGS_TRANSFER,
182
            64 => Type::SEPA_DIRECTDEBIT,
183
            93 => Type::BANK_COSTS,
184
            12 => Type::PAYMENT_TERMINAL,
185
            13 => Type::PAYMENT_TERMINAL,
186
            30 => Type::PAYMENT_TERMINAL,
187
            29 => Type::ATM_WITHDRAWAL,
188
            31 => Type::ATM_WITHDRAWAL,
189
            79 => Type::UNKNOWN,
190
            'MSC' => Type::BANK_INTEREST,
191
        ];
192
193
        $code = $this->parseTransactionCode();
194
        if ($code === 404) {
0 ignored issues
show
introduced by
The condition $code === 404 is always false.
Loading history...
195
            return (stripos($this->getCurrentTransactionData(),
196
                    'eurobetaling') !== false) ? Type::SEPA_TRANSFER : Type::TRANSFER;
197
        }
198
199
        if (array_key_exists($code, $map)) {
200
            return $map[$code];
201
        }
202
203
        throw new \RuntimeException("Don't know code $code for this bank");
204
    }
205
206
}
207