Completed
Push — master ( 4639e9...e17532 )
by Mārtiņš
02:47
created

EmailIdentity::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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