Completed
Push — master ( 2eb8ac...4b6668 )
by Mārtiņš
02:21
created

EmailIdentity::getFingerprint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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