Completed
Push — master ( cef25e...758dfd )
by Hilmi Erdem
02:56
created

NumericTokenGenerator::getPlainText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Erdemkeren\TemporaryAccess\Token\TokenGenerator;
4
5
use Exception;
6
7
class NumericTokenGenerator extends AbstractTokenGenerator implements TokenGeneratorInterface
8
{
9
    protected function getPlainText($length)
10
    {
11
        $range = $this->generateRangeForLength($length);
12
13
        try {
14
            $int = random_int($range[0], $range[1]);
15
        } catch (Exception $e) {
16
            $int = rand($range[0], $range[1]);
17
        }
18
19
        return (string) $int;
20
    }
21
22
    protected function generateRangeForLength($length)
23
    {
24
        $min = 1;
25
        $max = 9;
26
27
        while ($length > 1) {
28
            $min .= 0;
29
            $max .= 9;
30
31
            $length--;
32
        }
33
34
        return [
35
            $min, $max,
36
        ];
37
    }
38
}
39