| Conditions | 11 |
| Paths | 14 |
| Total Lines | 54 |
| Code Lines | 33 |
| 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 |
||
| 132 | public function deleteMyAccountAction( |
||
| 133 | Request $request, |
||
| 134 | CurrentUserApiInterface $currentUserApi, |
||
| 135 | UserRepositoryInterface $userRepository, |
||
| 136 | EventDispatcherInterface $eventDispatcher, |
||
| 137 | HookDispatcherInterface $hookDispatcher, |
||
| 138 | DeleteHelper $deleteHelper |
||
| 139 | ) { |
||
| 140 | if (!$currentUserApi->isLoggedIn()) { |
||
| 141 | throw new AccessDeniedException(); |
||
| 142 | } |
||
| 143 | if (!$this->getVar(UsersConstant::MODVAR_ALLOW_USER_SELF_DELETE, UsersConstant::DEFAULT_ALLOW_USER_SELF_DELETE)) { |
||
| 144 | $this->addFlash('error', 'Self deletion is disabled by the site administrator.'); |
||
| 145 | |||
| 146 | return $this->redirectToRoute('zikulausersmodule_account_menu'); |
||
| 147 | } |
||
| 148 | if (UsersConstant::USER_ID_ADMIN === $currentUserApi->get('uid')) { |
||
| 149 | $this->addFlash('error', 'Self deletion is not possible for main administrator.'); |
||
| 150 | |||
| 151 | return $this->redirectToRoute('zikulausersmodule_account_menu'); |
||
| 152 | } |
||
| 153 | $form = $this->createForm(DeletionType::class); |
||
| 154 | $deleteUserFormPostCreatedEvent = new DeleteUserFormPostCreatedEvent($form); |
||
| 155 | $eventDispatcher->dispatch($deleteUserFormPostCreatedEvent); |
||
| 156 | $form->handleRequest($request); |
||
| 157 | if ($form->isSubmitted()) { |
||
| 158 | if ($form->get('cancel')->isClicked()) { |
||
| 159 | $this->addFlash('status', 'Operation cancelled.'); |
||
| 160 | |||
| 161 | return $this->redirectToRoute('zikulausersmodule_account_menu'); |
||
| 162 | } |
||
| 163 | if ($form->get('delete')->isClicked()) { |
||
| 164 | $hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_VALIDATE, $hook = new ValidationHook()); |
||
| 165 | $validHooks = true; |
||
| 166 | if ($hook->getValidators()->hasErrors()) { |
||
| 167 | $message = implode('<br>', $hook->getValidators()->getErrors()); |
||
| 168 | $this->addFlash('error', $message); |
||
| 169 | $validHooks = false; |
||
| 170 | } |
||
| 171 | if ($validHooks && $form->isValid()) { |
||
| 172 | $deletedUser = $userRepository->find($currentUserApi->get('uid')); |
||
| 173 | $deleteHelper->deleteUser($deletedUser); |
||
| 174 | $eventDispatcher->dispatch(new DeleteUserFormPostValidatedEvent($form, $deletedUser)); |
||
| 175 | $request->getSession()->invalidate(); // logout |
||
| 176 | $this->addFlash('success', 'Success. Account deleted!'); |
||
| 177 | |||
| 178 | return $this->redirectToRoute('home'); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | return [ |
||
| 184 | 'form' => $form->createView(), |
||
| 185 | 'additionalTemplates' => isset($deleteUserFormPostCreatedEvent) ? $deleteUserFormPostCreatedEvent->getTemplates() : [] |
||
| 186 | ]; |
||
| 189 |