Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

UserTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 188
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 17

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreatedAt() 0 3 1
A getSalt() 0 2 1
A getUsername() 0 3 1
A setPlainPassword() 0 5 1
A getId() 0 3 1
A getPlainPassword() 0 3 1
A setRoles() 0 5 1
A setPassword() 0 5 1
A hasRole() 0 3 1
A updatedTimestamps() 0 5 1
A getEmail() 0 3 1
A setEmail() 0 5 1
A setCreatedAt() 0 5 1
A eraseCredentials() 0 3 1
A __toString() 0 3 1
A getPassword() 0 3 1
A getRoles() 0 7 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Symfony\Component\Validator\Constraints as Assert;
7
8
trait UserTrait
9
{
10
    /**
11
     * @ORM\Column(type="datetime", nullable=true)
12
     */
13
    protected $createdAt;
14
15
    /**
16
     * @var string
17
     * @ORM\Column(type="string", length=180, unique=true)
18
     * @Assert\Email(
19
     *     message = "user.email.invalid",
20
     *     checkMX = true
21
     * )
22
     */
23
    protected $email;
24
25
    /**
26
     * Loaded From BaseUser.
27
     *
28
     * @Assert\Length(
29
     *     min=7,
30
     *     max=100,
31
     *     minMessage="user.password.short"
32
     * )
33
     */
34
    protected $plainPassword;
35
36
    /**
37
     * @ORM\Column(type="json")
38
     */
39
    private $roles = [];
40
41
    /**
42
     * @var string The hashed password
43
     * @ORM\Column(type="string")
44
     */
45
    private $password;
46
47
    public function __toString()
48
    {
49
        return $this->email;
50
    }
51
52
    public function getId(): ?int
53
    {
54
        return $this->id;
55
    }
56
57
    public function getEmail(): ?string
58
    {
59
        return $this->email;
60
    }
61
62
    public function setEmail(string $email): self
63
    {
64
        $this->email = $email;
65
66
        return $this;
67
    }
68
69
    public function setPlainPassword($password)
70
    {
71
        $this->plainPassword = $password;
72
73
        return $this;
74
    }
75
76
    public function getPlainPassword()
77
    {
78
        return $this->plainPassword;
79
    }
80
81
    /**
82
     * A visual identifier that represents this user.
83
     *
84
     * @see UserInterface
85
     */
86
    public function getUsername(): string
87
    {
88
        return (string) $this->email;
89
    }
90
91
    /**
92
     * @see UserInterface
93
     */
94
    public function getRoles(): array
95
    {
96
        $roles = $this->roles;
97
        // guarantee every user at least has ROLE_USER
98
        $roles[] = static::ROLE_DEFAULT;
1 ignored issue
show
Bug introduced by
The constant PiedWeb\CMSBundle\Entity\UserTrait::ROLE_DEFAULT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
100
        return array_unique($roles);
101
    }
102
103
    public function setRoles(array $roles): self
104
    {
105
        $this->roles = $roles;
106
107
        return $this;
108
    }
109
110
    public function hasRole($role)
111
    {
112
        return in_array(strtoupper($role), $this->getRoles(), true);
113
    }
114
115
    /**
116
        public function __construct()
117
    {
118
        $this->roles = [];
119
    }
120
    public function addRole($role)
121
    {
122
        $role = strtoupper($role);
123
        if ($role === static::ROLE_DEFAULT) {
124
            return $this;
125
        }
126
127
        if (!in_array($role, $this->roles, true)) {
128
            $this->roles[] = $role;
129
        }
130
131
        return $this;
132
    }
133
134
135
    public function removeRole($role)
136
    {
137
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
138
            unset($this->roles[$key]);
139
            $this->roles = array_values($this->roles);
140
        }
141
142
        return $this;
143
    }/**/
144
145
    /**
146
     * @see UserInterface
147
     */
148
    public function getPassword(): string
149
    {
150
        return (string) $this->password;
151
    }
152
153
    public function setPassword(string $password): self
154
    {
155
        $this->password = $password;
156
157
        return $this;
158
    }
159
160
    /**
161
     * @see UserInterface
162
     */
163
    public function getSalt()
164
    {
165
        // not needed when using the "bcrypt" algorithm in security.yaml
166
    }
167
168
    /**
169
     * @see UserInterface
170
     */
171
    public function eraseCredentials()
172
    {
173
        $this->plainPassword = null;
174
    }
175
176
    /**
177
     * @ORM\PrePersist
178
     */
179
    public function updatedTimestamps(): self
180
    {
181
        $this->setCreatedAt(new \DateTime('now'));
182
183
        return $this;
184
    }
185
186
    public function getCreatedAt(): ?\DateTimeInterface
187
    {
188
        return $this->createdAt;
189
    }
190
191
    public function setCreatedAt(\DateTimeInterface $createdAt): self
192
    {
193
        $this->createdAt = $createdAt;
194
195
        return $this;
196
    }
197
}
198