Completed
Push — master ( 96ea8b...1276b9 )
by Alexis
08:08
created

User::setEmail()   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
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AWurth\Silex\User\Model;
13
14
use DateTime;
15
use Symfony\Component\Validator\Constraints\Email;
16
use Symfony\Component\Validator\Constraints\Length;
17
use Symfony\Component\Validator\Constraints\NotBlank;
18
use Symfony\Component\Validator\Constraints\Regex;
19
use Symfony\Component\Validator\Mapping\ClassMetadata;
20
21
/**
22
 * Base User class.
23
 *
24
 * @author Alexis Wurth <[email protected]>
25
 */
26
abstract class User implements UserInterface
27
{
28
    /**
29
     * @var int
30
     */
31
    protected $id;
32
33
    /**
34
     * @var string
35
     */
36
    protected $username;
37
38
    /**
39
     * @var string
40
     */
41
    protected $email;
42
43
    /**
44
     * @var string
45
     */
46
    protected $password;
47
48
    /**
49
     * @var string
50
     */
51
    protected $plainPassword;
52
53
    /**
54
     * @var string
55
     */
56
    protected $salt;
57
58
    /**
59
     * @var bool
60
     */
61
    protected $enabled = false;
62
63
    /**
64
     * @var DateTime
65
     */
66
    protected $lastLogin;
67
68
    /**
69
     * @var array
70
     */
71
    protected $roles = [];
72
73
    /**
74
     * @var string
75
     */
76
    protected $confirmationToken;
77
78
    /**
79
     * Used by the validator to validate the User data.
80
     *
81
     * @param ClassMetadata $metadata
82
     */
83
    public static function loadValidatorMetadata(ClassMetadata $metadata)
84
    {
85
        $metadata->addPropertyConstraint('username', new NotBlank(['message' => 'silex_user.username.blank']));
86
        $metadata->addPropertyConstraint('username', new Length([
87
            'min' => 2,
88
            'max' => 180,
89
            'minMessage' => 'silex_user.username.short',
90
            'maxMessage' => 'silex_user.username.long'
91
        ]));
92
        $metadata->addPropertyConstraint('username', new Regex([
93
            'pattern' => '/^[a-z0-9._-]+$/i',
94
            'message' => 'silex_user.username.invalid'
95
        ]));
96
97
        $metadata->addPropertyConstraint('email', new NotBlank(['message' => 'silex_user.email.blank']));
98
        $metadata->addPropertyConstraint('email', new Length([
99
            'max' => 180,
100
            'maxMessage' => 'silex_user.email.long'
101
        ]));
102
        $metadata->addPropertyConstraint('email', new Email(['message' => 'silex_user.email.invalid']));
103
104
        $metadata->addPropertyConstraint('plainPassword', new NotBlank(['message' => 'silex_user.password.blank']));
105
        $metadata->addPropertyConstraint('plainPassword', new Length([
106
            'min' => 2,
107
            'max' => 255,
108
            'minMessage' => 'silex_user.password.short',
109
            'maxMessage' => 'silex_user.password.long'
110
        ]));
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function isAccountNonExpired()
117
    {
118
        return true;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function isAccountNonLocked()
125
    {
126
        return true;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function isCredentialsNonExpired()
133
    {
134
        return true;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function eraseCredentials()
141
    {
142
        $this->plainPassword = null;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function isSuperAdmin()
149
    {
150
        return $this->hasRole(static::ROLE_SUPER_ADMIN);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function setSuperAdmin($boolean)
157
    {
158
        if (true === $boolean) {
159
            $this->addRole(static::ROLE_SUPER_ADMIN);
160
        } else {
161
            $this->removeRole(static::ROLE_SUPER_ADMIN);
162
        }
163
164
        return $this;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function isEnabled()
171
    {
172
        return $this->enabled;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function setEnabled($enabled)
179
    {
180
        $this->enabled = (bool) $enabled;
181
182
        return $this;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function getId()
189
    {
190
        return $this->id;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function setUsername($username)
197
    {
198
        $this->username = $username;
199
200
        return $this;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function getUsername()
207
    {
208
        return $this->username;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function setEmail($email)
215
    {
216
        $this->email = $email;
217
218
        return $this;
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function getEmail()
225
    {
226
        return $this->email;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function setPassword($password)
233
    {
234
        $this->password = $password;
235
236
        return $this;
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function getPassword()
243
    {
244
        return $this->password;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250
    public function setPlainPassword($password)
251
    {
252
        $this->plainPassword = $password;
253
254
        return $this;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function getPlainPassword()
261
    {
262
        return $this->plainPassword;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268
    public function setSalt($salt)
269
    {
270
        $this->salt = $salt;
271
272
        return $this;
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function getSalt()
279
    {
280
        return $this->salt;
281
    }
282
283
    /**
284
     * {@inheritdoc}
285
     */
286
    public function setLastLogin(DateTime $date = null)
287
    {
288
        $this->lastLogin = $date;
289
290
        return $this;
291
    }
292
293
    /**
294
     * {@inheritdoc}
295
     */
296
    public function getLastLogin()
297
    {
298
        return $this->lastLogin;
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     */
304
    public function setRoles(array $roles)
305
    {
306
        $this->roles = $roles;
307
308
        return $this;
309
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314
    public function getRoles()
315
    {
316
        $roles = $this->roles;
317
318
        $roles[] = self::ROLE_DEFAULT;
319
320
        return array_unique($roles);
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326
    public function hasRole($role)
327
    {
328
        return in_array(strtoupper($role), $this->getRoles(), true);
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     */
334
    public function addRole($role)
335
    {
336
        $role = strtoupper($role);
337
        if (static::ROLE_DEFAULT === $role) {
338
            return $this;
339
        }
340
341
        if (!in_array($role, $this->roles, true)) {
342
            $this->roles[] = $role;
343
        }
344
345
        return $this;
346
    }
347
348
    /**
349
     * {@inheritdoc}
350
     */
351
    public function removeRole($role)
352
    {
353
        $key = array_search(strtoupper($role), $this->roles, true);
354
        if (false !== $key) {
355
            unset($this->roles[$key]);
356
            $this->roles = array_values($this->roles);
357
        }
358
359
        return $this;
360
    }
361
362
    /**
363
     * {@inheritdoc}
364
     */
365
    public function setConfirmationToken($token)
366
    {
367
        $this->confirmationToken = $token;
368
369
        return $this;
370
    }
371
372
    /**
373
     * {@inheritdoc}
374
     */
375
    public function getConfirmationToken()
376
    {
377
        return $this->confirmationToken;
378
    }
379
}
380