Passed
Push — master ( 13644f...5aa358 )
by Wilmer
02:12
created

Password   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 0 3 1
A validate() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace terabytesoft\helpers;
4
5
use yii\base\Security;
6
7
/**
8
 * Class Password
9
 *
10
 * Helper security methods
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
33
    {
34 3
        return password_hash($password, $algo, $params);
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
44
    {
45 3
        return password_verify($password, $hash);
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;
78
    }
79
}
80