1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\Entity; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeZone; |
9
|
|
|
use Ramsey\Uuid\Uuid; |
10
|
|
|
use Ramsey\Uuid\UuidInterface; |
11
|
|
|
use Doctrine\ORM\Mapping as ORM; |
12
|
|
|
use SixtyEightPublishers\User\Common\Exception\InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @ORM\Entity |
16
|
|
|
* @ORM\HasLifecycleCallbacks |
17
|
|
|
*/ |
18
|
|
|
class PasswordRequest implements PasswordRequestInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @ORM\Id |
22
|
|
|
* @ORM\Column(type="uuid", unique=true) |
23
|
|
|
* |
24
|
|
|
* @var \Ramsey\Uuid\UuidInterface |
25
|
|
|
*/ |
26
|
|
|
private $id; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @ORM\Column(type="string", length=15) |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $status; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @ORM\Column(type="datetime") |
37
|
|
|
* |
38
|
|
|
* @var \DateTime |
39
|
|
|
*/ |
40
|
|
|
private $created; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @ORM\Column(type="datetime") |
44
|
|
|
* |
45
|
|
|
* @var \DateTime |
46
|
|
|
*/ |
47
|
|
|
private $updated; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @ORM\ManyToOne(targetEntity="UserInterface") |
51
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
52
|
|
|
* |
53
|
|
|
* @var \SixtyEightPublishers\User\ForgotPassword\Entity\UserInterface |
54
|
|
|
*/ |
55
|
|
|
private $user; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @ORM\Embedded(class="DeviceInfo", columnPrefix="request_") |
59
|
|
|
* |
60
|
|
|
* @var \SixtyEightPublishers\User\ForgotPassword\Entity\DeviceInfo |
61
|
|
|
*/ |
62
|
|
|
private $requestDeviceInfo; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @ORM\Embedded(class="DeviceInfo", columnPrefix="reset_") |
66
|
|
|
* |
67
|
|
|
* @var \SixtyEightPublishers\User\ForgotPassword\Entity\DeviceInfo |
68
|
|
|
*/ |
69
|
|
|
private $resetDeviceInfo; |
70
|
|
|
|
71
|
|
|
/** @var string */ |
72
|
|
|
private static $expiration = self::DEFAULT_EXPIRATION; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\Entity\UserInterface $user |
76
|
|
|
* |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public function __construct(UserInterface $user) |
80
|
|
|
{ |
81
|
|
|
$this->id = Uuid::uuid4(); |
82
|
|
|
$this->user = $user; |
83
|
|
|
$this->status = self::STATUS_CREATED; |
84
|
|
|
$this->created = new DateTime('now', new DateTimeZone('UTC')); |
85
|
|
|
$this->updated = new DateTime('now', new DateTimeZone('UTC')); |
86
|
|
|
$this->requestDeviceInfo = new DeviceInfo(); |
87
|
|
|
$this->resetDeviceInfo = new DeviceInfo(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @internal |
92
|
|
|
* @ORM\PreUpdate |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
public function onPreUpdate(): void |
98
|
|
|
{ |
99
|
|
|
$this->updated = new DateTime('now'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $expiration |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public static function setExpirationString(string $expiration): void |
108
|
|
|
{ |
109
|
|
|
self::$expiration = $expiration; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \Ramsey\Uuid\UuidInterface |
114
|
|
|
*/ |
115
|
|
|
public function getId(): UuidInterface |
116
|
|
|
{ |
117
|
|
|
return $this->id; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getStatus(): string |
124
|
|
|
{ |
125
|
|
|
return $this->status; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function setStatus(string $status): void |
132
|
|
|
{ |
133
|
|
|
if (!in_array($status, self::STATUSES, TRUE)) { |
134
|
|
|
throw new InvalidArgumentException(sprintf( |
135
|
|
|
'Value %s is not in allowed set [%s]', |
136
|
|
|
$status, |
137
|
|
|
implode(', ', array_map('strval', self::STATUSES)) |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->status = $status; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function getCreated(): DateTime |
148
|
|
|
{ |
149
|
|
|
return $this->created; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function getUpdated(): DateTime |
156
|
|
|
{ |
157
|
|
|
return $this->updated; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function getUser(): UserInterface |
164
|
|
|
{ |
165
|
|
|
return $this->user; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function getExpiration(): DateTime |
172
|
|
|
{ |
173
|
|
|
return (clone $this->created)->modify(self::$expiration); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
* |
179
|
|
|
* @throws \Exception |
180
|
|
|
*/ |
181
|
|
|
public function isExpired(): bool |
182
|
|
|
{ |
183
|
|
|
return $this->getExpiration() < new DateTime('now', new DateTimeZone('UTC')); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
public function getRequestDeviceInfo(): DeviceInfo |
190
|
|
|
{ |
191
|
|
|
return $this->requestDeviceInfo; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function getResetDeviceInfo(): DeviceInfo |
198
|
|
|
{ |
199
|
|
|
return $this->resetDeviceInfo; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|