1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Users\Entity; |
5
|
|
|
|
6
|
|
|
use App\Users\Entity\User; |
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @ORM\Entity(repositoryClass="App\Users\Repository\ConfirmationTokenRepository") |
12
|
|
|
* @ORM\Table(name="users_confirmation_tokens") |
13
|
|
|
* @UniqueEntity(fields="token", message="This token already taken") |
14
|
|
|
*/ |
15
|
|
|
class ConfirmationToken |
16
|
|
|
{ |
17
|
|
|
const TYPE_CONFIRM_EMAIl = 'confirm_email'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @ORM\Id() |
21
|
|
|
* @ORM\GeneratedValue() |
22
|
|
|
* @ORM\Column(type="integer") |
23
|
|
|
*/ |
24
|
|
|
private $id; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var $user User |
28
|
|
|
* @ORM\ManyToOne(targetEntity="App\Users\Entity\User") |
29
|
|
|
*/ |
30
|
|
|
private $user; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @ORM\Column(type="string", length=32, unique=true) |
34
|
|
|
*/ |
35
|
|
|
private $token; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @ORM\Column(type="string", length=256) |
39
|
|
|
*/ |
40
|
|
|
private $type; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
44
|
|
|
*/ |
45
|
|
|
private $expires_at; |
46
|
|
|
|
47
|
|
|
public static $validTypes = [self::TYPE_CONFIRM_EMAIl]; |
48
|
|
|
|
49
|
5 |
|
public function __construct(User $user, $type) |
50
|
|
|
{ |
51
|
5 |
|
if (in_array($type, self::$validTypes) === false) { |
52
|
|
|
throw new \InvalidArgumentException(sprintf('$type should be valid type! Instead %s given', $type)); |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
$this->type = $type; |
56
|
5 |
|
$this->token = bin2hex(openssl_random_pseudo_bytes(16)); |
57
|
5 |
|
$this->user = $user; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \DateTimeInterface $expires_at |
62
|
|
|
* @return ConfirmationToken |
63
|
|
|
*/ |
64
|
5 |
|
public function setExpiresAt(\DateTimeInterface $expires_at) |
65
|
|
|
{ |
66
|
5 |
|
$this->expires_at = $expires_at; |
67
|
5 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function getId() |
74
|
|
|
{ |
75
|
|
|
return $this->id; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return User |
80
|
|
|
*/ |
81
|
1 |
|
public function getUser(): User |
82
|
|
|
{ |
83
|
1 |
|
return $this->user; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
5 |
|
public function getToken() |
90
|
|
|
{ |
91
|
5 |
|
return $this->token; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function getType() |
98
|
|
|
{ |
99
|
|
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function getExpiresAt() |
106
|
|
|
{ |
107
|
|
|
return $this->expires_at; |
108
|
|
|
} |
109
|
|
|
} |