PasswordGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 5 1
A defaultCharacters() 0 4 1
1
<?php
2
namespace Nubs\PwMan;
3
4
use RandomLib\Factory as RandomFactory;
5
6
/**
7
 * Generate random passwords.
8
 */
9
class PasswordGenerator
10
{
11
    /** @type string The characters to use in the password. */
12
    private $_characters;
13
14
    /** @type int The length of the password. */
15
    private $_length;
16
17
    /**
18
     * Construct the password generator with the desired settings.
19
     *
20
     * @api
21
     * @param string $characters The characters to use in the password.
22
     * @param int $length The length of the password to generate.
23
     */
24
    public function __construct($characters, $length = 32)
25
    {
26
        $this->_characters = $characters;
27
        $this->_length = $length;
28
    }
29
30
    /**
31
     * Generate a password.
32
     *
33
     * @api
34
     * @return string The random password.
35
     */
36
    public function __invoke()
37
    {
38
        $generator = (new RandomFactory())->getMediumStrengthGenerator();
39
        return $generator->generateString($this->_length, $this->_characters);
40
    }
41
42
    /**
43
     * Returns the default characters used by the generator.
44
     *
45
     * This is set to all of the printable ASCII characters.
46
     *
47
     * @return string The default characters used.
48
     */
49
    public static function defaultCharacters()
50
    {
51
        return join(range(chr(32), chr(126)));
52
    }
53
}
54