| Total Complexity | 57 |
| Total Lines | 399 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like UserAdministrationController 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 UserAdministrationController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 66 | class UserAdministrationController extends AbstractController |
||
| 67 | { |
||
| 68 | /** |
||
| 69 | * @Route("/list/{sort}/{sortdir}/{letter}/{startnum}") |
||
| 70 | * @PermissionCheck("moderate") |
||
| 71 | * @Theme("admin") |
||
| 72 | * @Template("@ZikulaUsersModule/UserAdministration/list.html.twig") |
||
| 73 | */ |
||
| 74 | public function listAction( |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Called from UsersModule/Resources/public/js/Zikula.Users.Admin.View.js |
||
| 116 | * to populate a username search |
||
| 117 | * |
||
| 118 | * @Route("/getusersbyfragmentastable", methods = {"POST"}, options={"expose"=true, "i18n"=false}) |
||
| 119 | */ |
||
| 120 | public function getUsersByFragmentAsTableAction( |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @Route("/user/modify/{user}", requirements={"user" = "^[1-9]\d*$"}) |
||
| 146 | * @Theme("admin") |
||
| 147 | * @Template("@ZikulaUsersModule/UserAdministration/modify.html.twig") |
||
| 148 | * |
||
| 149 | * @return array|RedirectResponse |
||
| 150 | * @throws AccessDeniedException Thrown if the user hasn't edit permissions for the user record |
||
| 151 | */ |
||
| 152 | public function modifyAction( |
||
| 210 | ]; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @Route("/approve/{user}/{force}", requirements={"user" = "^[1-9]\d*$"}) |
||
| 215 | * @PermissionCheck("moderate") |
||
| 216 | * @Theme("admin") |
||
| 217 | * @Template("@ZikulaUsersModule/UserAdministration/approve.html.twig") |
||
| 218 | * |
||
| 219 | * @return array|RedirectResponse |
||
| 220 | */ |
||
| 221 | public function approveAction( |
||
| 274 | ]; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @Route("/delete/{user}", requirements={"user" = "^[1-9]\d*$"}) |
||
| 279 | * @PermissionCheck("delete") |
||
| 280 | * @Theme("admin") |
||
| 281 | * @Template("@ZikulaUsersModule/UserAdministration/delete.html.twig") |
||
| 282 | * |
||
| 283 | * @return array|RedirectResponse |
||
| 284 | */ |
||
| 285 | public function deleteAction( |
||
| 286 | Request $request, |
||
| 287 | CurrentUserApiInterface $currentUserApi, |
||
| 288 | UserRepositoryInterface $userRepository, |
||
| 289 | HookDispatcherInterface $hookDispatcher, |
||
| 290 | EventDispatcherInterface $eventDispatcher, |
||
| 291 | UserEntity $user = null |
||
| 292 | ) { |
||
| 293 | $uids = []; |
||
| 294 | if (!isset($user) && 'POST' === $request->getMethod() && $request->request->has('zikulausersmodule_delete')) { |
||
| 295 | $uids = $request->request->get('zikulausersmodule_delete')['users']; |
||
| 296 | } elseif (isset($user)) { |
||
| 297 | $uids = [$user->getUid()]; |
||
| 298 | } |
||
| 299 | $usersImploded = implode(',', $uids); |
||
| 300 | |||
| 301 | $deleteConfirmationForm = $this->createForm(DeleteConfirmationType::class, [ |
||
| 302 | 'users' => $usersImploded |
||
| 303 | ]); |
||
| 304 | $deleteConfirmationForm->handleRequest($request); |
||
| 305 | if (empty($uids) && !$deleteConfirmationForm->isSubmitted()) { |
||
| 306 | throw new InvalidArgumentException($this->trans('No users selected.')); |
||
| 307 | } |
||
| 308 | if ($deleteConfirmationForm->isSubmitted()) { |
||
| 309 | if ($deleteConfirmationForm->get('cancel')->isClicked()) { |
||
| 310 | $this->addFlash('success', 'Operation cancelled.'); |
||
| 311 | |||
| 312 | return $this->redirectToRoute('zikulausersmodule_useradministration_list'); |
||
| 313 | } |
||
| 314 | $userIdsImploded = $deleteConfirmationForm->get('users')->getData(); |
||
| 315 | $userIds = explode(',', $userIdsImploded); |
||
| 316 | $valid = true; |
||
| 317 | foreach ($userIds as $k => $uid) { |
||
| 318 | if (in_array($uid, [UsersConstant::USER_ID_ANONYMOUS, UsersConstant::USER_ID_ADMIN, $currentUserApi->get('uid')], true)) { |
||
| 319 | unset($userIds[$k]); |
||
| 320 | $this->addFlash('danger', $this->trans('You are not allowed to delete user id %uid%', ['%uid%' => $uid])); |
||
| 321 | continue; |
||
| 322 | } |
||
| 323 | $event = new GenericEvent(null, ['id' => $uid], new ValidationProviders()); |
||
| 324 | $validators = $eventDispatcher->dispatch($event, UserEvents::DELETE_VALIDATE)->getData(); |
||
| 325 | $hook = new ValidationHook($validators); |
||
| 326 | $hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_VALIDATE, $hook); |
||
| 327 | $validators = $hook->getValidators(); |
||
| 328 | if ($validators->hasErrors()) { |
||
| 329 | $valid = false; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | if ($valid && $deleteConfirmationForm->isValid()) { |
||
| 333 | // send email to 'denied' registrations. see MailHelper::sendNotification (regdeny) #2915 |
||
| 334 | $deletedUsers = $userRepository->query(['uid' => ['operator' => 'in', 'operand' => $userIds]]); |
||
| 335 | foreach ($deletedUsers as $deletedUser) { |
||
| 336 | $eventName = UsersConstant::ACTIVATED_ACTIVE === $deletedUser->getActivated() ? UserEvents::DELETE_ACCOUNT : RegistrationEvents::DELETE_REGISTRATION; |
||
| 337 | $eventDispatcher->dispatch(new GenericEvent($deletedUser->getUid()), $eventName); |
||
| 338 | $eventDispatcher->dispatch(new GenericEvent(null, ['id' => $deletedUser->getUid()]), UserEvents::DELETE_PROCESS); |
||
| 339 | $hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_PROCESS, new ProcessHook($deletedUser->getUid())); |
||
| 340 | $userRepository->removeAndFlush($deletedUser); |
||
| 341 | } |
||
| 342 | $this->addFlash( |
||
| 343 | 'success', |
||
| 344 | /** @Desc("{count, plural,\n one {User deleted!}\n other {# users deleted!}\n}") */ |
||
| 345 | $this->getTranslator()->trans( |
||
| 346 | 'plural_n.users.deleted', |
||
| 347 | ['%count%' => count($deletedUsers)] |
||
| 348 | ) |
||
| 349 | ); |
||
| 350 | |||
| 351 | return $this->redirectToRoute('zikulausersmodule_useradministration_list'); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | $users = $userRepository->findByUids($uids); |
||
| 355 | |||
| 356 | return [ |
||
| 357 | 'users' => $users, |
||
| 358 | 'form' => $deleteConfirmationForm->createView() |
||
| 359 | ]; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @Route("/search") |
||
| 364 | * @PermissionCheck("moderate") |
||
| 365 | * @Theme("admin") |
||
| 366 | * @Template("@ZikulaUsersModule/UserAdministration/search.html.twig") |
||
| 367 | * |
||
| 368 | * @return array|Response |
||
| 369 | */ |
||
| 370 | public function searchAction( |
||
| 371 | Request $request, |
||
| 372 | UserRepositoryInterface $userRepository, |
||
| 373 | VariableApiInterface $variableApi |
||
| 374 | ) { |
||
| 375 | $form = $this->createForm(SearchUserType::class, []); |
||
| 376 | $form->handleRequest($request); |
||
| 377 | if ($form->isSubmitted()) { |
||
| 378 | $resultsForm = $this->createForm(DeleteType::class, [], [ |
||
| 379 | 'choices' => $userRepository->queryBySearchForm($form->getData(), 250), |
||
| 380 | 'action' => $this->generateUrl('zikulausersmodule_useradministration_delete') |
||
| 381 | ]); |
||
| 382 | |||
| 383 | return $this->render('@ZikulaUsersModule/UserAdministration/searchResults.html.twig', [ |
||
| 384 | 'resultsForm' => $resultsForm->createView(), |
||
| 385 | 'mailForm' => $this->buildMailForm($variableApi)->createView() |
||
| 386 | ]); |
||
| 387 | } |
||
| 388 | |||
| 389 | return [ |
||
| 390 | 'form' => $form->createView() |
||
| 391 | ]; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Route("/mail") |
||
| 396 | * @PermissionCheck({"$_zkModule::MailUsers", "::", "comment"}) |
||
| 397 | */ |
||
| 398 | public function mailUsersAction( |
||
| 422 | } |
||
| 423 | |||
| 424 | private function buildMailForm(VariableApiInterface $variableApi): FormInterface |
||
| 425 | { |
||
| 426 | return $this->createForm(MailType::class, [ |
||
| 427 | 'from' => $variableApi->getSystemVar('sitename'), |
||
| 428 | 'replyto' => $variableApi->getSystemVar('adminmail'), |
||
| 429 | 'format' => 'text', |
||
| 430 | 'batchsize' => 100 |
||
| 431 | ], [ |
||
| 432 | 'action' => $this->generateUrl('zikulausersmodule_useradministration_mailusers') |
||
| 433 | ]); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Prevent user from modifying certain aspects of self. |
||
| 438 | */ |
||
| 439 | private function checkSelf( |
||
| 465 | } |
||
| 466 | } |
||
| 468 |