| Total Complexity | 49 |
| Total Lines | 370 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class User implements UserInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $id; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $username; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $email; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | protected $enabled; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var boolean |
||
| 40 | */ |
||
| 41 | protected $enforcePasswordChange; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The salt to use for hashing |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $salt; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Encrypted password. Must be persisted. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $password; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Plain password. Used for model validation. Must not be persisted. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $plainPassword; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \DateTime |
||
| 66 | */ |
||
| 67 | protected $lastLogin; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var TokenInterface |
||
| 71 | */ |
||
| 72 | protected $activationToken; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var TokenInterface |
||
| 76 | */ |
||
| 77 | protected $passwordResetToken; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var boolean |
||
| 81 | */ |
||
| 82 | protected $locked; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean |
||
| 86 | */ |
||
| 87 | protected $expired; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \DateTime |
||
| 91 | */ |
||
| 92 | protected $expiresAt; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $roles; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var boolean |
||
| 101 | */ |
||
| 102 | protected $credentialsExpired; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var \DateTime |
||
| 106 | */ |
||
| 107 | protected $credentialsExpireAt; |
||
| 108 | |||
| 109 | public function __construct() |
||
| 110 | { |
||
| 111 | $this->salt = base_convert(sha1(uniqid((string) mt_rand(), true)), 16, 36); |
||
| 112 | $this->enabled = false; |
||
| 113 | $this->locked = false; |
||
| 114 | $this->enforcePasswordChange = false; |
||
| 115 | $this->expired = false; |
||
| 116 | $this->roles = []; |
||
| 117 | $this->credentialsExpired = false; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Serializes the user. |
||
| 122 | * |
||
| 123 | * The serialized data have to contain the fields used by the equals method and the username. |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function serialize() |
||
| 128 | { |
||
| 129 | return serialize([ |
||
| 130 | $this->password, |
||
| 131 | $this->salt, |
||
| 132 | $this->username, |
||
| 133 | $this->expired, |
||
| 134 | $this->locked, |
||
| 135 | $this->credentialsExpired, |
||
| 136 | $this->enabled, |
||
| 137 | $this->id |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Unserializes the user. |
||
| 143 | * |
||
| 144 | * @param string $serialized |
||
| 145 | */ |
||
| 146 | public function unserialize($serialized) |
||
| 147 | { |
||
| 148 | $data = unserialize($serialized); |
||
| 149 | // add a few extra elements in the array to ensure that we have enough keys when unserializing |
||
| 150 | // older data which does not include all properties. |
||
| 151 | $data = array_merge($data, array_fill(0, 2, null)); |
||
| 152 | |||
| 153 | list( |
||
| 154 | $this->password, |
||
| 155 | $this->salt, |
||
| 156 | $this->username, |
||
| 157 | $this->expired, |
||
| 158 | $this->locked, |
||
| 159 | $this->credentialsExpired, |
||
| 160 | $this->enabled, |
||
| 161 | $this->id |
||
| 162 | ) = $data; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Removes sensitive data from the user. |
||
| 167 | */ |
||
| 168 | public function eraseCredentials(): void |
||
| 169 | { |
||
| 170 | $this->plainPassword = null; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the user unique id. |
||
| 175 | * |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | public function getId() |
||
| 179 | { |
||
| 180 | return $this->id; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getUsername(): ?string |
||
| 184 | { |
||
| 185 | return $this->username; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getSalt(): ?string |
||
| 189 | { |
||
| 190 | return $this->salt; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getEmail(): ?string |
||
| 194 | { |
||
| 195 | return $this->email; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getPassword(): ?string |
||
| 199 | { |
||
| 200 | return $this->password; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getPlainPassword(): ?string |
||
| 204 | { |
||
| 205 | return $this->plainPassword; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getLastLogin(): ?\DateTime |
||
| 209 | { |
||
| 210 | return $this->lastLogin; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getActivationToken(): ?TokenInterface |
||
| 214 | { |
||
| 215 | return $this->activationToken; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function setActivationToken(TokenInterface $activationToken): void |
||
| 219 | { |
||
| 220 | $this->activationToken = $activationToken; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function removeActivationToken(): void |
||
| 224 | { |
||
| 225 | $this->activationToken = null; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getPasswordResetToken(): ?TokenInterface |
||
| 229 | { |
||
| 230 | return $this->passwordResetToken; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function setPasswordResetToken(TokenInterface $passwordResetToken): void |
||
| 234 | { |
||
| 235 | $this->passwordResetToken = $passwordResetToken; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function removePasswordResetToken(): void |
||
| 239 | { |
||
| 240 | $this->passwordResetToken = null; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getRoles(): array |
||
| 244 | { |
||
| 245 | $roles = $this->roles; |
||
| 246 | |||
| 247 | return array_unique($roles); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function isAccountNonExpired(): bool |
||
| 251 | { |
||
| 252 | if (true === $this->expired) { |
||
| 253 | return false; |
||
| 254 | } |
||
| 255 | |||
| 256 | if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) { |
||
| 257 | return false; |
||
| 258 | } |
||
| 259 | |||
| 260 | return true; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function isAccountNonLocked(): bool |
||
| 264 | { |
||
| 265 | return !$this->locked; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function isCredentialsNonExpired(): bool |
||
| 269 | { |
||
| 270 | if (true === $this->credentialsExpired) { |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | |||
| 274 | if (null !== $this->credentialsExpireAt && $this->credentialsExpireAt->getTimestamp() < time()) { |
||
| 275 | return false; |
||
| 276 | } |
||
| 277 | |||
| 278 | return true; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function isCredentialsExpired(): bool |
||
| 282 | { |
||
| 283 | return !$this->isCredentialsNonExpired(); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function isEnabled(): bool |
||
| 287 | { |
||
| 288 | return $this->enabled; |
||
| 289 | } |
||
| 290 | |||
| 291 | public function isExpired(): bool |
||
| 292 | { |
||
| 293 | return !$this->isAccountNonExpired(); |
||
| 294 | } |
||
| 295 | |||
| 296 | public function isLocked(): bool |
||
| 297 | { |
||
| 298 | return !$this->isAccountNonLocked(); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function addRole(string $role): void |
||
| 302 | { |
||
| 303 | $role = strtoupper($role); |
||
| 304 | |||
| 305 | if (!in_array($role, $this->roles, true)) { |
||
| 306 | $this->roles[] = $role; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | public function removeRole(string $role): void |
||
| 311 | { |
||
| 312 | if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { |
||
| 313 | unset($this->roles[$key]); |
||
| 314 | $this->roles = array_values($this->roles); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | public function setUsername(string $username): void |
||
| 319 | { |
||
| 320 | $this->username = $username; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function setCredentialsExpireAt(\DateTime $date): void |
||
| 324 | { |
||
| 325 | $this->credentialsExpireAt = $date; |
||
| 326 | } |
||
| 327 | |||
| 328 | public function setCredentialsExpired(bool $boolean): void |
||
| 329 | { |
||
| 330 | $this->credentialsExpired = $boolean; |
||
| 331 | } |
||
| 332 | |||
| 333 | public function setEmail(string $email): void |
||
| 334 | { |
||
| 335 | $this->email = $email; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function setEnabled(bool $boolean): void |
||
| 339 | { |
||
| 340 | $this->enabled = $boolean; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function setExpired(bool $boolean): void |
||
| 344 | { |
||
| 345 | $this->expired = $boolean; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function setExpiresAt(\DateTime $date): void |
||
| 349 | { |
||
| 350 | $this->expiresAt = $date; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function setPassword(string $password): void |
||
| 354 | { |
||
| 355 | $this->password = $password; |
||
| 356 | } |
||
| 357 | |||
| 358 | public function setPlainPassword(string $password): void |
||
| 359 | { |
||
| 360 | $this->plainPassword = $password; |
||
| 361 | } |
||
| 362 | |||
| 363 | public function setLastLogin(\DateTime $time): void |
||
| 364 | { |
||
| 365 | $this->lastLogin = $time; |
||
| 366 | } |
||
| 367 | |||
| 368 | public function setLocked(bool $boolean): void |
||
| 369 | { |
||
| 370 | $this->locked = $boolean; |
||
| 371 | } |
||
| 372 | |||
| 373 | public function isForcedToChangePassword(): bool |
||
| 374 | { |
||
| 375 | return $this->enforcePasswordChange; |
||
| 376 | } |
||
| 377 | |||
| 378 | public function enforcePasswordChange(bool $enforcePasswordChange): void |
||
| 381 | } |
||
| 382 | |||
| 383 | public function __toString(): string |
||
| 384 | { |
||
| 385 | return (string) $this->getUsername(); |
||
| 386 | } |
||
| 387 | } |
||
| 388 |