Completed
Push — master ( 50cc67...a8ce2f )
by Valentyn
03:46
created

User::isPasswordValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Users\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use Symfony\Component\Serializer\Annotation\Groups;
12
13
/**
14
 * @ORM\Entity(repositoryClass="App\Users\Repository\UserRepository")
15
 * @ORM\Table(name="users")
16
 * @UniqueEntity(fields="email", repositoryMethod="getUsersByCriteria", message="Email already taken")
17
 * @UniqueEntity(fields="username", repositoryMethod="getUsersByCriteria", message="Username already taken")
18
 */
19
class User implements UserInterface
20
{
21
    /**
22
     * @ORM\Id()
23
     * @ORM\GeneratedValue()
24
     * @ORM\Column(type="integer")
25
     * @Groups({"list", "view"})
26
     */
27
    private $id;
28
29
    /**
30
     * @var UserProfile
31
     * @ORM\OneToOne(targetEntity="App\Users\Entity\UserProfile", cascade={"persist", "remove"})
32
     * @Groups({"list", "view"})
33
     */
34
    private $profile;
35
36
    /**
37
     * @Groups({"ROLE_ADMIN", "ROLE_MODER"})
38
     * @ORM\Column(type="string", length=255, unique=true)
39
     */
40
    private $email;
41
42
    /**
43
     * @Groups({"ROLE_ADMIN", "ROLE_MODER"})
44
     * @ORM\Column(type="integer", length=1, options={"default": 0})
45
     */
46
    private $isEmailConfirmed = 0;
47
48
    /**
49
     * @ORM\Column(type="string", length=255, unique=true)
50
     * @Groups({"list", "view"})
51
     */
52
    private $username;
53
54
    /**
55
     * @Groups({"ROLE_ADMIN", "ROLE_MODER"})
56
     * @ORM\Embedded(class="App\Users\Entity\UserRoles", columnPrefix=false)
57
     */
58
    private $roles;
59
60
    /**
61
     * @var string
62
     */
63
    private $plainPassword;
64
65
    /**
66
     * @ORM\Column(type="string", length=64)
67
     */
68
    private $password;
69
70 32
    public function __construct(string $email, string $username, string $password)
71
    {
72 32
        $this->roles = new UserRoles();
73 32
        $this->profile = new UserProfile();
74
75 32
        $this->email = $email;
76 32
        $this->username = $username;
77 32
        $this->setPlainPassword($password);
78 32
    }
79
80 13
    public function getProfile(): UserProfile
81
    {
82 13
        return $this->profile;
83
    }
84
85 13
    public function getId(): ?int
86
    {
87 13
        return $this->id;
88
    }
89
90 4
    public function getEmail(): string
91
    {
92 4
        return $this->email;
93
    }
94
95
    public function isEmailConfirmed(): bool
96
    {
97
        return (bool) $this->isEmailConfirmed;
98
    }
99
100 33
    public function setPlainPassword(string $plainPassword): self
101
    {
102 33
        if (!empty($plainPassword)) {
103 33
            $this->plainPassword = $plainPassword;
104
            // Change some mapped values so preUpdate will get called.
105 33
            $this->password = ''; // just blank it out
106
        }
107
108 33
        return $this;
109
    }
110
111 8
    public function getPlainPassword(): string
112
    {
113 8
        return (string) $this->plainPassword;
114
    }
115
116 10
    public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self
117
    {
118 10
        $this->plainPassword = $password;
119 10
        $this->password = $passwordEncoder->encodePassword($this, $password);
120
121 10
        return $this;
122
    }
123
124
    /**
125
     * @param $password
126
     * @param UserPasswordEncoderInterface $passwordEncoder
127
     *
128
     * @return bool
129
     */
130 1
    public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool
131
    {
132 1
        return $passwordEncoder->isPasswordValid($this, $password);
133
    }
134
135 5
    public function getPassword(): ?string
136
    {
137 5
        return $this->password;
138
    }
139
140 11
    public function getSalt(): ?string
141
    {
142 11
        return null;
143
    }
144
145 23
    public function getUsername(): ?string
146
    {
147 23
        return $this->username;
148
    }
149
150 26
    public function eraseCredentials(): self
151
    {
152 26
        $this->plainPassword = null;
153
154 26
        return $this;
155
    }
156
157 1
    public function confirmEmail(): self
158
    {
159 1
        $this->isEmailConfirmed = 1;
160
161 1
        return $this;
162
    }
163
164
    public function changeEmail($email): self
165
    {
166
        $this->email = $email;
167
        $this->isEmailConfirmed = 0;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return array
174
     */
175 22
    public function getRoles()
176
    {
177 22
        return $this->getRolesObject()->getRoles();
178
    }
179
180 25
    public function getRolesObject(): UserRoles
181
    {
182 25
        return $this->roles;
183
    }
184
}
185