| Conditions | 5 |
| Paths | 6 |
| Total Lines | 57 |
| Code Lines | 32 |
| 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 |
||
| 32 | public function handle(): int |
||
| 33 | { |
||
| 34 | $days = (int) $this->option('days'); |
||
| 35 | $force = $this->option('force'); |
||
| 36 | |||
| 37 | $this->info("Backfilling user activity stats for the last {$days} days..."); |
||
| 38 | |||
| 39 | $startDate = Carbon::now()->subDays($days - 1)->startOfDay(); |
||
|
|
|||
| 40 | $progressBar = $this->output->createProgressBar($days); |
||
| 41 | |||
| 42 | $statsCollected = 0; |
||
| 43 | $statsSkipped = 0; |
||
| 44 | |||
| 45 | for ($i = $days - 1; $i >= 0; $i--) { |
||
| 46 | $date = Carbon::now()->subDays($i)->format('Y-m-d'); |
||
| 47 | |||
| 48 | // Check if stats already exist for this date |
||
| 49 | if (! $force && UserActivityStat::where('stat_date', $date)->exists()) { |
||
| 50 | $statsSkipped++; |
||
| 51 | $progressBar->advance(); |
||
| 52 | |||
| 53 | continue; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Count downloads for the date |
||
| 57 | $downloadsCount = UserDownload::query() |
||
| 58 | ->whereRaw('DATE(timestamp) = ?', [$date]) |
||
| 59 | ->count(); |
||
| 60 | |||
| 61 | // Count API hits for the date |
||
| 62 | $apiHitsCount = UserRequest::query() |
||
| 63 | ->whereRaw('DATE(timestamp) = ?', [$date]) |
||
| 64 | ->count(); |
||
| 65 | |||
| 66 | // Store or update the stats |
||
| 67 | UserActivityStat::updateOrCreate( |
||
| 68 | ['stat_date' => $date], |
||
| 69 | [ |
||
| 70 | 'downloads_count' => $downloadsCount, |
||
| 71 | 'api_hits_count' => $apiHitsCount, |
||
| 72 | ] |
||
| 73 | ); |
||
| 74 | |||
| 75 | $statsCollected++; |
||
| 76 | $progressBar->advance(); |
||
| 77 | } |
||
| 78 | |||
| 79 | $progressBar->finish(); |
||
| 80 | $this->newLine(2); |
||
| 81 | |||
| 82 | $this->info('Backfill complete!'); |
||
| 83 | $this->info("Stats collected: {$statsCollected}"); |
||
| 84 | if ($statsSkipped > 0) { |
||
| 85 | $this->info("Stats skipped (already existed): {$statsSkipped}"); |
||
| 86 | } |
||
| 87 | |||
| 88 | return Command::SUCCESS; |
||
| 89 | } |
||
| 91 |