Completed
Push — master ( caf5ac...a42f17 )
by
unknown
16s
created

BaseUser   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 4

Importance

Changes 0
Metric Value
wmc 29
lcom 4
cbo 4
dl 0
loc 243
rs 10
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 2
A getRoles() 0 4 1
A getPassword() 0 4 1
A getSalt() 0 4 1
A encodePassword() 0 13 3
A getUsername() 0 4 1
A eraseCredentials() 0 4 1
A getDisplayName() 0 4 1
A __toString() 0 4 1
A clearPasswordResetToken() 0 6 1
A generatePasswordResetToken() 0 6 1
A getPasswordResetToken() 0 4 1
A getEmail() 0 4 1
A setPassword() 0 6 1
A getId() 0 4 1
A hasPlainPassword() 0 4 1
A getPlainPassword() 0 4 1
A change() 0 7 1
A toggleBlock() 0 10 2
A isBlocked() 0 4 1
A canSwitchTo() 0 4 1
A serialize() 0 9 1
A unserialize() 0 4 1
A isEqualTo() 0 8 2
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Serializable;
7
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
8
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
9
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
10
use SumoCoders\FrameworkMultiUserBundle\ValueObject\Status;
11
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
12
use Symfony\Component\Security\Core\User\EquatableInterface;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
15
/**
16
 * @ORM\Entity(repositoryClass="SumoCoders\FrameworkMultiUserBundle\User\DoctrineBaseUserRepository")
17
 * @ORM\InheritanceType("JOINED")
18
 * @ORM\DiscriminatorColumn(name="discr", type="string")
19
 */
20
class BaseUser implements User, Serializable, EquatableInterface
21
{
22
    /**
23
     * @var int
24
     *
25
     * @ORM\Column(type="integer")
26
     * @ORM\Id
27
     * @ORM\GeneratedValue
28
     */
29
    protected $id;
30
31
    /**
32
     * @var string
33
     *
34
     * @ORM\Column(type="string")
35
     */
36
    protected $username;
37
38
    /**
39
     * @var string
40
     *
41
     * @ORM\Column(type="string")
42
     */
43
    protected $password;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(type="string")
49
     */
50
    protected $salt;
51
52
    /**
53
     * @var string
54
     *
55
     * @ORM\Column(type="string")
56
     */
57
    protected $displayName;
58
59
    /**
60
     * @var PasswordResetToken
61
     *
62
     * @ORM\Column(type="string", nullable=true)
63
     */
64
    protected $passwordResetToken;
65
66
    /**
67
     * @var string
68
     *
69
     * @ORM\Column(type="string")
70
     */
71
    protected $email;
72
73
    /**
74
     * @var Status
75
     *
76
     * @ORM\Column(type="user_status")
77
     */
78
    protected $status;
79
80
    /**
81
     * @var string
82
     */
83
    protected $plainPassword;
84
85
    /**
86
     * @param string $username
87
     * @param string $plainPassword
88
     * @param string $displayName
89
     * @param string $email
90
     * @param int $id
91
     * @param PasswordResetToken $token
92
     */
93
    public function __construct(
94
        string $username,
95
        string $plainPassword,
96
        string $displayName,
97
        string $email,
98
        int $id = null,
99
        PasswordResetToken $token = null
100
    ) {
101
        $this->username = $username;
102
        $this->plainPassword = $plainPassword;
103
        $this->displayName = $displayName;
104
        $this->email = $email;
105
        $this->id = $id;
106
107
        // set the default status to active
108
        $this->status = Status::active();
0 ignored issues
show
Documentation Bug introduced by
It seems like \SumoCoders\FrameworkMul...Object\Status::active() of type object<self> is incompatible with the declared type object<SumoCoders\Framew...dle\ValueObject\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
109
110
        if ($token) {
111
            $this->passwordResetToken = $token;
112
        }
113
    }
114
115
    public function getRoles(): array
116
    {
117
        return ['ROLE_USER'];
118
    }
119
120
    public function getPassword(): string
121
    {
122
        return $this->password;
123
    }
124
125
    public function getSalt(): string
126
    {
127
        return $this->salt;
128
    }
129
130
    public function encodePassword(PasswordEncoderInterface $encoder): void
131
    {
132
        if (empty($this->plainPassword)) {
133
            return;
134
        }
135
136
        if (empty($this->salt)) {
137
            $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
138
        }
139
140
        $this->password = $encoder->encodePassword($this->plainPassword, $this->salt);
141
        $this->eraseCredentials();
142
    }
143
144
    public function getUsername(): string
145
    {
146
        return $this->username;
147
    }
148
149
    public function eraseCredentials(): void
150
    {
151
        $this->plainPassword = null;
152
    }
153
154
    public function getDisplayName(): string
155
    {
156
        return $this->displayName;
157
    }
158
159
    public function __toString(): string
160
    {
161
        return $this->getDisplayName();
162
    }
163
164
    public function clearPasswordResetToken(): self
165
    {
166
        $this->passwordResetToken = null;
167
168
        return $this;
169
    }
170
171
    public function generatePasswordResetToken(): self
172
    {
173
        $this->passwordResetToken = PasswordResetToken::generate();
174
175
        return $this;
176
    }
177
178
    public function getPasswordResetToken(): ?PasswordResetToken
179
    {
180
        return $this->passwordResetToken;
181
    }
182
183
    public function getEmail(): string
184
    {
185
        return $this->email;
186
    }
187
188
    public function setPassword(string $password): self
189
    {
190
        $this->plainPassword = $password;
191
192
        return $this;
193
    }
194
195
    public function getId(): int
196
    {
197
        return $this->id;
198
    }
199
200
    public function hasPlainPassword(): bool
201
    {
202
        return !empty($this->plainPassword);
203
    }
204
205
    public function getPlainPassword(): string
206
    {
207
        return $this->plainPassword;
208
    }
209
210
    public function change(UserDataTransferObject $data): void
211
    {
212
        $this->username = $data->getUserName();
213
        $this->plainPassword = $data->getPlainPassword();
214
        $this->displayName = $data->getDisplayName();
215
        $this->email = $data->getEmail();
216
    }
217
218
    public function toggleBlock(): void
219
    {
220
        if ((string) $this->status === Status::BLOCKED) {
221
            $this->status = Status::active();
0 ignored issues
show
Documentation Bug introduced by
It seems like \SumoCoders\FrameworkMul...Object\Status::active() of type object<self> is incompatible with the declared type object<SumoCoders\Framew...dle\ValueObject\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
222
223
            return;
224
        }
225
226
        $this->status = Status::blocked();
0 ignored issues
show
Documentation Bug introduced by
It seems like \SumoCoders\FrameworkMul...bject\Status::blocked() of type object<self> is incompatible with the declared type object<SumoCoders\Framew...dle\ValueObject\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
227
    }
228
229
    public function isBlocked(): bool
230
    {
231
        return $this->status->isBlocked();
232
    }
233
234
    public function canSwitchTo(BaseUser $user): bool
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
235
    {
236
        return false;
237
    }
238
239
    public function serialize(): string
240
    {
241
        return serialize(
242
            [
243
                $this->id,
244
                $this->username,
245
            ]
246
        );
247
    }
248
249
    public function unserialize($serialized): void
250
    {
251
        [$this->id, $this->username] = unserialize($serialized);
252
    }
253
254
    public function isEqualTo(UserInterface $user): bool
255
    {
256
        if ($user instanceof self) {
257
            return $user->getId() === $this->getId();
258
        }
259
260
        return false;
261
    }
262
}
263