Completed
Pull Request — master (#26)
by Valentyn
02:41
created

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