Completed
Pull Request — master (#30)
by
unknown
07:39
created

User::isBlocked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\User;
4
5
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
6
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
7
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User as UserInterface;
8
use SumoCoders\FrameworkMultiUserBundle\ValueObject\Status;
9
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
10
11
class User implements UserInterface
12
{
13
    /** @var string */
14
    protected $username;
15
16
    /** @var string */
17
    protected $salt;
18
19
    /** @var string */
20
    protected $plainPassword;
21
22
    /** @var string */
23
    protected $password;
24
25
    /** @var string */
26
    protected $displayName;
27
28
    /** @var PasswordResetToken */
29
    protected $passwordResetToken;
30
31
    /** @var string */
32
    protected $email;
33
34
    /** @var int */
35
    protected $id;
36
37
    /** @var Status */
38
    protected $status;
39
40
    /**
41
     * @param string $username
42
     * @param string $plainPassword
43
     * @param string $displayName
44
     * @param string $email
45
     * @param int $id
46
     * @param PasswordResetToken $token
47
     */
48
    public function __construct(
49
        $username,
50
        $plainPassword,
51
        $displayName,
52
        $email,
53
        $id = null,
54
        PasswordResetToken $token = null
55
    ) {
56
        $this->username = $username;
57
        $this->plainPassword = $plainPassword;
58
        $this->displayName = $displayName;
59
        $this->email = $email;
60
        $this->id = $id;
61
62
        if ($token) {
63
            $this->passwordResetToken = $token;
64
        }
65
    }
66
67
    public function getRoles()
68
    {
69
        return ['ROLE_USER'];
70
    }
71
72
    public function getPassword()
73
    {
74
        return $this->password;
75
    }
76
77
    public function getSalt()
78
    {
79
        return $this->salt;
80
    }
81
82
    public function encodePassword(PasswordEncoderInterface $encoder)
83
    {
84
        if (empty($this->plainPassword)) {
85
            return;
86
        }
87
88
        if (empty($this->salt)) {
89
            $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
90
        }
91
92
        $this->password = $encoder->encodePassword($this->plainPassword, $this->salt);
93
        $this->eraseCredentials();
94
    }
95
96
    public function getUsername()
97
    {
98
        return $this->username;
99
    }
100
101
    public function eraseCredentials()
102
    {
103
        $this->plainPassword = null;
104
    }
105
106
    public function getDisplayName()
107
    {
108
        return $this->displayName;
109
    }
110
111
    public function __toString()
112
    {
113
        return $this->getDisplayName();
114
    }
115
116
    public function clearPasswordResetToken()
117
    {
118
        $this->passwordResetToken = null;
119
120
        return $this;
121
    }
122
123
    public function generatePasswordResetToken()
124
    {
125
        $this->passwordResetToken = PasswordResetToken::generate();
126
127
        return $this;
128
    }
129
130
    public function getPasswordResetToken()
131
    {
132
        return $this->passwordResetToken;
133
    }
134
135
    public function getEmail()
136
    {
137
        return $this->email;
138
    }
139
140
    public function setPassword($password)
141
    {
142
        $this->plainPassword = $password;
143
144
        return $this;
145
    }
146
147
    public function getId()
148
    {
149
        return $this->id;
150
    }
151
152
    public function hasPlainPassword()
153
    {
154
        return !empty($this->plainPassword);
155
    }
156
157
    public function getPlainPassword()
158
    {
159
        return $this->plainPassword;
160
    }
161
162
    public function change(UserDataTransferObject $data)
163
    {
164
        $this->username = $data->getUserName();
165
        $this->plainPassword = $data->getPlainPassword();
166
        $this->displayName = $data->getDisplayName();
167
        $this->email = $data->getEmail();
168
    }
169
170
    public function toggleBlock()
171
    {
172
        if ((string) $this->status === Status::BLOCKED) {
173
            $this->status = Status::active();
174
175
            return;
176
        }
177
178
        $this->status = Status::blocked();
179
    }
180
181
    public function isBlocked()
182
    {
183
        return $this->status->isBlocked();
184
    }
185
}
186