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

StandardIdentity   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 82
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setIdentifier() 0 4 1
A getIdentifier() 0 4 1
A getFingerprint() 0 4 1
A setPassword() 0 5 1
A rehashPassword() 0 4 1
A getHash() 0 4 1
A createHash() 0 4 1
A setHash() 0 8 2
A matchPassword() 0 4 1
A hasOldHash() 0 4 1
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