Completed
Push — master ( 734e62...750475 )
by Mārtiņš
03:48
created

StandardIdentity::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Palladium\Entity;
4
5
use RuntimeException;
6
7
class StandardIdentity extends Identity
8
{
9
10
    const HASH_ALGO = PASSWORD_BCRYPT;
11
    const HASH_COST = 12;
12
13
    private $identifier;
14
    private $password;
15
    private $hash;
16
17
    protected $type = Identity::TYPE_EMAIL;
18
19
20 2
    public function setIdentifier(string $identifier)
21
    {
22 2
        $this->identifier = strtolower($identifier);
23 2
    }
24
25
26
    /**
27
     * @codeCoverageIgnore
28
     */
29
    public function getIdentifier()
30
    {
31
        return $this->identifier;
32
    }
33
34
35 1
    public function getFingerprint(): string
36
    {
37 1
        return hash('sha384', $this->identifier);
38
    }
39
40
41 2
    public function setPassword($password, $cost = self::HASH_COST)
42
    {
43 2
        $this->password = (string) $password;
44 2
        $this->hash = $this->createHash($password, $cost);
45 2
    }
46
47
48 1
    public function rehashPassword($cost = self::HASH_COST)
49
    {
50 1
        $this->hash = $this->createHash($this->password, $cost);
51 1
    }
52
53
    /**
54
     * @codeCoverageIgnore
55
     */
56
    public function getHash()
57
    {
58
        return $this->hash;
59
    }
60
61
62 2
    private function createHash($password, int $cost): string
63
    {
64 2
        return password_hash($password, self::HASH_ALGO, ['cost' => $cost]);
65
    }
66
67
68 4
    public function setHash($hash)
69
    {
70 4
        if (null === $hash) {
71 1
            $this->hash = null;
72 1
            return;
73
        }
74 4
        $this->hash = (string) $hash;
75 4
    }
76
77
78 1
    public function matchPassword($password): bool
79
    {
80 1
        return password_verify($password, $this->hash);
81
    }
82
83
84 1
    public function hasOldHash($cost = self::HASH_COST): bool
85
    {
86 1
        return password_needs_rehash($this->hash, self::HASH_ALGO, ['cost' => $cost]);
87
    }
88
}
89