Passed
Push — master ( 459651...cce8a1 )
by Robin
01:42 queued 10s
created

Knab::parseTransactionType()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 10
nop 0
dl 0
loc 32
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kingsquare\Parser\Banking\Mt940\Engine;
4
5
use Kingsquare\Parser\Banking\Mt940\Engine;
6
use Kingsquare\Objects\TransactionType;
7
8
/**
9
 * Knab parser for Kingsquare mt940 package.
10
 *
11
 * @package Kingsquare\Parser\Banking\Mt940\Engine
12
 * @author Kingsquare ([email protected])
13
 * @author Sam Mousa ([email protected])
14
 * @license http://opensource.org/licenses/MIT MIT
15
 */
16
class Knab extends Engine
17
{
18
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';
19
20
    /**
21
     * @inheritdoc
22
     */
23
    protected function parseStatementBank()
24
    {
25
        return 'KNAB';
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    protected function parseStatementStartPrice()
32
    {
33
        return $this->parseStatementPrice('60M');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    protected function parseStatementEndPrice()
40
    {
41
        return $this->parseStatementPrice('62M');
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    protected function parseStatementStartTimestamp()
48
    {
49
        return $this->parseTimestampFromStatement('60M');
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    protected function parseStatementEndTimestamp()
56
    {
57
        return $this->parseTimestampFromStatement('62M');
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    protected function parseTransactionAccount()
64
    {
65
        $results = [];
66
67
        // SEPA MT940 Structured
68
        if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
69
            && !empty($results[1])
70
        ) {
71
            return $this->sanitizeAccount($results[1]);
72
        }
73
74
75
        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
76
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
77
            && !empty($results['account'])
78
        ) {
79
            return $results['account'];
80
        }
81
82
        return '';
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    protected function parseTransactionAccountName()
89
    {
90
        $results = [];
91
92
        // SEPA MT940 Structured
93
        if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
94
            && !empty($results[1])
95
        ) {
96
            $accountName = trim($results[1]);
97
            if (!empty($accountName)) {
98
                return $this->sanitizeAccountName($accountName);
99
            }
100
        }
101
102
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
103
            && !empty($results[1])
104
        ) {
105
            return trim($results[1]);
106
        }
107
        if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
108
            && !empty($results[1])
109
        ) {
110
            return trim($results[1]);
111
        }
112
113
        return '';
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    protected function parseTransactionDescription()
120
    {
121
        $description = parent::parseTransactionDescription();
122
123
        // SEPA MT940 Structured
124
        if (strpos($description, '/REMI/') !== false
125
            && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
126
        ) {
127
            return $results[1];
128
        }
129
130
        $accountIsInDescription = strpos($description, 'REK:');
131
        if ($accountIsInDescription !== false) {
132
            return trim(substr($description, 0, $accountIsInDescription));
133
        }
134
135
        $name = $this->parseTransactionAccountName();
136
        if ($name === '') {
137
            return $description;
138
        }
139
        $accountNameIsInDescription = strpos($description, $name);
140
        if ($accountNameIsInDescription !== false) {
141
            return trim(substr($description, 0, $accountNameIsInDescription - 6));
142
        }
143
        return $description;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public static function isApplicable($string)
150
    {
151
        $firstline = strtok($string, "\r\n\t");
152
        return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false;
153
    }
154
155
    protected function parseTransactionType()
156
    {
157
        $code = $this->parseTransactionCode();
158
        switch ($code) {
159
            case 541:
160
            case 544:
161
            case 547:
162
                $result = TransactionType::get(TransactionType::SEPA_TRANSFER);
163
                break;
164
            case 64:
165
                $result = TransactionType::get(TransactionType::SEPA_DIRECTDEBIT);
166
                break;
167
            case 93:
168
                $result = TransactionType::get(TransactionType::BANK_COSTS);
169
                break;
170
            case 13:
171
            case 30:
172
                $result = TransactionType::get(TransactionType::PAYMENT_TERMINAL);
173
                break;
174
            case "MSC":
175
                $result = TransactionType::get(TransactionType::BANK_INTEREST);
176
                break;
177
            case "TRF":
178
                $result = TransactionType::get(TransactionType::UNKNOWN);
179
                break;
180
            default:
181
                var_dump($code);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($code) looks like debug code. Are you sure you do not want to remove it?
Loading history...
182
                var_dump($this->getCurrentTransactionData()); die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
183
                throw new \RuntimeException("Don't know code $code for RABOBANK");
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
184
        }
185
186
        return $result;
187
    }
188
189
}
190