| Total Complexity | 55 |
| Total Lines | 581 |
| 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 |
||
| 39 | class User implements UserInterface, TimestampableInterface, SluggableInterface, PasswordAuthenticatedUserInterface |
||
| 40 | { |
||
| 41 | use TimestampableTrait; |
||
| 42 | use SluggableTrait; |
||
| 43 | |||
| 44 | const ROLE_DEFAULT = 'ROLE_USER'; |
||
| 45 | const ROLE_ADMIN = 'ROLE_ADMIN'; |
||
| 46 | const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @ORM\Id |
||
| 50 | * @ORM\Column(type="integer") |
||
| 51 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 52 | */ |
||
| 53 | protected ?int $id = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ORM\Column(type="string", length=50, unique=true) |
||
| 57 | */ |
||
| 58 | protected string $username = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 62 | */ |
||
| 63 | private string $email = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @ORM\Column(type="boolean") |
||
| 67 | */ |
||
| 68 | protected bool $enabled = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(type="array") |
||
| 72 | */ |
||
| 73 | private array $roles = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @ORM\Column(name="password", type="string") |
||
| 77 | */ |
||
| 78 | private ?string $password; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Plain password. Used for model validation. Must not be persisted. |
||
| 82 | */ |
||
| 83 | protected string $plainPassword = ''; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @ORM\Column(name="salt", type="string") |
||
| 87 | */ |
||
| 88 | private ?string $salt = null; |
||
|
|
|||
| 89 | |||
| 90 | /** |
||
| 91 | * @ORM\Column(name="last_login",type="datetime", nullable=true) |
||
| 92 | */ |
||
| 93 | protected ?DateTime $lastLogin = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @ORM\Column(name="confirmation_token",type="string", length=180, nullable=true, unique=true) |
||
| 97 | */ |
||
| 98 | protected ?string $confirmationToken = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @ORM\Column(name="password_requested_at",type="datetime", nullable=true) |
||
| 102 | */ |
||
| 103 | protected ?DateTime $passwordRequestedAt = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @ORM\Column(name="nbConnexion", type="integer", nullable=false) |
||
| 107 | */ |
||
| 108 | protected int $nbConnexion = 0; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @ORM\Column(name="nbForumMessage", type="integer", nullable=false) |
||
| 112 | */ |
||
| 113 | protected int $nbForumMessage = 0; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @ORM\Column(name="avatar", type="string", length=100, nullable=false) |
||
| 117 | */ |
||
| 118 | protected string $avatar = 'default.png'; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @ORM\Column(name="comment", type="text", length=100, nullable=true) |
||
| 122 | */ |
||
| 123 | protected ?string $comment = null; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @ORM\Column(name="locale", type="string", length=2, nullable=true) |
||
| 127 | */ |
||
| 128 | protected string $locale = 'en'; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @ORM\Column(name="rules_accepted", type="boolean", nullable=false) |
||
| 132 | */ |
||
| 133 | protected bool $rules_accepted = true; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @ORM\ManyToMany(targetEntity="ProjetNormandie\UserBundle\Entity\Group") |
||
| 137 | * @ORM\JoinTable(name="user_group", |
||
| 138 | * joinColumns={@ORM\JoinColumn(name="userId", referencedColumnName="id")}, |
||
| 139 | * inverseJoinColumns={@ORM\JoinColumn(name="groupId", referencedColumnName="id")} |
||
| 140 | * ) |
||
| 141 | * @var Collection |
||
| 142 | */ |
||
| 143 | protected $groups; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @ORM\OneToMany(targetEntity="ProjetNormandie\UserBundle\Entity\UserIp", mappedBy="user") |
||
| 147 | */ |
||
| 148 | private $userIp; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @ORM\ManyToOne(targetEntity="ProjetNormandie\UserBundle\Entity\Status") |
||
| 152 | * @ORM\JoinColumns({ |
||
| 153 | * @ORM\JoinColumn(name="idStatus", referencedColumnName="id", nullable=false) |
||
| 154 | * }) |
||
| 155 | */ |
||
| 156 | private $status; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return int|null |
||
| 160 | */ |
||
| 161 | public function getId(): ?int |
||
| 162 | { |
||
| 163 | return $this->id; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getUsername(): string |
||
| 170 | { |
||
| 171 | return $this->username; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param $username |
||
| 176 | * @return User |
||
| 177 | */ |
||
| 178 | public function setUsername($username): User |
||
| 179 | { |
||
| 180 | $this->username = $username; |
||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string|null |
||
| 186 | */ |
||
| 187 | public function getEmail(): ?string |
||
| 188 | { |
||
| 189 | return $this->email; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $email |
||
| 194 | * @return User |
||
| 195 | */ |
||
| 196 | public function setEmail(string $email): User |
||
| 197 | { |
||
| 198 | $this->email = $email; |
||
| 199 | |||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $boolean |
||
| 205 | * @return User |
||
| 206 | */ |
||
| 207 | public function setEnabled($boolean): User |
||
| 208 | { |
||
| 209 | $this->enabled = (bool) $boolean; |
||
| 210 | |||
| 211 | return $this; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function isEnabled(): bool |
||
| 218 | { |
||
| 219 | return $this->enabled; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return array|string[] |
||
| 224 | */ |
||
| 225 | public function getRoles(): array |
||
| 226 | { |
||
| 227 | $roles = $this->roles; |
||
| 228 | |||
| 229 | foreach ($this->getGroups() as $group) { |
||
| 230 | $roles = array_merge($roles, $group->getRoles()); |
||
| 231 | } |
||
| 232 | |||
| 233 | // we need to make sure to have at least one role |
||
| 234 | $roles[] = 'ROLE_USER'; |
||
| 235 | |||
| 236 | return array_values(array_unique($roles)); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $roles |
||
| 241 | * @return User |
||
| 242 | */ |
||
| 243 | public function setRoles(array $roles): User |
||
| 244 | { |
||
| 245 | $this->roles = $roles; |
||
| 246 | |||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getPassword(): ?string |
||
| 254 | { |
||
| 255 | return $this->password; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $password |
||
| 260 | * @return User |
||
| 261 | */ |
||
| 262 | public function setPassword(string $password): User |
||
| 263 | { |
||
| 264 | $this->password = $password; |
||
| 265 | |||
| 266 | return $this; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getPlainPassword(): ?string |
||
| 273 | { |
||
| 274 | return $this->plainPassword; |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $role |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public function hasRole($role): bool |
||
| 283 | { |
||
| 284 | return in_array(strtoupper($role), $this->getRoles(), true); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returning a salt is only needed, if you are not using a modern |
||
| 289 | * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. |
||
| 290 | */ |
||
| 291 | public function getSalt(): ?string |
||
| 292 | { |
||
| 293 | return null; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * |
||
| 298 | */ |
||
| 299 | public function eraseCredentials() |
||
| 300 | { |
||
| 301 | // If you store any temporary, sensitive data on the user, clear it here |
||
| 302 | // $this->plainPassword = null; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return DateTime|null |
||
| 307 | */ |
||
| 308 | public function getLastLogin(): ?DateTime |
||
| 309 | { |
||
| 310 | return $this->lastLogin; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param DateTime|null $time |
||
| 315 | * @return User |
||
| 316 | */ |
||
| 317 | public function setLastLogin(DateTime $time = null) : User |
||
| 318 | { |
||
| 319 | $lastLogin = $this->getLastLogin(); |
||
| 320 | if (($lastLogin === null) || ($lastLogin->format('Y-m-d') != $time->format('Y-m-d'))) { |
||
| 321 | ++$this->nbConnexion; |
||
| 322 | } |
||
| 323 | $this->lastLogin = $time; |
||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $confirmationToken |
||
| 329 | * @return User |
||
| 330 | */ |
||
| 331 | public function setConfirmationToken($confirmationToken): User |
||
| 332 | { |
||
| 333 | $this->confirmationToken = $confirmationToken; |
||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getConfirmationToken(): ?string |
||
| 341 | { |
||
| 342 | return $this->confirmationToken; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param DateTime|null $date |
||
| 347 | * @return User |
||
| 348 | */ |
||
| 349 | public function setPasswordRequestedAt(DateTime $date = null): User |
||
| 350 | { |
||
| 351 | $this->passwordRequestedAt = $date; |
||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Gets the timestamp that the user requested a password reset. |
||
| 357 | * @return null|DateTime |
||
| 358 | */ |
||
| 359 | public function getPasswordRequestedAt(): ?DateTime |
||
| 360 | { |
||
| 361 | return $this->passwordRequestedAt; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param $ttl |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function isPasswordRequestNonExpired($ttl): bool |
||
| 369 | { |
||
| 370 | return $this->getPasswordRequestedAt() instanceof DateTime && |
||
| 371 | $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time(); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getLocale(): string |
||
| 378 | { |
||
| 379 | return $this->locale; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $locale |
||
| 384 | * @return User |
||
| 385 | */ |
||
| 386 | public function setLocale(string $locale): User |
||
| 387 | { |
||
| 388 | $this->locale = $locale; |
||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return int |
||
| 394 | */ |
||
| 395 | public function getNbConnexion(): int |
||
| 396 | { |
||
| 397 | return $this->nbConnexion; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param int $nbConnexion |
||
| 402 | * @return User |
||
| 403 | */ |
||
| 404 | public function setNbConnexion(int $nbConnexion): User |
||
| 405 | { |
||
| 406 | $this->nbConnexion = $nbConnexion; |
||
| 407 | return $this; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return int |
||
| 412 | */ |
||
| 413 | public function getNbForumMessage(): int |
||
| 414 | { |
||
| 415 | return $this->nbForumMessage; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param int $nbForumMessage |
||
| 420 | * @return User |
||
| 421 | */ |
||
| 422 | public function setNbForumMessage(int $nbForumMessage): User |
||
| 423 | { |
||
| 424 | $this->nbForumMessage = $nbForumMessage; |
||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getAvatar(): string |
||
| 432 | { |
||
| 433 | return $this->avatar; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $avatar |
||
| 438 | * @return User |
||
| 439 | */ |
||
| 440 | public function setAvatar(string $avatar): User |
||
| 441 | { |
||
| 442 | $this->avatar = $avatar; |
||
| 443 | return $this; |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getComment(): ?string |
||
| 450 | { |
||
| 451 | return $this->comment; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string|null $comment |
||
| 456 | * @return User |
||
| 457 | */ |
||
| 458 | public function setComment(string $comment = null) : User |
||
| 459 | { |
||
| 460 | $this->comment = $comment; |
||
| 461 | return $this; |
||
| 462 | } |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * @return mixed |
||
| 467 | */ |
||
| 468 | public function getUserIp() |
||
| 469 | { |
||
| 470 | return $this->userIp; |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Set status |
||
| 475 | * @param Status|object|null $status |
||
| 476 | * @return User |
||
| 477 | */ |
||
| 478 | public function setStatus(Status $status = null): User |
||
| 479 | { |
||
| 480 | $this->status = $status; |
||
| 481 | return $this; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get status |
||
| 486 | * @return Status |
||
| 487 | */ |
||
| 488 | public function getStatus(): Status |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Set rules_accepted |
||
| 495 | * @param bool $rules_accepted |
||
| 496 | * @return User |
||
| 497 | */ |
||
| 498 | public function setRulesAccepted(bool $rules_accepted): User |
||
| 499 | { |
||
| 500 | $this->rules_accepted = $rules_accepted; |
||
| 501 | return $this; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Get rules_accepted |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | public function getRulesAccepted(): bool |
||
| 511 | } |
||
| 512 | |||
| 513 | |||
| 514 | /** |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | public function getIsOnline(): bool |
||
| 518 | { |
||
| 519 | $lastLogin = $this->getLastLogin(); |
||
| 520 | if (null === $lastLogin) { |
||
| 521 | return false; |
||
| 522 | } |
||
| 523 | $now = new DateTime(); |
||
| 524 | $diff = $now->format('U') - $lastLogin->format('U'); |
||
| 525 | return $diff < 300; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param $groups |
||
| 530 | * @return User |
||
| 531 | */ |
||
| 532 | public function setGroups($groups): User |
||
| 533 | { |
||
| 534 | $this->groups = $groups; |
||
| 535 | return $this; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return Collection |
||
| 540 | */ |
||
| 541 | public function getGroups(): Collection |
||
| 542 | { |
||
| 543 | return $this->groups; |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param $password |
||
| 548 | * @return User |
||
| 549 | */ |
||
| 550 | public function setPlainPassword($password): User |
||
| 551 | { |
||
| 552 | $this->plainPassword = $password; |
||
| 553 | return $this; |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param $group |
||
| 558 | * @return User |
||
| 559 | */ |
||
| 560 | public function addGroup($group): User |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param Group $group |
||
| 568 | */ |
||
| 569 | public function removeGroup(Group $group) |
||
| 570 | { |
||
| 571 | $this->groups->removeElement($group); |
||
| 572 | } |
||
| 573 | |||
| 574 | |||
| 575 | /** |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function __toString() |
||
| 579 | { |
||
| 580 | return sprintf('%s [%d]', $this->getUsername(), $this->getId()); |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @param $role |
||
| 585 | * @return User |
||
| 586 | */ |
||
| 587 | public function addRole($role): User |
||
| 588 | { |
||
| 589 | $role = strtoupper($role); |
||
| 590 | if ($role === static::ROLE_DEFAULT) { |
||
| 591 | return $this; |
||
| 592 | } |
||
| 593 | if (!in_array($role, $this->roles, true)) { |
||
| 594 | $this->roles[] = $role; |
||
| 595 | } |
||
| 596 | return $this; |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param $role |
||
| 601 | * @return User |
||
| 602 | */ |
||
| 603 | public function removeRole($role): User |
||
| 610 | } |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * Returns an array of the fields used to generate the slug. |
||
| 615 | * @return string[] |
||
| 616 | */ |
||
| 617 | public function getSluggableFields(): array |
||
| 622 |