| Conditions | 12 |
| Paths | 38 |
| Total Lines | 83 |
| Code Lines | 56 |
| 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 |
||
| 212 | protected function activatePendingRoles(): int |
||
| 213 | { |
||
| 214 | $this->warn('This command manually activates pending roles that are scheduled for activation.'); |
||
| 215 | $this->warn('Normally, this is done automatically by the scheduled task.'); |
||
| 216 | $this->newLine(); |
||
| 217 | |||
| 218 | if (!$this->confirm('Do you want to continue?')) { |
||
| 219 | $this->info('Operation cancelled.'); |
||
| 220 | return 0; |
||
| 221 | } |
||
| 222 | |||
| 223 | $now = Carbon::now(); |
||
| 224 | $users = User::whereNotNull('pending_roles_id') |
||
| 225 | ->whereNotNull('pending_role_start_date') |
||
| 226 | ->where('pending_role_start_date', '<=', $now) |
||
| 227 | ->get(); |
||
| 228 | |||
| 229 | if ($users->isEmpty()) { |
||
| 230 | $this->info('No pending roles ready for activation.'); |
||
| 231 | return 0; |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->info(sprintf('Found %d pending roles ready for activation:', $users->count())); |
||
| 235 | $this->newLine(); |
||
| 236 | |||
| 237 | $activated = 0; |
||
| 238 | $failed = 0; |
||
| 239 | |||
| 240 | foreach ($users as $user) { |
||
| 241 | $pendingRole = $user->getPendingRole(); |
||
| 242 | $oldRole = $user->roles->first(); |
||
| 243 | |||
| 244 | try { |
||
| 245 | // Activate the pending role |
||
| 246 | $user->update([ |
||
| 247 | 'roles_id' => $user->pending_roles_id, |
||
| 248 | 'pending_roles_id' => null, |
||
| 249 | 'pending_role_start_date' => null, |
||
| 250 | ]); |
||
| 251 | |||
| 252 | if ($pendingRole) { |
||
| 253 | $user->syncRoles([$pendingRole->name]); |
||
| 254 | } |
||
| 255 | |||
| 256 | // Record in history |
||
| 257 | UserRoleHistory::recordRoleChange( |
||
| 258 | userId: $user->id, |
||
| 259 | oldRoleId: $oldRole ? $oldRole->id : null, |
||
| 260 | newRoleId: $pendingRole ? $pendingRole->id : $user->roles_id, |
||
| 261 | oldExpiryDate: null, |
||
| 262 | newExpiryDate: null, |
||
| 263 | effectiveDate: $now, |
||
| 264 | isStacked: true, |
||
| 265 | changeReason: 'manual_activation', |
||
| 266 | changedBy: null |
||
| 267 | ); |
||
| 268 | |||
| 269 | $this->info(sprintf( |
||
| 270 | '✓ Activated %s -> %s for user: %s', |
||
| 271 | $oldRole ? $oldRole->name : 'None', |
||
| 272 | $pendingRole ? $pendingRole->name : 'Unknown', |
||
| 273 | $user->username |
||
| 274 | )); |
||
| 275 | |||
| 276 | $activated++; |
||
| 277 | } catch (\Exception $e) { |
||
| 278 | $this->error(sprintf( |
||
| 279 | '✗ Failed to activate role for user %s: %s', |
||
| 280 | $user->username, |
||
| 281 | $e->getMessage() |
||
| 282 | )); |
||
| 283 | $failed++; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | $this->newLine(); |
||
| 288 | $this->info("Activation complete:"); |
||
| 289 | $this->info(" Successful: {$activated}"); |
||
| 290 | if ($failed > 0) { |
||
| 291 | $this->error(" Failed: {$failed}"); |
||
| 292 | } |
||
| 293 | |||
| 294 | return $failed > 0 ? 1 : 0; |
||
| 295 | } |
||
| 315 |