Completed
Push — master ( c7ec68...dd7588 )
by
unknown
44s
created

User::setPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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