Completed
Push — master ( ef6d1c...a819d1 )
by
unknown
12s queued 10s
created

Kbs::parseTransactionDescription()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 14
c 1
b 0
f 1
nc 5
nop 0
dl 0
loc 25
rs 8.8333
1
<?php
2
3
namespace Kingsquare\Parser\Banking\Mt940\Engine;
4
5
use Kingsquare\Banking\Transaction\Type;
6
use Kingsquare\Parser\Banking\Mt940\Engine;
7
8
/**
9
 * Kbs parser for Kingsquare mt940 package.
10
 *
11
 * @package Kingsquare\Parser\Banking\Mt940\Engine
12
 * @author Sevan Nerse ([email protected])
13
 * @license http://opensource.org/licenses/MIT MIT
14
 */
15
class Kbs extends Engine
16
{
17
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected function parseStatementBank()
23
    {
24
        return 'KBS';
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    protected function parseTransactionAccount()
31
    {
32
        $results = [];
33
34
        // SEPA MT940 Structured
35
        if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
36
            && !empty($results[1])
37
        ) {
38
            return $this->sanitizeAccount($results[1]);
39
        }
40
41
        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
42
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
43
            && !empty($results['account'])
44
        ) {
45
            return $results['account'];
46
        }
47
48
        return '';
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    protected function parseTransactionAccountName()
55
    {
56
        $results = [];
57
58
        // SEPA MT940 Structured
59
        if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
60
            && !empty($results[1])
61
        ) {
62
            $accountName = trim($results[1]);
63
            if (!empty($accountName)) {
64
                return $this->sanitizeAccountName($accountName);
65
            }
66
        }
67
68
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
69
            && !empty($results[1])
70
        ) {
71
            return trim($results[1]);
72
        }
73
        if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
74
            && !empty($results[1])
75
        ) {
76
            return trim($results[1]);
77
        }
78
79
        return '';
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    protected function parseTransactionDescription()
86
    {
87
        $description = parent::parseTransactionDescription();
88
89
        // SEPA MT940 Structured
90
        if (strpos($description, '/REMI/') !== false
91
            && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
92
        ) {
93
            return $results[1];
94
        }
95
96
        $accountIsInDescription = strpos($description, 'REK:');
97
        if ($accountIsInDescription !== false) {
98
            return trim(substr($description, 0, $accountIsInDescription));
99
        }
100
101
        $name = $this->parseTransactionAccountName();
102
        if ($name === '') {
103
            return $description;
104
        }
105
        $accountNameIsInDescription = strpos($description, $name);
106
        if ($accountNameIsInDescription !== false) {
107
            return trim(substr($description, 0, $accountNameIsInDescription - 6));
108
        }
109
        return $description;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public static function isApplicable($string)
116
    {
117
        $firstline = strtok($string, "\r\n\t");
118
119
        return strpos($firstline, 'F01KOBSMK2XAXXX1201000000') !== false;
120
    }
121
122
    /**
123
     * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
124
     * @return int
125
     */
126
    protected function parseTransactionType()
127
    {
128
        static $map = [
129
            541 => Type::SEPA_TRANSFER,
130
            544 => Type::SEPA_TRANSFER,
131
            547 => Type::SEPA_TRANSFER,
132
            64 => Type::SEPA_DIRECTDEBIT,
133
            93 => Type::BANK_COSTS,
134
            13 => Type::PAYMENT_TERMINAL,
135
            30 => Type::PAYMENT_TERMINAL,
136
            'MSC' => Type::BANK_INTEREST,
137
            'TRF' => Type::UNKNOWN,
138
        ];
139
140
        $code = $this->parseTransactionCode();
141
        if (array_key_exists($code, $map)) {
142
            return $map[$code];
143
        }
144
        throw new \RuntimeException("Don't know code $code for this bank");
145
    }
146
}
147