Completed
Push — master ( 2e9ff7...454a32 )
by Alexis
07:25
created

User::getRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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