Completed
Push — master ( 0875a1...338cb2 )
by
unknown
8s
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\DataTransferObject\UserDataTransferObject;
6
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
7
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
8
9
class UserWithPassword implements User, PasswordReset
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $username;
15
16
    /**
17
     * @var string
18
     */
19
    protected $salt;
20
21
    /**
22
     * @var string
23
     */
24
    protected $plainPassword;
25
26
    /**
27
     * @var string
28
     */
29
    protected $password;
30
31
    /**
32
     * @var string
33
     */
34
    protected $displayName;
35
36
    /**
37
     * @var PasswordResetToken
38
     */
39
    protected $passwordResetToken;
40
41
    /**
42
     * @var string
43
     */
44
    protected $email;
45
46
    /**
47
     * @var int
48
     */
49
    protected $id;
50
51
    /**
52
     * @param string $username
53
     * @param string $plainPassword
54
     * @param string $displayName
55
     * @param string $email
56
     * @param int $id
57
     * @param PasswordResetToken $token
58
     */
59
    public function __construct(
60
        $username,
61
        $plainPassword,
62
        $displayName,
63
        $email,
64
        $id = null,
65
        PasswordResetToken $token = null
66
    ) {
67
        $this->username = $username;
68
        $this->plainPassword = $plainPassword;
69
        $this->displayName = $displayName;
70
        $this->email = $email;
71
        $this->id = $id;
72
73
        if ($token) {
74
            $this->passwordResetToken = $token;
75
        }
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function getRoles()
82
    {
83
        return [ 'ROLE_USER' ];
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function getPassword()
90
    {
91
        return $this->password;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getSalt()
98
    {
99
        return $this->salt;
100
    }
101
102
    /**
103
     * @param PasswordEncoderInterface $encoder
104
     */
105
    public function encodePassword(PasswordEncoderInterface $encoder)
106
    {
107
        if (empty($this->plainPassword)) {
108
            return;
109
        }
110
111
        if (empty($this->salt)) {
112
            $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
113
        }
114
115
        $this->password = $encoder->encodePassword($this->plainPassword, $this->salt);
116
        $this->eraseCredentials();
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function getUsername()
123
    {
124
        return $this->username;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function eraseCredentials()
131
    {
132
        $this->plainPassword = null;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function getDisplayName()
139
    {
140
        return $this->displayName;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function __toString()
147
    {
148
        return $this->getDisplayName();
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function clearPasswordResetToken()
155
    {
156
        $this->passwordResetToken = null;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return self
163
     */
164
    public function generatePasswordResetToken()
165
    {
166
        $this->passwordResetToken = PasswordResetToken::generate();
167
168
        return $this;
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getPasswordResetToken()
175
    {
176
        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...
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getEmail()
183
    {
184
        return $this->email;
185
    }
186
187
    /**
188
     * @param string $password
189
     *
190
     * @return self
191
     */
192
    public function setPassword($password)
193
    {
194
        $this->password = $password;
195
196
        return $this;
197
    }
198
199
    /**
200
     * @return int
201
     */
202
    public function getId()
203
    {
204
        return $this->id;
205
    }
206
207
    /**
208
     * @return bool
209
     */
210
    public function hasPlainPassword()
211
    {
212
        return !empty($this->plainPassword);
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    public function getPlainPassword()
219
    {
220
        return $this->plainPassword;
221
    }
222
223
    /**
224
     * @param UserDataTransferObject $data
225
     */
226
    public function change(
227
        UserDataTransferObject $data
228
    ) {
229
        $this->username = $data->userName;
0 ignored issues
show
Bug introduced by
Accessing userName on the interface SumoCoders\FrameworkMult...\UserDataTransferObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
230
        $this->plainPassword = $data->plainPassword;
0 ignored issues
show
Bug introduced by
Accessing plainPassword on the interface SumoCoders\FrameworkMult...\UserDataTransferObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
231
        $this->displayName = $data->displayName;
0 ignored issues
show
Bug introduced by
Accessing displayName on the interface SumoCoders\FrameworkMult...\UserDataTransferObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
232
        $this->email = $data->email;
0 ignored issues
show
Bug introduced by
Accessing email on the interface SumoCoders\FrameworkMult...\UserDataTransferObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
233
    }
234
}
235