Total Complexity | 6 |
Total Lines | 66 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | **/ |
||
12 | class Password |
||
13 | { |
||
14 | /** |
||
15 | * @var Security $security |
||
16 | **/ |
||
17 | protected $security; |
||
18 | |||
19 | /** |
||
20 | * __construct |
||
21 | **/ |
||
22 | 3 | public function __construct() |
|
23 | { |
||
24 | 3 | $this->security = new Security(); |
|
25 | 3 | } |
|
26 | |||
27 | /** |
||
28 | * hash |
||
29 | * |
||
30 | * Wrapper for yii security helper method |
||
31 | **/ |
||
32 | 3 | public function hash(string $password, int $algo, array $params = []): string |
|
35 | } |
||
36 | |||
37 | |||
38 | /** |
||
39 | * validate |
||
40 | * |
||
41 | * wrapper for yii security helper method |
||
42 | **/ |
||
43 | 3 | public function validate(string $password, string $hash): bool |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * generate |
||
50 | * |
||
51 | * generates user-friendly random password containing at least one lower case letter, one uppercase letter and one |
||
52 | * digit. The remaining characters in the password are chosen at random from those three sets |
||
53 | * |
||
54 | * @see https://gist.github.com/tylerhall/521810 |
||
55 | **/ |
||
56 | 3 | public function generate(int $length): string |
|
57 | { |
||
58 | $sets = [ |
||
59 | 3 | 'abcdefghjkmnpqrstuvwxyz', |
|
60 | 'ABCDEFGHJKMNPQRSTUVWXYZ', |
||
61 | '23456789', |
||
62 | ]; |
||
63 | 3 | $all = ''; |
|
64 | 3 | $password = ''; |
|
65 | 3 | foreach ($sets as $set) { |
|
66 | 3 | $password .= $set[array_rand(str_split($set))]; |
|
67 | 3 | $all .= $set; |
|
68 | } |
||
69 | |||
70 | 3 | $all = str_split($all); |
|
71 | 3 | for ($i = 0; $i < $length - count($sets); $i++) { |
|
72 | 3 | $password .= $all[array_rand($all)]; |
|
73 | } |
||
74 | |||
75 | 3 | $password = str_shuffle($password); |
|
76 | |||
77 | 3 | return $password; |
|
80 |