Passed
Push — master ( d2d827...f0512d )
by John
04:16
created

Password::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PasswordHelper;
6
7
/**
8
 * Class Password
9
 * @package PasswordHelper
10
 * @codeCoverageIgnore
11
 */
12
class Password
13
{
14
    /**
15
     * @var StrengthChecker
16
     */
17
    protected $checker;
18
19
    /**
20
     * @var Generator
21
     */
22
    protected $generator;
23
24
    /**
25
     * @var Policy
26
     */
27
    private $policy;
28
29
    /**
30
     * @var Validator
31
     */
32
    protected $validator;
33
34
    public function __construct(array $config = [])
35
    {
36
        $this->policy = new Policy($config);
37
        $this->checker = new StrengthChecker();
38
        $this->validator = new Validator($this->policy);
39
        $this->generator = new Generator($this->policy, $this->validator);
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function __debugInfo(): array
46
    {
47
        return [
48
            'minimumDigits' => $this->policy->getMinimumDigits(),
49
            'minimumLowercase' => $this->policy->getMinimumLowercase(),
50
            'minimumSpecialChars' => $this->policy->getMinimumSpecialChars(),
51
            'minimumUppercase' => $this->policy->getMinimumUppercase(),
52
            'minimumLetters' => $this->policy->getMinimumLetters(),
53
            'minimumLength' => $this->policy->getMinimumLength(),
54
        ];
55
    }
56
57
    /**
58
     * Generates a password that satisfies the password policy
59
     *
60
     * @return string
61
     *
62
     * @throws \Exception
63
     */
64
    public function generate(): string
65
    {
66
        return $this->generator->generatePassword();
67
    }
68
69
    /**
70
     * Validates that a password satisfies the password policy
71
     *
72
     * @param string $password
73
     *
74
     * @return bool
75
     */
76
    public function validateComplexity(string $password): bool
77
    {
78
        return $this->validator->isValidPassword($password);
79
    }
80
81
    /**
82
     * Determines the strength of a given password
83
     *
84
     * @param string $password
85
     *
86
     * @return string
87
     */
88
    public function checkStrength(string $password): string
89
    {
90
        return $this->checker->checkStrength($password);
91
    }
92
93
    /**
94
     * Creates a password hash.
95
     *
96
     * @see https://secure.php.net/manual/en/function.password-hash.php
97
     *
98
     * @param string $password The user's password.
99
100
     * @return string
101
     */
102
    public function hash(string $password): string
103
    {
104
        return password_hash($password, PASSWORD_DEFAULT);
0 ignored issues
show
Bug Best Practice introduced by
The expression return password_hash($pa...elper\PASSWORD_DEFAULT) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
105
    }
106
107
    /**
108
     * Checks if the given hash matches the given options.
109
     *
110
     * @see https://secure.php.net/manual/en/function.password-verify.php
111
     *
112
     * @param string $password The user's password.
113
     * @param string $hash A hash created by password_hash().
114
     *
115
     * @return bool Returns TRUE if the password and hash match, or FALSE otherwise.
116
     */
117
    public function verify(string $password, string $hash): bool
118
    {
119
        return password_verify($password, $hash);
120
    }
121
122
    /**
123
     * Returns information about the given hash
124
     *
125
     * @see https://secure.php.net/manual/en/function.password-get-info.php
126
     *
127
     * @param string $hash A hash created by password_hash()
128
     *
129
     * @return array Returns an associative array with three elements:
130
     *     - algo, which will match a password algorithm constant
131
     *     - algoName, which has the human readable name of the algorithm
132
     *     - options which includes the options provided when calling password_hash()
133
     */
134
    public function getInfo(string $hash): array
135
    {
136
        return password_get_info($hash);
0 ignored issues
show
Bug Best Practice introduced by
The expression return password_get_info($hash) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
137
    }
138
139
    /**
140
     * Checks if the given hash matches the given options.
141
     *
142
     * @see https://secure.php.net/manual/en/function.password-needs-rehash.php
143
     *
144
     * @param string $hash A hash created by password_hash().
145
     *
146
     * @return bool Returns TRUE if the hash should be rehashed to match the given algo and options, or FALSE otherwise.
147
     */
148
    public function checkForRehash(string $hash): bool
149
    {
150
        return password_needs_rehash($hash, PASSWORD_DEFAULT);
151
    }
152
}