Completed
Push — master ( ba6b04...ce04f3 )
by Vitaliy
05:28 queued 01:29
created

Length::setMinimum()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3
1
<?php
2
3
namespace iiifx\PasswordGenerator;
4
5
use InvalidArgumentException;
6
7
class Length
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $minimum;
13
14
    /**
15
     * @var int
16
     */
17
    protected $maximum;
18
19
    /**
20
     * @param int      $minimum
21
     * @param int|null $maximum
22
     */
23 9
    public function __construct ( $minimum = 8, $maximum = null )
24
    {
25 9
        $this->setMinimum( $minimum );
26 8
        $this->setMaximum( $maximum === null ? $minimum : $maximum );
27 7
    }
28
29
    /**
30
     * @param int $minimum
31
     *
32
     * @return bool
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 9
    protected function setMinimum ( $minimum )
37
    {
38 9
        if ( $minimum > 0 && $minimum <= 100 ) {
39 8
            $this->minimum = (int) $minimum;
40 8
            return true;
41
        }
42 1
        throw new InvalidArgumentException( 'The minimum length should be from 1 to 100' );
43
    }
44
45
    /**
46
     * @param int $maximum
47
     *
48
     * @return bool
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 8
    protected function setMaximum ( $maximum )
53
    {
54 8
        if ( $maximum > 0 && $maximum <= 100 ) {
55 7
            $this->maximum = (int) $maximum;
56 7
            return true;
57
        }
58 1
        throw new InvalidArgumentException( 'The maximum length should be from 1 to 100' );
59
    }
60
61
    /**
62
     * @return int
63
     *
64
     * @throws InvalidArgumentException
65
     */
66 5
    public function getLength ()
67
    {
68 5
        if ( $this->minimum <= $this->maximum ) {
69 4
            return mt_rand( $this->minimum, $this->maximum );
70
        }
71 1
        throw new InvalidArgumentException( 'The minimum length should be less than the maximum' );
72
    }
73
}
74