Completed
Push — development ( 1fb984...7ea9e3 )
by Nils
08:29
created

PronounceablePasswordGenerator::generatePassword()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 3
nop 0
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace PasswordGenerator\Generator;
4
5
/**
6
 * Pronounceable passwords generator
7
 */
8
class PronounceablePasswordGenerator extends ComputerPasswordGenerator
9
{
10
    const PARAMETER_VOWELS = 'VOWELS';
11
12
    public function __construct()
13
    {
14
        parent::__construct();
15
        $this->setParameter(self::PARAMETER_VOWELS, 'aeiou');
16
    }
17
18
    public function generatePassword()
19
    {
20
        $vowels = $this->getParameter(self::PARAMETER_VOWELS, '');
21
22
        if (!strlen($vowels)) {
23
            return parent::generatePassword();
24
        }
25
26
        $characterList = $this->getCharacterList()->getCharacters();
27
        $charactersCount = strlen($characterList);
28
        $password = '';
29
30
        $length = $this->getLength();
31
        $isLastCharVowel = null;
32
33
        for ($i = 0; $i < $length; ++$i) {
34
            do {
35
                $char = $characterList[$this->randomInteger(0, $charactersCount - 1)];
36
                $isVowel = false !== stripos($vowels, $char);
37
38
            } while (null !== $isLastCharVowel && $isVowel === $isLastCharVowel);
39
40
            $password .= $char;
41
            $isLastCharVowel = $isVowel;
42
        }
43
44
        return $password;
45
    }
46
}
47