Completed
Push — master ( 6f429a...4083cc )
by Alexis
06:02
created

User::setConfirmationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace AWurth\SilexUser\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
7
use Symfony\Component\Validator\Constraints\Email;
8
use Symfony\Component\Validator\Constraints\Length;
9
use Symfony\Component\Validator\Constraints\Regex;
10
use Symfony\Component\Validator\Mapping\ClassMetadata;
11
12
/**
13
 * Base User class.
14
 *
15
 * @author Alexis Wurth <[email protected]>
16
 *
17
 * @ORM\MappedSuperclass
18
 */
19
abstract class User implements UserInterface
20
{
21
    /**
22
     * @var int
23
     *
24
     * @ORM\Id
25
     * @ORM\GeneratedValue
26
     * @ORM\Column(name="id", type="integer")
27
     */
28
    protected $id;
29
30
    /**
31
     * @var string
32
     *
33
     * @ORM\Column(name="username", type="string", length=30, unique=true)
34
     */
35
    protected $username;
36
37
    /**
38
     * @var string
39
     *
40
     * @ORM\Column(name="email", type="string", length=255, unique=true)
41
     */
42
    protected $email;
43
44
    /**
45
     * @var string
46
     *
47
     * @ORM\Column(name="password", type="string", length=255)
48
     */
49
    protected $password;
50
51
    /**
52
     * @var string
53
     */
54
    protected $plainPassword;
55
56
    /**
57
     * @var string
58
     *
59
     * @ORM\Column(name="salt", type="string", nullable=true)
60
     */
61
    protected $salt;
62
63
    /**
64
     * @var bool
65
     *
66
     * @ORM\Column(name="enabled", type="boolean")
67
     */
68
    protected $enabled = false;
69
70
    /**
71
     * @var array
72
     *
73
     * @ORM\Column(name="roles", type="array")
74
     */
75
    protected $roles = [];
76
77
    /**
78
     * @var string
79
     *
80
     * @ORM\Column(name="confirmation_token", type="string", unique=true, nullable=true)
81
     */
82
    protected $confirmationToken;
83
84
    public static function loadValidatorMetadata(ClassMetadata $metadata)
85
    {
86
        $metadata->addConstraint(new UniqueEntity([
87
            'fields' => 'username',
88
            'message' => 'This username is already taken.'
89
        ]));
90
        $metadata->addConstraint(new UniqueEntity([
91
            'fields' => 'email',
92
            'message' => 'This email address is already used.'
93
        ]));
94
95
        $metadata->addPropertyConstraint('username', new Length(['min' => 3, 'max' => 20]));
96
        $metadata->addPropertyConstraint('username', new Regex(['pattern' => '/^[a-z0-9._-]+$/i']));
97
        $metadata->addPropertyConstraint('email', new Email());
98
        $metadata->addPropertyConstraint('password', new Length(['min' => 8, 'max' => 32]));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function isAccountNonExpired()
105
    {
106
        return true;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function isAccountNonLocked()
113
    {
114
        return true;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function isCredentialsNonExpired()
121
    {
122
        return true;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function eraseCredentials()
129
    {
130
        $this->plainPassword = null;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function isEnabled()
137
    {
138
        return $this->enabled;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function setEnabled($enabled)
145
    {
146
        $this->enabled = (bool) $enabled;
147
148
        return $this;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getId()
155
    {
156
        return $this->id;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function setUsername($username)
163
    {
164
        $this->username = $username;
165
166
        return $this;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getUsername()
173
    {
174
        return $this->username;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function setEmail($email)
181
    {
182
        $this->email = $email;
183
184
        return $this;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function getEmail()
191
    {
192
        return $this->email;
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function setPassword($password)
199
    {
200
        $this->password = $password;
201
202
        return $this;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function getPassword()
209
    {
210
        return $this->password;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function setPlainPassword($password)
217
    {
218
        $this->plainPassword = $password;
219
220
        return $this;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function getPlainPassword()
227
    {
228
        return $this->plainPassword;
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    public function setSalt($salt)
235
    {
236
        $this->salt = $salt;
237
238
        return $this;
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function getSalt()
245
    {
246
        return $this->salt;
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function setRoles(array $roles)
253
    {
254
        $this->roles = $roles;
255
256
        return $this;
257
    }
258
259
    /**
260
     * {@inheritdoc}
261
     */
262
    public function getRoles()
263
    {
264
        $roles = $this->roles;
265
266
        $roles[] = 'ROLE_USER';
267
268
        return array_unique($roles);
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274
    public function setConfirmationToken($token)
275
    {
276
        $this->confirmationToken = $token;
277
278
        return $this;
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284
    public function getConfirmationToken()
285
    {
286
        return $this->confirmationToken;
287
    }
288
}
289