Completed
Push — master ( d91ea6...c19eb7 )
by Alexis
01:32
created

User::loadValidatorMetadata()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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