IbanSanitizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace fieldwork\sanitizers;
4
5
use fieldwork\sanitizers;
6
//use GuzzleHttp\Client;
7
8
class IbanSanitizer extends sanitizers\FieldSanitizer
9
{
10
11
    const ENDPOINT = 'http://opendata.siteworkers.nl/openiban?bban=';
12
13
    private $openIbanUsername,
14
        $openIbanPassword;
15
16
    function __construct ($openIbanUsername = null, $openIbanPassword = null)
17
    {
18
        $this->openIbanUsername = $openIbanUsername;
19
        $this->openIbanPassword = $openIbanPassword;
20
    }
21
22
    /**
23
     * @param $bban
24
     * @return string
25
     */
26
    private static function getRequestEndpoint ($bban)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
27
    {
28
        return self::ENDPOINT . $bban;
29
    }
30
31
    public function describeObject ()
32
    {
33
        return 'iban';
34
    }
35
36
    public function isLive ()
37
    {
38
        return true;
39
    }
40
41
    public function isRealtime ()
42
    {
43
        return false;
44
    }
45
46
    public function sanitize ($value)
47
    {
48
        $value = preg_replace('/\s/', '', $value);
49
        if (preg_match('/^[0-9]{1,10}$/', $value) && $this->openIbanUsername !== null && $this->openIbanPassword !== null) {
50
            $convertedValue = self::convertUsingOpenIban($value, $this->openIbanUsername, $this->openIbanPassword);
51
            if ($convertedValue !== null)
52
                $value = $convertedValue;
53
        }
54
        $parts = str_split($value, 4);
55
        return implode(' ', $parts);
56
    }
57
58
    /**
59
     * Converst BBAN number into IBAN number using the openiban API
60
     *
61
     * @param string $bban
62
     * @param string $username
63
     * @param string $password
64
     *
65
     * @return string|null
66
     */
67
    private static function convertUsingOpenIban ($bban, $username, $password)
0 ignored issues
show
Unused Code introduced by
The parameter $bban is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $username is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
//        try {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
//            $client      = new Client();
71
//            $res         = $client->request('GET', self::getRequestEndpoint($bban), [
72
//                'auth' => [$username, $password]
73
//            ]);
74
//            $apiResponse = json_decode($res->getBody());
75
//            return $apiResponse === null ? null : $apiResponse['iban'];
76
//        } catch (\Exception $e) {
77
            return null;
78
//        }
79
    }
80
81
}