Passed
Push — Recipes ( 7632b6...2e7935 )
by Laurent
02:54
created

User::getEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Symfony\Component\Security\Core\User\UserInterface;
7
use Symfony\Component\Validator\Constraints as Assert;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
10
/**
11
 * User Entity.
12
 *
13
 * @category Entity
14
 *
15
 * @ORM\Table(name="app_user")
16
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
17
 * @UniqueEntity(
18
 *  fields={"username"},
19
 *  message="L'utilisateur que vous avez indiqué existe déjà !!"
20
 * )
21
 */
22
class User implements UserInterface
23
{
24
    const ROLE_DEFAULT = 'ROLE_USER';
25
26
    /**
27
     * @ORM\Id()
28
     * @ORM\GeneratedValue()
29
     * @ORM\Column(name="id", type="integer")
30
     */
31
    private $id;
32
33
    /**
34
     * @ORM\Column(type="string", length=180, unique=true)
35
     */
36
    private $username;
37
38
    /**
39
     * @ORM\Column(type="string", length=180, unique=true)
40
     */
41
    private $email;
42
43
    /**
44
     * @ORM\Column(type="string", length=180)
45
     * @Assert\Length(min="8", minMessage="Votre mot de passe doit faire minimum 8 caractères")
46
     */
47
    private $password;
48
49
    /**
50
     * @Assert\EqualTo(propertyPath="password", message="Vous n'avez pas typé le même mot de passe !!")
51
     */
52
    public $confirmPassword;
53
54
    /**
55
     * @ORM\Column(type="string", length=32, nullable=true)
56
     */
57
    public $salt;
58
59
    /**
60
     * @ORM\Column(name="enabled", type="boolean")
61
     */
62
    private $enabled;
63
64
    /**
65
     * @ORM\Column(name="roles", type="json")
66
     */
67
    private $roles = [];
68
69
    /**
70
     * @ORM\Column(name="admin", type="boolean")
71
     */
72
    private $admin;
73
74
    /**
75
     * @ORM\Column(name="assistant", type="boolean")
76
     */
77
    private $assistant;
78
79
    public function __construct()
80
    {
81
        $this->admin = false;
82
        $this->assistant = false;
83
        $this->enabled = true;
84
    }
85
86
    public function getId(): ?int
87
    {
88
        return $this->id;
89
    }
90
91
    /**
92
     * A visual identifier that represents this user.
93
     *
94
     * @see UserInterface
95
     */
96
    public function getUsername(): string
97
    {
98
        return (string) $this->username;
99
    }
100
101
    public function setUsername(string $username): self
102
    {
103
        $this->username = $username;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @see UserInterface
110
     */
111
    public function getPassword(): string
112
    {
113
        return (string) $this->password;
114
    }
115
116
    public function setPassword(string $password): self
117
    {
118
        $this->password = $password;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @see UserInterface
125
     */
126
    public function getRoles(): array
127
    {
128
        $roles = [];
129
        if ($this->admin === true) {
130
            $roles[] = 'ROLE_SUPER_ADMIN';
131
        }
132
        if ($this->assistant === true) {
133
            $roles[] = 'ROLE_ADMIN';
134
        }
135
        if (empty($this->roles)) {
136
            /* guarantee every user at least has ROLE_USER */
137
            $roles[] = 'ROLE_USER';
138
        }
139
140
        return array_unique($roles);
141
    }
142
143
    /**
144
     * Adds a role to the user.
145
     *
146
     * @param string $role
147
     *
148
     * @return static
149
     */
150
    public function addRoles($role)
151
    {
152
        $role = strtoupper($role);
153
        if ($role === static::ROLE_DEFAULT) {
154
            return $this;
155
        }
156
157
        if (!in_array($role, $this->roles, true)) {
158
            $this->roles[] = $role;
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * Sets the roles of the user.
166
     *
167
     * This overwrites any previous roles.
168
     *
169
     * @param array $roles
170
     */
171
    public function setRoles(array $roles = array()): self
172
    {
173
        $this->roles = [];
174
175
        if (empty($roles)) {
176
            /* guarantee every user at least has ROLE_USER */
177
            $roles[] = 'ROLE_USER';
178
        }
179
180
        foreach ($roles as $role) {
181
            $this->addRoles($role);
182
        }
183
184
        return $this;
185
    }
186
187
    /**
188
     * Get the value of admin.
189
     */
190
    public function getAdmin(): bool
191
    {
192
        return $this->admin;
193
    }
194
195
    /**
196
     * Get the value of assistant.
197
     */
198
    public function getAssistant(): bool
199
    {
200
        return $this->assistant;
201
    }
202
203
    /**
204
     * Get the value of enabled.
205
     */
206
    public function enabled(): bool
207
    {
208
        return $this->enabled;
209
    }
210
211
    /**
212
     * Set the value of admin.
213
     */
214
    public function setAdmin($admin): self
215
    {
216
        $this->admin = $admin;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Set the value of assistant.
223
     */
224
    public function setAssistant($assistant): self
225
    {
226
        $this->assistant = $assistant;
227
228
        return $this;
229
    }
230
231
    public function setEnabled($enabled)
232
    {
233
        $this->enabled = $enabled;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @see UserInterface
240
     */
241
    public function getSalt()
242
    {
243
        /* not needed when using the "bcrypt" algorithm in security.yaml */
244
        return null;
245
    }
246
247
    /**
248
     * @see UserInterface
249
     */
250
    public function eraseCredentials()
251
    {
252
        /* If you store any temporary, sensitive data on the user, clear it here
253
         $this->plainPassword = null; */
254
    }
255
256
    /**
257
     * Get the value of email
258
     */ 
259
    public function getEmail()
260
    {
261
        return $this->email;
262
    }
263
264
    /**
265
     * Set the value of email
266
     *
267
     * @return  self
268
     */ 
269
    public function setEmail($email)
270
    {
271
        $this->email = $email;
272
273
        return $this;
274
    }
275
}
276