GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch master (2650d5)
by Xaf
02:28
created

Iban   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
lcom 2
cbo 1
dl 0
loc 247
ccs 72
cts 72
cp 1
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A countryCode() 0 4 1
A ibanCheckDigits() 0 4 1
A bankCode() 0 4 1
A branchCode() 0 4 1
A countryCheckDigits() 0 4 1
A accountNumber() 0 4 1
A __toString() 0 6 1
A validateCountryCodeFormat() 0 6 2
A validateCheckDigitsFormat() 0 6 2
A validateControlDigit() 0 11 2
A validateChecksum() 0 12 1
A digitToInt() 0 8 2
A fromBbanAndCountry() 0 12 1
A fromString() 0 22 2
A validateSupportedCountry() 0 11 2
1
<?php
2
3
namespace IbanGenerator;
4
5
use IbanGenerator\Bban\BbanInterface;
6
use IbanGenerator\Bban\SpainBban;
7
use InvalidArgumentException;
8
9
class Iban
10
{
11
    /**
12
     * @var BbanInterface
13
     */
14
    private $bban;
15
16
    /**
17
     * @var string
18
     */
19
    private $countryCode;
20
21
    /**
22
     * @var string
23
     */
24
    private $checkDigits;
25
26
    /**
27
     * @var array
28
     */
29
    private static $countriesSupported = [
30
        'ES' => SpainBban::class,
31
    ];
32
33
    /**
34
     * Iban constructor.
35
     *
36
     * @param string $countryCode
37
     * @param string $checkDigits
38
     * @param BbanInterface $bban
39
     *
40
     * @throws InvalidArgumentException
41
     */
42 16
    public function __construct($countryCode, $checkDigits, BbanInterface $bban)
43
    {
44 16
        $countryCode = strtoupper($countryCode);
45 16
        self::validateCountryCodeFormat($countryCode);
46 12
        self::validateCheckDigitsFormat($checkDigits);
47 8
        self::validateControlDigit($countryCode, $checkDigits, $bban);
48 6
        $this->countryCode = $countryCode;
49 6
        $this->checkDigits = $checkDigits;
50 6
        $this->bban = $bban;
51 6
    }
52
53
    /**
54
     * @param string $iban
55
     *
56
     * @throws InvalidArgumentException
57
     *
58
     * @return static
59
     */
60 6
    public static function fromString($iban)
61
    {
62 6
        $iban = preg_replace('/[^0-9a-zA-Z]+/', '', $iban);
63
64 6
        if (! preg_match('/^[0-9a-zA-Z]{16,34}$/', $iban)) {
65 3
            throw new InvalidArgumentException('Iban should be between 16 and 34 characters');
66
        }
67
68 3
        $countryCode = strtoupper(substr($iban, 0, 2));
69 3
        $checkDigits = strtoupper(substr($iban, 2, 2));
70 3
        $bbanString = strtoupper(substr($iban, 4));
71
72 3
        self::validateSupportedCountry($countryCode);
73 2
        $bbanClass = self::$countriesSupported[$countryCode];
74
75
        /**
76
         * @var BbanInterface
77
         */
78 2
        $bban = $bbanClass::fromString($bbanString);
79
80 2
        return new static($countryCode, $checkDigits, $bban);
81
    }
82
83
    /**
84
     * @param BbanInterface $bban
85
     * @param string $countryCode
86
     *
87
     * @throws InvalidArgumentException
88
     *
89
     * @return static
90
     */
91 2
    public static function fromBbanAndCountry(BbanInterface $bban, $countryCode)
92
    {
93 2
        self::validateCountryCodeFormat($countryCode);
94 2
        self::validateCountryCodeFormat($countryCode);
95 2
        self::validateSupportedCountry($countryCode);
96
97 2
        $checksum = self::validateChecksum($countryCode, '00', $bban);
98 2
        $checkDigit = 98 - (int) $checksum;
99 2
        $checkDigit = str_pad($checkDigit, 2, 0, STR_PAD_LEFT);
100
101 2
        return new static($countryCode, $checkDigit, $bban);
102
    }
103
104
    /**
105
     * @return string
106
     */
107 2
    public function countryCode()
108
    {
109 2
        return $this->countryCode;
110
    }
111
112
    /**
113
     * @return string
114
     */
115 4
    public function ibanCheckDigits()
116
    {
117 4
        return $this->checkDigits;
118
    }
119
120
    /**
121
     * @return string
122
     */
123 2
    public function bankCode()
124
    {
125 2
        return $this->bban->bankCode();
126
    }
127
128
    /**
129
     * @return string
130
     */
131 2
    public function branchCode()
132
    {
133 2
        return $this->bban->branchCode();
134
    }
135
136
    /**
137
     * @return string
138
     */
139 2
    public function countryCheckDigits()
140
    {
141 2
        return $this->bban->checkDigits();
142
    }
143
144
    /**
145
     * @return string
146
     */
147 2
    public function accountNumber()
148
    {
149 2
        return $this->bban->accountNumber();
150
    }
151
152
    /**
153
     * @return string
154
     */
155 2
    public function __toString()
156
    {
157 2
        $bbanString = $this->bban;
158
159 2
        return $this->countryCode . $this->checkDigits . $bbanString;
160
    }
161
162
    /**
163
     * @param $countryCode
164
     *
165
     * @throws InvalidArgumentException
166
     */
167 16
    private static function validateCountryCodeFormat($countryCode)
168
    {
169 16
        if (! preg_match('/^[A-Z]{2}$/', $countryCode)) {
170 4
            throw new InvalidArgumentException('The country code should be 2 letters');
171
        }
172 12
    }
173
174
    /**
175
     * @param $checkDigits
176
     *
177
     * @throws InvalidArgumentException
178
     */
179 12
    private static function validateCheckDigitsFormat($checkDigits)
180
    {
181 12
        if (! preg_match('/^[\d]{2}$/', $checkDigits)) {
182 4
            throw new InvalidArgumentException('The IBAN checksum must be 2 numeric characters');
183
        }
184 8
    }
185
186
    /**
187
     * @param string $countryCode
188
     * @param string $checkDigits
189
     * @param BbanInterface $bban
190
     *
191
     * @throws InvalidArgumentException
192
     */
193 8
    private static function validateControlDigit(
194
        $countryCode,
195
        $checkDigits,
196
        BbanInterface $bban
197
    ) {
198 8
        $checksum = self::validateChecksum($countryCode, $checkDigits, $bban);
199
200 8
        if ($checksum !== '01') {
201 2
            throw new InvalidArgumentException('The IBAN checksum digits are not valid');
202
        }
203 6
    }
204
205
    /**
206
     * @param $countryCode
207
     *
208
     * @throws InvalidArgumentException
209
     */
210 5
    private static function validateSupportedCountry($countryCode)
211
    {
212 5
        if (!array_key_exists($countryCode, self::$countriesSupported)) {
213 1
            throw new InvalidArgumentException(
214
                sprintf(
215 1
                    'The country code %s is not supported',
216
                    $countryCode
217
                )
218
            );
219
        }
220 4
    }
221
222
    /**
223
     * @param $countryCode
224
     * @param $checkDigits
225
     * @param BbanInterface $bban
226
     *
227
     * @return string
228
     */
229 8
    private static function validateChecksum($countryCode, $checkDigits, BbanInterface $bban)
230
    {
231 8
        $rearranged = (string) $bban . $countryCode . $checkDigits;
232 8
        $digitsList = str_split($rearranged);
233
234 8
        $digitsList = array_map(['self', 'digitToInt'], $digitsList);
235 8
        $stringToCompute = implode('', $digitsList);
236
237 8
        $checksum = bcmod($stringToCompute, '97');
238
239 8
        return str_pad($checksum, 2, 0, STR_PAD_LEFT);
240
    }
241
242
    /**
243
     * @param string $value
244
     *
245
     * @return int
246
     */
247 8
    private static function digitToInt($value)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
248
    {
249 8
        if (is_numeric($value)) {
250 8
            return (int) $value;
251
        }
252
253 8
        return ord($value) - 55;
254
    }
255
}
256