Completed
Push — master ( 9159fd...a2f733 )
by Oliver
08:55
created

PhpGenerator::getRandomInt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 3
cts 6
cp 0.5
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.5
1
<?php
2
3
namespace TQ\Shamir\Random;
4
5
use RuntimeException;
6
7
/**
8
 * Class PhpGenerator
9
 *
10
 * @package TQ\Shamir\Random
11
 */
12
class PhpGenerator implements Generator
13
{
14
    /**
15
     * The maximum random number
16
     *
17
     * @var int
18
     */
19
    protected $max = PHP_INT_MAX;
20
21
    /**
22
     * The minimum random number
23
     *
24
     * @var int
25
     */
26
    protected $min = 1;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param  int  $max  The maximum random number
32
     * @param  int  $min  The minimum random number (must be positive)
33
     */
34 69
    public function __construct($max = PHP_INT_MAX, $min = 1)
35
    {
36 69
        if((int)$min < 1) {
37 3
            throw new \OutOfRangeException('The min number must be a positive integer.');
38
        }
39
40 66
        $this->min = (int)$min;
41 66
        $this->max = (int)$max;
42 66
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 20
    public function getRandomInt()
48
    {
49
        try {
50 20
            $random = random_int($this->min, $this->max);
51
        } catch (\Exception $e) {
52
            throw new RuntimeException(
53
                'Random number generator algorithm failed.', 0, $e
54
            );
55
        }
56
57 20
        return $random;
58
    }
59
}
60