|
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); |
|
|
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: