TINValid::getMessageForStatusCode()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 17
rs 8.8333
1
<?php
2
3
namespace LeKoala\Tin;
4
5
use InvalidArgumentException;
6
use LeKoala\Tin\Algo\TINAlgorithmInterface;
7
use LeKoala\Tin\Exception\TINValidationException;
8
use LeKoala\Tin\Exception\InvalidCountryException;
9
10
/**
11
 * The main class to validate TIN numbers
12
 */
13
class TINValid
14
{
15
    /**
16
     * Check if a tin is valid
17
     *
18
     * If you need the detail about why the check failed, use validateTIN
19
     *
20
     * @param string $countryCode Alpha-2 iso code
21
     * @param string $tin
22
     * @return boolean
23
     */
24
    public static function checkTIN(string $countryCode, string $tin)
25
    {
26
        try {
27
            self::validateTIN($countryCode, $tin);
28
            return true;
29
        } catch (TINValidationException $ex) {
30
            return false;
31
        }
32
    }
33
34
    /**
35
     * Throw an exception if number is not valid
36
     *
37
     * @throws TINValidationException
38
     * @param string $countryCode Alpha-2 iso code
39
     * @param string $tin
40
     * @return void
41
     */
42
    public static function validateTIN(string $countryCode, string $tin)
43
    {
44
        $inst = self::getAlgoForCountry($countryCode);
45
        $statusCode = $inst->isValid($tin);
46
47
        if ($statusCode !== 0) {
48
            $message = self::getMessageForStatusCode($statusCode);
49
            throw new TINValidationException($message, $statusCode);
50
        }
51
    }
52
53
    /**
54
     * Check if country is supported
55
     *
56
     * @param string $countryCode
57
     * @return boolean
58
     */
59
    public static function isCountrySupported(string $countryCode)
60
    {
61
        try {
62
            self::getAlgoForCountry($countryCode);
63
            return true;
64
        } catch (InvalidCountryException $ex) {
65
            return false;
66
        }
67
    }
68
69
    /**
70
     * @param integer $statusCode
71
     * @return string
72
     */
73
    protected static function getMessageForStatusCode($statusCode)
74
    {
75
        switch ($statusCode) {
76
            case 0:
77
                return "Valid";
78
            case -1:
79
                return "No Information";
80
            case 2:
81
                return "No Syntax Checker";
82
            case 1:
83
                return "Invalid Syntax";
84
            case 4:
85
                return "Invalid Length";
86
            case 3:
87
                return "Invalid Pattern";
88
            default:
89
                return "Default";
90
        }
91
    }
92
93
    /**
94
     * @param string $countryCode
95
     * @return TINAlgorithmInterface
96
     */
97
    protected static function getAlgoForCountry(string $countryCode)
98
    {
99
        if (strlen($countryCode) != 2) {
100
            throw new InvalidArgumentException("Country code should be 2 chars long.");
101
        }
102
        $class = "LeKoala\\Tin\\Algo\\" . strtoupper($countryCode) . "Algorithm";
103
        if (!class_exists($class)) {
104
            throw new InvalidCountryException("Algorithm '$class' was not found.");
105
        }
106
        return new $class;
107
    }
108
}
109