PhpGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
eloc 13
c 5
b 1
f 3
dl 0
loc 44
ccs 9
cts 12
cp 0.75
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRandomInt() 0 11 2
A __construct() 0 8 2
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