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

PronounceablePasswordGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B generatePassword() 0 28 5
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