Completed
Branch v2.0.0 (e47d62)
by Alexander
03:40
created

User::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Domain\User;
7
8
use Domain\User\Exceptions\UserException;
9
use Domain\User\Specifications\UserPasswordSpecification;
10
use Domain\User\Specifications\UserSpecification;
11
12
class User
13
{
14
    private $userID = 0;
15
    private $email = '';
16
    private $name = '';
17
    private $password = '';
18
19
    public function buildPasswordHash(): string
20
    {
21
        $passwordSpecification = new UserPasswordSpecification($this->password);
22
        $passwordSpecification->validate();
23
24
        return $this->generatePasswordHash();
25
    }
26
27
    /**
28
     * @throws DomainException
29
     */
30
    public function doesPasswordMatch(int $userID, string $inputPassword): bool
31
    {
32
        $user = $this->repository->fetchUser($userID);
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
34
        return password_verify($inputPassword, $user->password_hash);
35
    }
36
37
    public function setUserId(int $userID): User
38
    {
39
        if ($this->userID > 0) {
40
            throw new UserException('Identifier `userID` is already defined.');
41
        }
42
        $this->userID = $userID;
43
44
        $specification = new UserSpecification($this);
45
        $specification->validateIdentifier();
46
47
        return $this;
48
    }
49
50
    public function setPassword(string $password): User
51
    {
52
        $this->password = $password;
53
54
        return $this;
55
    }
56
57
    public function setName(string $name): User
58
    {
59
        $this->name = $name;
60
61
        return $this;
62
    }
63
64
    public function setEmail(string $email): User
65
    {
66
        $this->email = $email;
67
68
        return $this;
69
    }
70
71
    public function getUserID(): int
72
    {
73
        return $this->userID;
74
    }
75
76
    public function getEmail(): string
77
    {
78
        return $this->email;
79
    }
80
81
    public function getName(): string
82
    {
83
        return $this->name;
84
    }
85
86
    public function passwordDefined(): bool
87
    {
88
        return $this->password ? true : false;
89
    }
90
91
    private function generatePasswordHash(): string
92
    {
93
        return password_hash($this->password, PASSWORD_ARGON2I, [
94
            'salt' => $this->generateSalt(),
95
        ]);
96
    }
97
98
    private function generateSalt(): string
99
    {
100
        $length = random_int(16, 32);
101
102
        return substr(md5($this->email.'_'.microtime()), 0, $length);
103
    }
104
}
105