| Conditions | 18 |
| Paths | 197 |
| Total Lines | 80 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 92 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 93 | { |
||
| 94 | $io = new SymfonyStyle($input, $output); |
||
| 95 | $uid = $input->getOption('uid'); |
||
| 96 | $gid = $input->getOption('gid'); |
||
| 97 | $status = $input->getOption('status'); |
||
| 98 | $date = $input->getOption('date'); |
||
| 99 | |||
| 100 | if (($uid && $gid) || ($uid && $status) || ($gid && $status)) { |
||
| 101 | $io->error('Do not use more than one option or argument.'); |
||
| 102 | |||
| 103 | return 1; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (isset($gid)) { |
||
| 107 | if (Constant::GROUP_ID_USERS === (int) $gid) { |
||
| 108 | if (!$io->confirm('You have selected to delete from the main user group. This is not recommended. Do you wish to proceed?', false)) { |
||
| 109 | $io->caution('Deletion cancelled'); |
||
| 110 | |||
| 111 | return 0; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | $users = $this->groupRespository->find($gid)->getUsers(); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (isset($status)) { |
||
| 118 | $statuses = [ |
||
| 119 | 'I' => UsersConstant::ACTIVATED_INACTIVE, |
||
| 120 | 'P' => UsersConstant::ACTIVATED_PENDING_REG, |
||
| 121 | 'M' => UsersConstant::ACTIVATED_PENDING_DELETE |
||
| 122 | ]; |
||
| 123 | if (!array_key_exists($status, $statuses)) { |
||
| 124 | $io->error('Invalid status value'); |
||
| 125 | |||
| 126 | return 2; |
||
| 127 | } |
||
| 128 | $users = $this->userRepository->findBy(['activated' => $statuses[$status]]); |
||
| 129 | $users = new ArrayCollection($users); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (isset($uid)) { |
||
| 133 | $user = $this->userRepository->find($uid); |
||
| 134 | $users = new ArrayCollection([$user]); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (isset($date)) { |
||
| 138 | $date = \DateTime::createFromFormat('YmdHis', $date, new \DateTimeZone('UTC')); |
||
|
|
|||
| 139 | $users = $users->filter(function (UserEntity $user) use ($date) { |
||
| 140 | return $user->getRegistrationDate() < $date; |
||
| 141 | }); |
||
| 142 | } |
||
| 143 | |||
| 144 | $adminUser = $this->userRepository->find(UsersConstant::USER_ID_ADMIN); |
||
| 145 | if ($users->contains($adminUser)) { |
||
| 146 | $users->removeElement($adminUser); |
||
| 147 | $io->note(sprintf('The main admin user cannot be deleted (uname: %s)', $adminUser->getUname())); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($users->isEmpty()) { |
||
| 151 | $io->error('No users found!'); |
||
| 152 | |||
| 153 | return 3; |
||
| 154 | } |
||
| 155 | |||
| 156 | $io->title('Zikula user deletion'); |
||
| 157 | $count = count($users); |
||
| 158 | $io->progressStart($count); |
||
| 159 | foreach ($users as $user) { |
||
| 160 | $eventName = UsersConstant::ACTIVATED_ACTIVE === $user->getActivated() ? UserEvents::DELETE_ACCOUNT : RegistrationEvents::DELETE_REGISTRATION; |
||
| 161 | $this->eventDispatcher->dispatch(new GenericEvent($user->getUid()), $eventName); |
||
| 162 | $this->eventDispatcher->dispatch(new GenericEvent(null, ['id' => $user->getUid()]), UserEvents::DELETE_PROCESS); |
||
| 163 | $this->hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_PROCESS, new ProcessHook($user->getUid())); |
||
| 164 | $this->userRepository->removeAndFlush($user); |
||
| 165 | $io->progressAdvance(); |
||
| 166 | } |
||
| 167 | $io->progressFinish(); |
||
| 168 | |||
| 169 | $io->success(sprintf('Success! %d users deleted.', $count)); |
||
| 170 | |||
| 171 | return 0; |
||
| 172 | } |
||
| 174 |