Symbols   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 78
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setSymbols() 0 8 3
A setPriority() 0 8 4
A getPriority() 0 4 1
A getRandomSymbol() 0 10 2
1
<?php
2
3
namespace iiifx\PasswordGenerator;
4
5
use iiifx\PasswordGenerator\Method\MethodInterface;
6
use InvalidArgumentException;
7
8
class Symbols
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $symbols;
14
15
    /**
16
     * @var int
17
     */
18
    protected $priority;
19
20
    /**
21
     * @param string $symbols
22
     * @param int    $priority
23
     */
24 8
    public function __construct ( $symbols, $priority = 100 )
25
    {
26 8
        $this->setSymbols( $symbols );
27 7
        $this->setPriority( $priority );
28 6
    }
29
30
    /**
31
     * @param $symbols
32
     *
33
     * @return bool
34
     *
35
     * @throws InvalidArgumentException
36
     */
37 8
    protected function setSymbols ( $symbols )
38
    {
39 8
        if ( is_string( $symbols ) && strlen( $symbols ) > 0 ) {
40 7
            $this->symbols = $symbols;
41 7
            return true;
42
        }
43 1
        throw new InvalidArgumentException( 'Symbols must be a string, and contain at least one character' );
44
    }
45
46
    /**
47
     * @param int $priority
48
     *
49
     * @return bool
50
     *
51
     * @throws InvalidArgumentException
52
     */
53 7
    protected function setPriority ( $priority )
54
    {
55 7
        if ( is_int( $priority ) && $priority > 0 && $priority <= 100 ) {
56 6
            $this->priority = $priority;
57 6
            return true;
58
        }
59 1
        throw new InvalidArgumentException( 'Priority must be interger and in the range from 1 to 100' );
60
    }
61
62
    /**
63
     * @return int
64
     */
65 3
    public function getPriority ()
66
    {
67 3
        return $this->priority;
68
    }
69
70
    /**
71
     * @param MethodInterface $method
72
     *
73
     * @return string
74
     */
75 2
    public function getRandomSymbol ( MethodInterface $method = null )
76
    {
77 2
        $length = strlen( $this->symbols );
78 2
        if ( $method ) {
79 2
            $position = $method->getRandomInt( $length - 1 );
80 2
        } else {
81 1
            $position = mt_rand( 0, $length - 1 );
82
        }
83 2
        return $this->symbols[ $position ];
84
    }
85
}
86