Password   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 49
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 7 2
A needsRehash() 0 7 2
A hash() 0 4 1
1
<?php
2
3
namespace Ds\Authenticate;
4
5
/**
6
 * Class Password
7
 * @package Ds\Authenticate
8
 */
9
class Password implements PasswordInterface
10
{
11
12
    /**
13
     * Verify Password hash.
14
     *
15
     * @param  string $password
16
     * @param  string $hash
17
     * @return bool
18
     * @throws \Exception
19
     */
20 23
    public function verify($password, $hash)
21
    {
22 23
        if (\password_verify($password, $hash)) {
23 13
            return true;
24
        }
25 10
        throw new \Exception('Verify failed. Non matching hash');
26
    }
27
28
    /**
29
     * Check if provided password needs rehashing with new Algorithm.
30
     *
31
     * @param  string $password
32
     * @param  string $hash
33
     * @param  int    $algo
34
     * @param  int    $cost
35
     * @return bool|string
36
     */
37 2
    public function needsRehash($password, $hash, $algo = PASSWORD_DEFAULT, $cost = 12)
38
    {
39 2
        if (\password_needs_rehash($hash, $algo, ['cost' => $cost])) {
40 1
            return $this->hash($password, $algo, $cost);
41
        }
42 1
        return false;
43
    }
44
45
    /**
46
     * Create hash from string.
47
     *
48
     * @param  string $password
49
     * @param  int    $algo
50
     * @param  int    $cost
51
     * @return string
52
     */
53 8
    public function hash($password, $algo = PASSWORD_DEFAULT, $cost = 12)
54
    {
55 8
        return \password_hash($password, $algo, ['cost' => $cost]);
56
    }
57
}
58