| Conditions | 30 |
| Paths | 903 |
| Total Lines | 190 |
| Code Lines | 130 |
| 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 |
||
| 109 | public function edit(Request $request) |
||
| 110 | { |
||
| 111 | $this->setAdminPrefs(); |
||
| 112 | |||
| 113 | $user = [ |
||
| 114 | 'id' => '', |
||
| 115 | 'username' => '', |
||
| 116 | 'email' => '', |
||
| 117 | 'password' => '', |
||
| 118 | 'role' => User::ROLE_USER, |
||
| 119 | 'notes' => '', |
||
| 120 | 'rate_limit' => 60, |
||
| 121 | ]; |
||
| 122 | |||
| 123 | $meta_title = $title = 'View User'; |
||
| 124 | |||
| 125 | // set the current action |
||
| 126 | $action = $request->input('action') ?? 'view'; |
||
| 127 | |||
| 128 | // get the user roles |
||
| 129 | $userRoles = Role::cursor()->remember(); |
||
| 130 | $roles = []; |
||
| 131 | $defaultRole = 'User'; |
||
| 132 | $defaultInvites = Invitation::DEFAULT_INVITES; |
||
| 133 | foreach ($userRoles as $r) { |
||
| 134 | $roles[$r->id] = $r->name; |
||
| 135 | if ($r->isdefault === 1) { |
||
| 136 | $defaultRole = $r->id; |
||
| 137 | $defaultInvites = $r->defaultinvites; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $error = null; |
||
| 142 | |||
| 143 | switch ($action) { |
||
| 144 | case 'add': |
||
| 145 | $user += [ |
||
| 146 | 'role' => $defaultRole, |
||
| 147 | 'notes' => '', |
||
| 148 | 'invites' => $defaultInvites, |
||
| 149 | 'movieview' => 0, |
||
| 150 | 'xxxview' => 0, |
||
| 151 | 'musicview' => 0, |
||
| 152 | 'consoleview' => 0, |
||
| 153 | 'gameview' => 0, |
||
| 154 | 'bookview' => 0, |
||
| 155 | ]; |
||
| 156 | break; |
||
| 157 | case 'submit': |
||
| 158 | if (empty($request->input('id'))) { |
||
| 159 | $invites = $defaultInvites; |
||
| 160 | foreach ($userRoles as $role) { |
||
| 161 | if ($role['id'] === $request->input('role')) { |
||
| 162 | $invites = $role['defaultinvites']; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | $ret = User::signUp($request->input('username'), $request->input('password'), $request->input('email'), '', $request->input('notes'), $invites, '', true, $request->input('role'), false); |
||
| 166 | } else { |
||
| 167 | $editedUser = User::find($request->input('id')); |
||
| 168 | |||
| 169 | // Check if role is changing and get stack preference |
||
| 170 | $roleChanged = $editedUser->roles_id != $request->input('role'); |
||
| 171 | $stackRole = $request->input('stack_role') ? true : false; // Check if checkbox is checked |
||
| 172 | $changedBy = auth()->check() ? auth()->id() : null; |
||
| 173 | |||
| 174 | // CRITICAL: Capture the ORIGINAL rolechangedate BEFORE any updates |
||
| 175 | // This is needed for accurate role history tracking |
||
| 176 | $originalRoleChangeDate = $editedUser->rolechangedate; |
||
| 177 | |||
| 178 | // Handle pending role cancellation |
||
| 179 | if ($request->has('cancel_pending_role') && $request->input('cancel_pending_role')) { |
||
| 180 | $editedUser->cancelPendingRole(); |
||
| 181 | } |
||
| 182 | |||
| 183 | // Handle rolechangedate - Update the expiry for the CURRENT role FIRST |
||
| 184 | // This must happen BEFORE role change so the new expiry applies to the old role |
||
| 185 | if ($request->has('rolechangedate')) { |
||
| 186 | $roleChangeDate = $request->input('rolechangedate'); |
||
| 187 | if (! empty($roleChangeDate)) { |
||
| 188 | User::updateUserRoleChangeDate($editedUser->id, $roleChangeDate); |
||
| 189 | } else { |
||
| 190 | // Clear the rolechangedate if empty string is provided |
||
| 191 | $editedUser->update(['rolechangedate' => null]); |
||
| 192 | } |
||
| 193 | $editedUser->refresh(); |
||
| 194 | } |
||
| 195 | |||
| 196 | // If role is changing, handle it with stacking logic |
||
| 197 | // Pass the original expiry so history records the correct old_expiry_date |
||
| 198 | if ($roleChanged && $request->input('role') !== null) { |
||
| 199 | User::updateUserRole( |
||
| 200 | $editedUser->id, |
||
| 201 | (int) $request->input('role'), // Cast to integer |
||
| 202 | true, // Apply promotions |
||
| 203 | $stackRole, // Stack role if requested |
||
| 204 | $changedBy, |
||
| 205 | $originalRoleChangeDate // Pass original expiry for history |
||
| 206 | ); |
||
| 207 | $editedUser->refresh(); |
||
| 208 | } elseif (!$roleChanged && $request->input('role') !== null) { |
||
| 209 | // Role isn't changing, but we should still apply promotions if there are any |
||
| 210 | // This handles the case where admin extends expiry date for existing role |
||
| 211 | User::updateUserRole( |
||
| 212 | $editedUser->id, |
||
| 213 | (int) $request->input('role'), // Same role |
||
| 214 | true, // Apply promotions |
||
| 215 | false, // Don't stack (not changing role) |
||
| 216 | $changedBy, |
||
| 217 | $originalRoleChangeDate |
||
| 218 | ); |
||
| 219 | $editedUser->refresh(); |
||
| 220 | } |
||
| 221 | |||
| 222 | // Update user basic information (but NOT the role - it's handled above) |
||
| 223 | // Use current role to avoid overwriting |
||
| 224 | $ret = User::updateUser( |
||
| 225 | $editedUser->id, |
||
| 226 | $request->input('username'), |
||
| 227 | $request->input('email'), |
||
| 228 | $editedUser->grabs, |
||
| 229 | $editedUser->roles_id, // Use current role, not the request role |
||
| 230 | $request->input('notes'), |
||
| 231 | $request->input('invites'), |
||
| 232 | ($request->has('movieview') ? 1 : 0), |
||
| 233 | ($request->has('musicview') ? 1 : 0), |
||
| 234 | ($request->has('gameview') ? 1 : 0), |
||
| 235 | ($request->has('xxxview') ? 1 : 0), |
||
| 236 | ($request->has('consoleview') ? 1 : 0), |
||
| 237 | ($request->has('bookview') ? 1 : 0) |
||
| 238 | ); |
||
| 239 | |||
| 240 | if ($request->input('password') !== null) { |
||
| 241 | User::updatePassword($editedUser->id, $request->input('password')); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | if ($ret >= 0) { |
||
| 246 | return redirect()->to('admin/user-list'); |
||
| 247 | } |
||
| 248 | |||
| 249 | $error = match ($ret) { |
||
| 250 | User::ERR_SIGNUP_BADUNAME => 'Bad username. Try a better one.', |
||
| 251 | User::ERR_SIGNUP_BADPASS => 'Bad password. Try a longer one.', |
||
| 252 | User::ERR_SIGNUP_BADEMAIL => 'Bad email.', |
||
| 253 | User::ERR_SIGNUP_UNAMEINUSE => 'Username in use.', |
||
| 254 | User::ERR_SIGNUP_EMAILINUSE => 'Email in use.', |
||
| 255 | default => 'Unknown save error.', |
||
| 256 | }; |
||
| 257 | $user += [ |
||
| 258 | 'id' => $request->input('id'), |
||
| 259 | 'username' => $request->input('username'), |
||
| 260 | 'email' => $request->input('email'), |
||
| 261 | 'role' => $request->input('role'), |
||
| 262 | 'notes' => $request->input('notes'), |
||
| 263 | ]; |
||
| 264 | break; |
||
| 265 | case 'view': |
||
| 266 | default: |
||
| 267 | if ($request->has('id')) { |
||
| 268 | $title = 'User Edit'; |
||
| 269 | $id = $request->input('id'); |
||
| 270 | $user = User::find($id); |
||
| 271 | |||
| 272 | // Add daily API and download counts |
||
| 273 | if ($user) { |
||
| 274 | try { |
||
| 275 | $user->daily_api_count = UserRequest::getApiRequests($user->id); |
||
| 276 | $user->daily_download_count = UserDownload::getDownloadRequests($user->id); |
||
| 277 | } catch (\Exception $e) { |
||
| 278 | $user->daily_api_count = 0; |
||
| 279 | $user->daily_download_count = 0; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | break; |
||
| 285 | } |
||
| 286 | |||
| 287 | $this->viewData = array_merge($this->viewData, [ |
||
| 288 | 'yesno_ids' => [1, 0], |
||
| 289 | 'yesno_names' => ['Yes', 'No'], |
||
| 290 | 'role_ids' => array_keys($roles), |
||
| 291 | 'role_names' => $roles, |
||
| 292 | 'user' => $user, |
||
| 293 | 'error' => $error, |
||
| 294 | 'title' => $title, |
||
| 295 | 'meta_title' => $meta_title, |
||
| 296 | ]); |
||
| 297 | |||
| 298 | return view('admin.users.edit', $this->viewData); |
||
| 299 | } |
||
| 346 |