Repository navigation
Expand file tree
/
Copy pathResettablePassword.php
More file actions
71 lines (55 loc) · 1.64 KB
/
Copy pathResettablePassword.php
File metadata and controls
71 lines (55 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace MsgPhp\User\Model;
use MsgPhp\User\Event\Domain\CancelPasswordRequest;
use MsgPhp\User\Event\Domain\FinishPasswordRequest;
use MsgPhp\User\Event\Domain\RequestPassword;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
trait ResettablePassword
{
/** @var null|string */
private $passwordResetToken;
/** @var null|\DateTimeInterface */
private $passwordRequestedAt;
public function getPasswordResetToken(): ?string
{
return $this->passwordResetToken;
}
public function getPasswordRequestedAt(): ?\DateTimeInterface
{
return $this->passwordRequestedAt;
}
public function requestPassword(?string $token = null): void
{
$this->passwordResetToken = $token ?? bin2hex(random_bytes(32));
$this->passwordRequestedAt = new \DateTimeImmutable();
}
public function abortPasswordRequest(): void
{
$this->passwordResetToken = null;
$this->passwordRequestedAt = null;
}
private function onRequestPasswordEvent(RequestPassword $event): bool
{
$this->requestPassword($event->token);
return true;
}
private function onCancelPasswordRequestEvent(CancelPasswordRequest $event): bool
{
if (null === $this->passwordRequestedAt) {
return false;
}
$this->abortPasswordRequest();
return true;
}
private function onFinishPasswordRequestEvent(FinishPasswordRequest $event): bool
{
if (null === $this->passwordRequestedAt) {
return false;
}
$this->abortPasswordRequest();
return true;
}
}