| Conditions | 5 |
| Paths | 5 |
| Total Lines | 53 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 41 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 42 | { |
||
| 43 | $io = new SymfonyStyle($input, $output); |
||
| 44 | $user_name = $input->getArgument('user'); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var User $user |
||
| 48 | */ |
||
| 49 | $users = $this->entityManager->getRepository(User::class)->findBy(['name' => $user_name]); |
||
| 50 | $user = $users[0]; |
||
| 51 | |||
| 52 | |||
| 53 | if($user == null) |
||
| 54 | { |
||
| 55 | $io->error(sprintf('No user with the given username %s found in the database!', $user_name)); |
||
|
|
|||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | $io->note('User found!'); |
||
| 60 | |||
| 61 | $proceed = $io->confirm( |
||
| 62 | sprintf('You are going to change the password of %s with ID %d. Proceed?', |
||
| 63 | $user->getFullName(true), $user->getID())); |
||
| 64 | |||
| 65 | if(!$proceed) |
||
| 66 | { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | $success = false; |
||
| 71 | $new_password = ""; |
||
| 72 | |||
| 73 | while(!$success) { |
||
| 74 | $pw1 = $io->askHidden("Please enter new password:"); |
||
| 75 | $pw2 = $io->askHidden('Please confirm:'); |
||
| 76 | if($pw1 !== $pw2) { |
||
| 77 | $io->error('The entered password did not match! Please try again.'); |
||
| 78 | } else { |
||
| 79 | //Exit loop |
||
| 80 | $success = true; |
||
| 81 | $new_password = $pw1; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | //Encode password |
||
| 86 | $hash = $this->encoder->encodePassword($user, $new_password); |
||
| 87 | $user->setPassword($hash); |
||
| 88 | |||
| 89 | //And save it to databae |
||
| 90 | $this->entityManager->persist($user); |
||
| 91 | $this->entityManager->flush(); |
||
| 92 | |||
| 93 | $io->success('Password was set successful! You can now log in using the new password.'); |
||
| 94 | } |
||
| 96 |