StandardIdentity::matchPassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Palladium\Entity;
4
5
class StandardIdentity extends Identity
6
{
7
8
    const HASH_ALGO = PASSWORD_BCRYPT;
9
    const HASH_COST = 12;
10
11
    private $identifier;
12
    private $password;
13
    private $hash;
14
15
    protected $type = Identity::TYPE_STANDARD;
16
17
18 18
    public function setIdentifier(string $identifier)
19
    {
20 18
        $this->identifier = strtolower($identifier);
21 18
    }
22
23
24
    /**
25
     * @codeCoverageIgnore
26
     */
27
    public function getIdentifier()
28
    {
29
        return $this->identifier;
30
    }
31
32
33 16
    public function getFingerprint(): string
34
    {
35 16
        return hash('sha384', $this->identifier);
36
    }
37
38
39 13
    public function setPassword(string $password, int $cost = null)
40
    {
41 13
        $this->password = $password;
42 13
        if ($cost) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cost of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
43 5
            $this->hash = $this->createHash($password, $cost);
44
        }
45 13
    }
46
47
48 2
    public function rehashPassword(int $cost = StandardIdentity::HASH_COST)
49
    {
50 2
        $this->hash = $this->createHash($this->password, $cost);
51 2
    }
52
53
    /**
54
     * @codeCoverageIgnore
55
     */
56
    public function getHash()
57
    {
58
        return $this->hash;
59
    }
60
61
62 7
    private function createHash(string $password, int $cost): string
63
    {
64 7
        return password_hash($password, StandardIdentity::HASH_ALGO, ['cost' => $cost]);
65
    }
66
67
68 17
    public function setHash(string $hash = null)
69
    {
70 17
        $this->hash = $hash;
71 17
    }
72
73
74 10
    public function matchPassword(string $password): bool
75
    {
76 10
        return password_verify($password, $this->hash);
77
    }
78
79
80 9
    public function hasOldHash(int $cost = StandardIdentity::HASH_COST): bool
81
    {
82 9
        return password_needs_rehash($this->hash, StandardIdentity::HASH_ALGO, ['cost' => $cost]);
83
    }
84
85 1
    public function isVerified(): bool
86
    {
87 1
        return $this->getStatus() === Identity::STATUS_ACTIVE;
88
    }
89
}
90