Completed
Branch master (7a9b43)
by Vitaliy
01:57
created

Length::setMax()   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 $min;
13
14
    /**
15
     * @var int
16
     */
17
    protected $max;
18
19
    /**
20
     * @param int      $min
21
     * @param int|null $max
22
     */
23 9
    public function __construct ( $min = 8, $max = null )
24
    {
25 9
        $this->setMin( $min );
26 8
        $this->setMax( $max === null ? $min : $max );
27 7
    }
28
29
    /**
30
     * @param int $min
31
     *
32
     * @return bool
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 9
    protected function setMin ( $min )
37
    {
38 9
        if ( $min > 0 && $min <= 100 ) {
39 8
            $this->min = (int) $min;
40 8
            return true;
41
        }
42 1
        throw new InvalidArgumentException( 'The min length should be from 1 to 100' );
43
    }
44
45
    /**
46
     * @param int $max
47
     *
48
     * @return bool
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 8
    protected function setMax ( $max )
53
    {
54 8
        if ( $max > 0 && $max <= 100 ) {
55 7
            $this->max = (int) $max;
56 7
            return true;
57
        }
58 1
        throw new InvalidArgumentException( 'The max 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->min <= $this->max ) {
69 4
            return mt_rand( $this->min, $this->max );
70
        }
71 1
        throw new InvalidArgumentException( 'The min length should be less than the max' );
72
    }
73
}
74