Completed
Pull Request — master (#16)
by
unknown
06:04 queued 05:31
created

UserWithPassword::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getEmail()
182
    {
183
        return $this->email;
184
    }
185
186
    /**
187
     * @param string $password
188
     *
189
     * @return self
190
     */
191
    public function setPassword($password)
192
    {
193
        $this->password = $password;
194
195
        return $this;
196
    }
197
198
    /**
199
     * @return int
200
     */
201
    public function getId()
202
    {
203
        return $this->id;
204
    }
205
206
    /**
207
     * @param string $username
208
     * @param string $plainPassword
209
     * @param string $displayName
210
     * @param string $email
211
     */
212
    public function change(
213
        $username,
214
        $plainPassword,
215
        $displayName,
216
        $email
217
    ) {
218
        $this->username = $username;
219
        $this->plainPassword = $plainPassword;
220
        $this->displayName = $displayName;
221
        $this->email = $email;
222
    }
223
}
224