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