Completed
Pull Request — master (#21)
by Valentyn
01:42
created

User::getProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Entity;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
9
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use JMS\Serializer\Annotation\Exclude;
12
use JMS\Serializer\Annotation\Expose;
13
14
/**
15
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
16
 * @ORM\Table(name="users")
17
 * @UniqueEntity(fields="email", message="Email already taken")
18
 * @UniqueEntity(fields="username", message="Username already taken")
19
 */
20
class User implements UserInterface, \Serializable
21
{
22
    const ROLE_USER = 'ROLE_USER';
23
    const ROLE_ADMIN = 'ROLE_ADMIN';
24
25
    /**
26
     * @ORM\Id()
27
     * @ORM\GeneratedValue()
28
     * @ORM\Column(type="integer")
29
     */
30
    private $id;
31
32
    #private $tokens;
33
34
    /**
35
     * @var $profile UserProfile
36
     * @ORM\OneToOne(targetEntity="App\Entity\UserProfile", cascade={"persist", "remove"})
37
     */
38
    private $profile;
39
40
    /**
41
     * @ORM\Column(type="string", length=255, unique=true)
42
     */
43
    public $email;
44
45
    /**
46
     * @ORM\Column(type="integer", length=1, options={"default" : 0})
47
     */
48
    private $isEmailConfirmed = 0;
49
50
    /**
51
     * @ORM\Column(type="string", length=255, unique=true)
52
     */
53
    public $username;
54
55
    /**
56
     * @ORM\Column(type="string", length=255, nullable=true)
57
     */
58
    private $roles;
59
60
    /**
61
     * @Exclude
62
     */
63
    private $plainPassword;
64
65
    /**
66
     * @Exclude
67
     * @ORM\Column(type="string", length=64)
68
     */
69
    private $password;
70
71 31
    public function __construct()
72
    {
73 31
        $this->addRole(self::ROLE_USER);
74 31
        $this->profile = new UserProfile($this);
75 31
    }
76
77 8
    public function getProfile(): UserProfile
78
    {
79 8
        return $this->profile;
80
    }
81
82 4
    public function getId(): ?int
83
    {
84 4
        return $this->id;
85
    }
86
87 31
    public function addRole(string $role): self
88
    {
89 31
        if (array_search($role, $this->getRoles()) === false) {
90 3
            return $this->setRoles(
91 3
                array_merge($this->getRoles(), [$role])
92
            );
93
        }
94
95 31
        return $this;
96
    }
97
98 1
    public function removeRole(string $role): self
99
    {
100 1
        $roles = $this->getRoles();
101 1
        $foundedRoleKey = array_search($role, $roles);
102
103 1
        if ($foundedRoleKey !== false) {
104 1
            unset($roles[$foundedRoleKey]);
105 1
            return $this->setRoles($roles);
106
        }
107
108
        return $this;
109
    }
110
111 3
    private function setRoles(array $roles): self
112
    {
113 3
        if (!count($roles)) {
114 1
            $this->roles = null;
115 1
            return $this;
116
        }
117
118 3
        $this->roles = json_encode($roles);
119
120 3
        return $this;
121
    }
122
123 35
    public function getRoles(): array
124
    {
125 35
        if (!$this->roles) {
126 33
            return $this->getDefaultRoles();
127
        }
128
129 5
        $roles = (array)json_decode($this->roles);
130
131 5
        if (!count($roles)) {
132
            return $this->getDefaultRoles();
133
        }
134
135 5
        return array_values($roles);
136
    }
137
138 33
    private function getDefaultRoles()
139
    {
140 33
        return [self::ROLE_USER];
141
    }
142
143 5
    public function setPlainPassword(string $plainPassword): self
144
    {
145 5
        if (!empty($plainPassword)) {
146 5
            $this->plainPassword = $plainPassword;
147
            // Change some mapped values so preUpdate will get called.
148 5
            $this->password = ''; // just blank it out
149
        }
150
151 5
        return $this;
152
    }
153
154 7
    public function getPlainPassword(): string
155
    {
156 7
        return (string)$this->plainPassword;
157
    }
158
159 9
    public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self
160
    {
161 9
        $this->password = $passwordEncoder->encodePassword($this, $password);
162
163 9
        return $this;
164
    }
165
166 6
    public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool
167
    {
168 6
        return $passwordEncoder->isPasswordValid($this, $password);
169
    }
170
171 4
    public function getPassword(): ?string
172
    {
173 4
        return $this->password;
174
    }
175
176 11
    public function getSalt(): ?string
177
    {
178 11
        return null;
179
    }
180
181 7
    public function getUsername(): ?string
182
    {
183 7
        return $this->username;
184
    }
185
186 10
    public function eraseCredentials(): self
187
    {
188 10
        $this->plainPassword = null;
189
190 10
        return $this;
191
    }
192
193 1
    public function serialize(): string
194
    {
195 1
        return serialize([
196 1
            $this->id,
197 1
            $this->email,
198 1
            $this->username,
199 1
            $this->roles,
200
        ]);
201
    }
202
203 1
    public function unserialize($serialized): self
204
    {
205
        list (
206 1
            $this->id,
207 1
            $this->email,
208 1
            $this->username,
209 1
            $this->roles,
210 1
            ) = unserialize($serialized);
211
212 1
        return $this;
213
    }
214
215 1
    public function confirmEmail()
216
    {
217 1
        $this->isEmailConfirmed = 1;
218
219 1
        return $this;
220
    }
221
222
    public function changeEmail($email)
223
    {
224
        $this->email = $email;
225
        $this->isEmailConfirmed = 0;
226
227
        return $this;
228
    }
229
}