| Total Complexity | 61 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AdminUserController 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 AdminUserController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AdminUserController extends BasePageController |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @throws \Throwable |
||
| 18 | */ |
||
| 19 | public function index(Request $request) |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return RedirectResponse|\Illuminate\View\View |
||
| 104 | * |
||
| 105 | * @throws \Exception |
||
| 106 | */ |
||
| 107 | public function edit(Request $request) |
||
| 108 | { |
||
| 109 | $this->setAdminPrefs(); |
||
| 110 | |||
| 111 | $user = [ |
||
| 112 | 'id' => '', |
||
| 113 | 'username' => '', |
||
| 114 | 'email' => '', |
||
| 115 | 'password' => '', |
||
| 116 | 'role' => User::ROLE_USER, |
||
| 117 | 'notes' => '', |
||
| 118 | 'rate_limit' => 60, |
||
| 119 | ]; |
||
| 120 | |||
| 121 | $meta_title = $title = 'View User'; |
||
| 122 | |||
| 123 | // set the current action |
||
| 124 | $action = $request->input('action') ?? 'view'; |
||
| 125 | |||
| 126 | // get the user roles |
||
| 127 | $userRoles = Role::cursor()->remember(); |
||
| 128 | $roles = []; |
||
| 129 | $defaultRole = 'User'; |
||
| 130 | $defaultInvites = Invitation::DEFAULT_INVITES; |
||
| 131 | foreach ($userRoles as $r) { |
||
| 132 | $roles[$r->id] = $r->name; |
||
| 133 | if ($r->isdefault === 1) { |
||
| 134 | $defaultRole = $r->id; |
||
| 135 | $defaultInvites = $r->defaultinvites; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $error = null; |
||
| 140 | |||
| 141 | switch ($action) { |
||
| 142 | case 'add': |
||
| 143 | $user += [ |
||
| 144 | 'role' => $defaultRole, |
||
| 145 | 'notes' => '', |
||
| 146 | 'invites' => $defaultInvites, |
||
| 147 | 'movieview' => 0, |
||
| 148 | 'xxxview' => 0, |
||
| 149 | 'musicview' => 0, |
||
| 150 | 'consoleview' => 0, |
||
| 151 | 'gameview' => 0, |
||
| 152 | 'bookview' => 0, |
||
| 153 | ]; |
||
| 154 | break; |
||
| 155 | case 'submit': |
||
| 156 | if (empty($request->input('id'))) { |
||
| 157 | $invites = $defaultInvites; |
||
| 158 | foreach ($userRoles as $role) { |
||
| 159 | if ($role['id'] === $request->input('role')) { |
||
| 160 | $invites = $role['defaultinvites']; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | $ret = User::signUp($request->input('username'), $request->input('password'), $request->input('email'), '', $request->input('notes'), $invites, '', true, $request->input('role'), false); |
||
| 164 | } else { |
||
| 165 | $editedUser = User::find($request->input('id')); |
||
| 166 | |||
| 167 | // Check if role is changing and get stack preference |
||
| 168 | $roleChanged = $editedUser->roles_id != $request->input('role'); |
||
| 169 | $stackRole = $request->input('stack_role') ? true : false; // Check if checkbox is checked |
||
| 170 | $changedBy = auth()->check() ? auth()->id() : null; |
||
| 171 | |||
| 172 | // CRITICAL: Capture the ORIGINAL rolechangedate BEFORE any updates |
||
| 173 | // This is needed for accurate role history tracking |
||
| 174 | $originalRoleChangeDate = $editedUser->rolechangedate; |
||
| 175 | |||
| 176 | // Handle pending role cancellation |
||
| 177 | if ($request->has('cancel_pending_role') && $request->input('cancel_pending_role')) { |
||
| 178 | $editedUser->cancelPendingRole(); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Handle rolechangedate - Update the expiry for the CURRENT role FIRST |
||
| 182 | // This must happen BEFORE role change so the new expiry applies to the old role |
||
| 183 | if ($request->has('rolechangedate')) { |
||
| 184 | $roleChangeDate = $request->input('rolechangedate'); |
||
| 185 | if (! empty($roleChangeDate)) { |
||
| 186 | User::updateUserRoleChangeDate($editedUser->id, $roleChangeDate); |
||
| 187 | $editedUser->refresh(); |
||
| 188 | } else { |
||
| 189 | // Clear the rolechangedate if empty string is provided |
||
| 190 | $editedUser->update(['rolechangedate' => null]); |
||
| 191 | $editedUser->refresh(); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // If role is changing, handle it with stacking logic |
||
| 196 | // Pass the original expiry so history records the correct old_expiry_date |
||
| 197 | if ($roleChanged && $request->input('role') !== null) { |
||
| 198 | User::updateUserRole( |
||
| 199 | $editedUser->id, |
||
| 200 | (int) $request->input('role'), // Cast to integer |
||
| 201 | true, // Apply promotions |
||
| 202 | $stackRole, // Stack role if requested |
||
| 203 | $changedBy, |
||
| 204 | $originalRoleChangeDate // Pass original expiry for history |
||
| 205 | ); |
||
| 206 | $editedUser->refresh(); |
||
| 207 | } elseif (!$roleChanged && $request->input('role') !== null) { |
||
| 208 | // Role isn't changing, but we should still apply promotions if there are any |
||
| 209 | // This handles the case where admin extends expiry date for existing role |
||
| 210 | User::updateUserRole( |
||
| 211 | $editedUser->id, |
||
| 212 | (int) $request->input('role'), // Same role |
||
| 213 | true, // Apply promotions |
||
| 214 | false, // Don't stack (not changing role) |
||
| 215 | $changedBy, |
||
| 216 | $originalRoleChangeDate |
||
| 217 | ); |
||
| 218 | $editedUser->refresh(); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Update user basic information (but NOT the role - it's handled above) |
||
| 222 | // Use current role to avoid overwriting |
||
| 223 | $ret = User::updateUser( |
||
| 224 | $editedUser->id, |
||
| 225 | $request->input('username'), |
||
| 226 | $request->input('email'), |
||
| 227 | $editedUser->grabs, |
||
| 228 | $editedUser->roles_id, // Use current role, not the request role |
||
| 229 | $request->input('notes'), |
||
| 230 | $request->input('invites'), |
||
| 231 | ($request->has('movieview') ? 1 : 0), |
||
| 232 | ($request->has('musicview') ? 1 : 0), |
||
| 233 | ($request->has('gameview') ? 1 : 0), |
||
| 234 | ($request->has('xxxview') ? 1 : 0), |
||
| 235 | ($request->has('consoleview') ? 1 : 0), |
||
| 236 | ($request->has('bookview') ? 1 : 0) |
||
| 237 | ); |
||
| 238 | |||
| 239 | if ($request->input('password') !== null) { |
||
| 240 | User::updatePassword($editedUser->id, $request->input('password')); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | if ($ret >= 0) { |
||
| 245 | return redirect()->to('admin/user-list'); |
||
| 246 | } |
||
| 247 | |||
| 248 | switch ($ret) { |
||
| 249 | case User::ERR_SIGNUP_BADUNAME: |
||
| 250 | $error = 'Bad username. Try a better one.'; |
||
| 251 | break; |
||
| 252 | case User::ERR_SIGNUP_BADPASS: |
||
| 253 | $error = 'Bad password. Try a longer one.'; |
||
| 254 | break; |
||
| 255 | case User::ERR_SIGNUP_BADEMAIL: |
||
| 256 | $error = 'Bad email.'; |
||
| 257 | break; |
||
| 258 | case User::ERR_SIGNUP_UNAMEINUSE: |
||
| 259 | $error = 'Username in use.'; |
||
| 260 | break; |
||
| 261 | case User::ERR_SIGNUP_EMAILINUSE: |
||
| 262 | $error = 'Email in use.'; |
||
| 263 | break; |
||
| 264 | default: |
||
| 265 | $error = 'Unknown save error.'; |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | $user += [ |
||
| 269 | 'id' => $request->input('id'), |
||
| 270 | 'username' => $request->input('username'), |
||
| 271 | 'email' => $request->input('email'), |
||
| 272 | 'role' => $request->input('role'), |
||
| 273 | 'notes' => $request->input('notes'), |
||
| 274 | ]; |
||
| 275 | break; |
||
| 276 | case 'view': |
||
| 277 | default: |
||
| 278 | if ($request->has('id')) { |
||
| 279 | $title = 'User Edit'; |
||
| 280 | $id = $request->input('id'); |
||
| 281 | $user = User::find($id); |
||
| 282 | |||
| 283 | // Add daily API and download counts |
||
| 284 | if ($user) { |
||
| 285 | try { |
||
| 286 | $user->daily_api_count = \App\Models\UserRequest::getApiRequests($user->id); |
||
| 287 | $user->daily_download_count = \App\Models\UserDownload::getDownloadRequests($user->id); |
||
| 288 | } catch (\Exception $e) { |
||
| 289 | $user->daily_api_count = 0; |
||
| 290 | $user->daily_download_count = 0; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | break; |
||
| 296 | } |
||
| 297 | |||
| 298 | $this->viewData = array_merge($this->viewData, [ |
||
| 299 | 'yesno_ids' => [1, 0], |
||
| 300 | 'yesno_names' => ['Yes', 'No'], |
||
| 301 | 'role_ids' => array_keys($roles), |
||
| 302 | 'role_names' => $roles, |
||
| 303 | 'user' => $user, |
||
| 304 | 'error' => $error, |
||
| 305 | 'title' => $title, |
||
| 306 | 'meta_title' => $meta_title, |
||
| 307 | ]); |
||
| 308 | |||
| 309 | return view('admin.users.edit', $this->viewData); |
||
| 310 | } |
||
| 311 | |||
| 312 | public function destroy(Request $request): RedirectResponse |
||
| 313 | { |
||
| 314 | if ($request->has('id')) { |
||
| 315 | $user = User::find($request->input('id')); |
||
| 316 | $username = $user->username; // Store username before deletion |
||
| 317 | |||
| 318 | $user->delete(); |
||
| 319 | |||
| 320 | // Redirect with username to display in notification |
||
| 321 | return redirect()->to('admin/user-list?deleted=1&username='.urlencode($username)); |
||
| 322 | } |
||
| 323 | |||
| 324 | if ($request->has('redir')) { |
||
| 325 | return redirect()->to($request->input('redir')); |
||
| 326 | } |
||
| 327 | |||
| 328 | return redirect()->to($request->server('HTTP_REFERER')); |
||
|
|
|||
| 329 | } |
||
| 330 | |||
| 331 | public function resendVerification(Request $request): RedirectResponse |
||
| 332 | { |
||
| 333 | if ($request->has('id')) { |
||
| 334 | $user = User::find($request->input('id')); |
||
| 335 | UserVerification::generate($user); |
||
| 336 | |||
| 337 | UserVerification::send($user, 'User email verification required'); |
||
| 338 | |||
| 339 | return redirect()->back()->with('success', 'Email verification for '.$user->username.' sent'); |
||
| 340 | } |
||
| 341 | |||
| 342 | return redirect()->back()->with('error', 'User is invalid'); |
||
| 343 | } |
||
| 344 | |||
| 345 | public function verify(Request $request): RedirectResponse |
||
| 355 | } |
||
| 356 | } |
||
| 357 |