|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alexander Torosh <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Domain\User; |
|
7
|
|
|
|
|
8
|
|
|
use Core\Domain\DomainModel; |
|
9
|
|
|
use Domain\User\Exceptions\UserException; |
|
10
|
|
|
use Domain\User\Specifications\UserPasswordSpecification; |
|
11
|
|
|
use Domain\User\Specifications\UserSpecification; |
|
12
|
|
|
use Domain\User\ValueObjects\UserRoles; |
|
13
|
|
|
|
|
14
|
|
|
class User extends DomainModel |
|
15
|
|
|
{ |
|
16
|
|
|
private $id = 0; |
|
17
|
|
|
private $email = ''; |
|
18
|
|
|
private $name = ''; |
|
19
|
|
|
private $role = UserRoles::DEFAULT_ROLE; |
|
20
|
|
|
private $password = ''; |
|
21
|
|
|
private $password_hash = ''; |
|
22
|
|
|
|
|
23
|
|
|
private $created_at; |
|
24
|
|
|
private $updated_at; |
|
25
|
|
|
|
|
26
|
|
|
public function initialize() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setSource('users'); |
|
29
|
|
|
|
|
30
|
|
|
$this->skipAttributesOnCreate(['id', 'password', 'created_at', 'updated_at']); |
|
31
|
|
|
$this->skipAttributesOnUpdate(['password']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function beforeUpdate() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->updated_at = date('Y-m-d H:i:s'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function buildPasswordHash(): string |
|
40
|
|
|
{ |
|
41
|
|
|
$passwordSpecification = new UserPasswordSpecification($this->password); |
|
42
|
|
|
$passwordSpecification->validate(); |
|
43
|
|
|
|
|
44
|
|
|
return $this->generatePasswordHash(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws DomainException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function doesPasswordMatch(string $inputPassword): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return password_verify($inputPassword, $this->password_hash); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setId(int $id): User |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->id > 0) { |
|
58
|
|
|
throw new UserException('Identifier `id` is already defined.'); |
|
59
|
|
|
} |
|
60
|
|
|
$this->id = $id; |
|
61
|
|
|
|
|
62
|
|
|
$specification = new UserSpecification($this); |
|
63
|
|
|
$specification->validateIdentifier(); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getId(): int |
|
69
|
|
|
{ |
|
70
|
|
|
return (int) $this->id; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setName(string $name): User |
|
74
|
|
|
{ |
|
75
|
|
|
$this->name = $name; |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getName(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->name; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setEmail(string $email): User |
|
86
|
|
|
{ |
|
87
|
|
|
$this->email = $email; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getEmail(): string |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->email; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function setPassword(string $password): User |
|
98
|
|
|
{ |
|
99
|
|
|
$this->password = $password; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function passwordDefined(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->password ? true : false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function revealPassword(): string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->password; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getRole(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->role; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function setRole($role): User |
|
120
|
|
|
{ |
|
121
|
|
|
$this->role = $role; |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function generatePasswordHash(): string |
|
127
|
|
|
{ |
|
128
|
|
|
return password_hash($this->password, PASSWORD_ARGON2I); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|