BasicUser::getAuthId()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\Auth\User;
6
7
use AllowDynamicProperties;
8
use Jasny\Auth\ContextInterface as Context;
9
use Jasny\Auth\UserInterface;
10
11
/**
12
 * A simple user class which can be used instead of creating a custom user class.
13
 */
14
#[AllowDynamicProperties]
15
final class BasicUser implements UserInterface
16
{
17
    public string|int $id;
18
    protected string $hashedPassword = '';
19
    public string|int $role;
20
21
    /**
22
     * @inheritDoc
23
     */
24 1
    public function getAuthId(): string
25
    {
26 1
        return (string)$this->id;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 1
    public function verifyPassword(string $password): bool
33
    {
34 1
        return password_verify($password, $this->hashedPassword);
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40 1
    public function getAuthChecksum(): string
41
    {
42 1
        return hash('sha256', $this->id . $this->hashedPassword);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 1
    public function getAuthRole(?Context $context = null): string|int
49
    {
50 1
        return $this->role;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 1
    public function requiresMfa(): bool
57
    {
58 1
        return false;
59
    }
60
61
    /**
62
     * Factory method; create object from data loaded from DB.
63
     *
64
     * @param array<string,mixed> $data
65
     * @return self
66
     */
67 2
    public static function fromData(array $data): self
68
    {
69 2
        $user = new self();
70
71 2
        foreach ($data as $key => $value) {
72 2
            $user->{$key} = $value;
73
        }
74
75 2
        return $user;
76
    }
77
}
78