PhpGenerator::getRandomInt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

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