Passed
Pull Request — master (#93)
by
unknown
01:56
created

Wise   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 31
c 2
b 0
f 1
dl 0
loc 100
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isApplicable() 0 5 1
A parseTransactionCode() 0 9 3
A parseStatementAccount() 0 17 5
A parseTransactionType() 0 14 2
A parseStatementBank() 0 3 1
A parseTransactionPrice() 0 10 3
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
class Wise extends Engine
9
{
10
    /**
11
     *
12
     * {@inheritdoc}
13
     * @see \Kingsquare\Parser\Banking\Mt940\Engine::parseStatementBank()
14
     */
15
    protected function parseStatementBank()
16
    {
17
        return 'WISE';
18
    }
19
20
    /**
21
     * uses field 25 to gather accoutnumber.
22
     *
23
     * @return string accountnumber
24
     */
25
    protected function parseStatementAccount()
26
    {
27
        $results = [];
28
        if (preg_match('/:25:([\d\.]+)*/', $this->getCurrentStatementData(), $results)
29
            && !empty($results[1])
30
        ) {
31
            return $this->sanitizeAccount($results[1]);
32
        }
33
34
        // SEPA / IBAN
35
        if (preg_match('/:25:([A-Z0-9]{8}[\d\.]+)*/', $this->getCurrentStatementData(), $results)
36
            && !empty($results[1])
37
        ) {
38
            return $this->sanitizeAccount($results[1]);
39
        }
40
41
        return 'WISE';
42
    }
43
44
45
    /**
46
     * uses the 61 field to determine amount/value of the transaction.
47
     *
48
     * @return float
49
     */
50
    protected function parseTransactionPrice()
51
    {
52
        $results = [];
53
        if (preg_match('/^:61:.*?[CD]([\d,\.]+)F/i', $this->getCurrentTransactionData(), $results)
54
            && !empty($results[1])
55
        ) {
56
            return $this->sanitizePrice($results[1]);
57
        }
58
59
        return 0;
60
    }
61
62
    /**
63
     * uses the 61 field to get the bank specific transaction code.
64
     *
65
     * @return string
66
     */
67
    protected function parseTransactionCode()
68
    {
69
        $results = [];
70
        if (preg_match('/^:61:.*?F(.{3}).*/', $this->getCurrentTransactionData(), $results)
71
            && !empty($results[1])
72
        ) {
73
            return trim($results[1]);
74
        }
75
        return '';
76
    }
77
    
78
    /**
79
    * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
80
    * @return int
81
    */
82
   protected function parseTransactionType()
83
   {
84
       static $map = [
85
            'CHG' => Type::BANK_COSTS,
86
            'MSC' => Type::BANK_INTEREST,
87
            'TRF' => Type::TRANSFER,
88
            'FEX' => Type::UNKNOWN,
89
       ];
90
91
       $code = $this->parseTransactionCode();
92
       if (array_key_exists($code, $map)) {
93
           return $map[$code];
94
       }
95
       throw new \RuntimeException("Don't know code $code for this bank");
96
   }
97
98
    /**
99
     *
100
     * {@inheritdoc}
101
     * @see \Kingsquare\Parser\Banking\Mt940\Engine::isApplicable()
102
     */
103
    public static function isApplicable($string)
104
    {
105
        $firstline = strtok($string, "\r\n\t");
106
107
        return strpos($firstline, 'F01TRWIGB2LAXXX0000000000') !== false;
108
    }
109
}
110