Completed
Push — master ( e32077...7e7359 )
by Tõnis
02:36
created

Random::generatePassword()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 48
nop 2
1
<?php
2
3
namespace andmemasin\helpers;
4
5
use Ramsey\Uuid\Uuid;
6
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
7
8
class Random
9
{
10
    const DEFAULT_PASSWORD_LENGTH = 9;
11
12
13
    public static function generatePassword($length=9, $strength=0) {
14
        $vowels = 'aeuy';
15
        $consonants = 'bdghjmnpqrstvz';
16
        if ($strength & 1) {
17
            $consonants .= 'BDGHJLMNPQRSTVWXZ';
18
        }
19
        if ($strength & 2) {
20
            $vowels .= "AEUY";
21
        }
22
        if ($strength & 4) {
23
            $consonants .= '23456789';
24
        }
25
        if ($strength & 8) {
26
            $consonants .= '@#$%';
27
        }
28
29
        $password = '';
30
        $alt = time() % 2;
31
        for ($i = 0; $i < $length; $i++) {
32
            if ($alt == 1) {
33
                $password .= $consonants[(rand() % strlen($consonants))];
34
                $alt = 0;
35
            } else {
36
                $password .= $vowels[(rand() % strlen($vowels))];
37
                $alt = 1;
38
            }
39
        }
40
        return $password;
41
    }
42
43
    /**
44
     * get an uuid V4
45
     * @return string
46
     */
47
    public static function getUuidV4(){
48
        $uuid = Uuid::uuid4();
49
        return $uuid->toString();
50
    }
51
52
}