-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
195 lines (165 loc) · 4.26 KB
/
User.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace AppBundle\Entity;
use AppBundle\Service\DateTimeFactory\DateTimeFactory;
use AppBundle\Service\IdGenerator\IdGenerator;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Type as SerializerType;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\UserRepository")
*/
class User implements UserInterface
{
public const TOKEN_LENGTH = 32;
private const ROLE_USER = 'ROLE_USER';
/**
* @var string
* @ORM\Column(name="id", type="string", length=255)
* @ORM\Id
*/
protected $id;
/**
* @var string
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
* @ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* @var integer
* @ORM\Column(name="flat", type="smallint", length=5, unique=true)
*/
private $flatNumber;
/**
* @var string
* @ORM\Column(name="token", type="string", length=32, unique=true)
* @Serializer\Exclude
*/
private $token;
/**
* @var string
* @ORM\Column(name="password", type="string", length=32)
* @Serializer\Exclude
*/
private $password;
/**
* @var string
* @ORM\Column(name="ip", type="string", length=16)
* @Serializer\Exclude
*/
private $ip;
/**
* @var boolean
* @ORM\Column(name="is_confirmed", type="boolean")
*/
private $isConfirmed;
/**
* @var \DateTime
* @ORM\Column(name="registration_date", type="datetime")
*/
private $registrationDate;
/**
* @var array
* @ORM\Column(name="roles", type="simple_array", nullable=true)
* @Serializer\Exclude
*/
private $roles;
/** @var IdGenerator */
private $idGenerator;
/** @var DateTimeFactory */
private $dateTimeFactory;
/* @var Card[]|ArrayCollection
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Card", mappedBy="creator")
* @SerializerType("array")
*/
private $cards;
public function __construct(
string $email,
string $name,
int $flatNumber,
string $password,
string $userIp,
IdGenerator $idGenerator = null,
DateTimeFactory $dateTimeFactory = null
) {
$this->idGenerator = $idGenerator ?? new IdGenerator();
$this->dateTimeFactory = $dateTimeFactory ?? new DateTimeFactory();
$this->id = $idGenerator->generateUuid();
$this->token = $idGenerator->generateString(self::TOKEN_LENGTH);
$this->registrationDate = $this->dateTimeFactory->getCurrentDateAndTime();
$this->name = $name;
$this->flatNumber = $flatNumber;
$this->email = $email;
$this->password = md5($password);
$this->ip = $userIp;
$this->cards = new ArrayCollection();
$this->isConfirmed = true;
}
public function getId(): string
{
return $this->id;
}
/** {@inheritDoc} */
public function getRoles(): array
{
return array_merge(
$this->roles,
[
self::ROLE_USER,
]
);
}
public function updateToken(string $token): void
{
$this->token = $token;
}
public function getToken(): string
{
return $this->token;
}
/** {@inheritDoc} */
public function getPassword(): string
{
return $this->password;
}
/** {@inheritDoc} */
public function getSalt()
{
}
/** {@inheritDoc} */
public function eraseCredentials(): void
{
}
/** {@inheritDoc} */
public function getUsername(): string
{
return $this->getId();
}
public function getName(): string
{
return $this->name;
}
public function getEmail(): string
{
return $this->email;
}
public function getFlatNumber(): int
{
return $this->flatNumber;
}
public function confirm(): void
{
$this->isConfirmed = true;
}
public function isConfirmed(): bool
{
return $this->isConfirmed;
}
}