| Conditions | 5 |
| Paths | 6 |
| Total Lines | 55 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 56 | protected function backfillDaily(bool $force): int |
||
| 57 | { |
||
| 58 | $days = (int) $this->option('days'); |
||
| 59 | |||
| 60 | $this->info("Backfilling daily user activity stats for the last {$days} days..."); |
||
| 61 | |||
| 62 | $progressBar = $this->output->createProgressBar($days); |
||
| 63 | |||
| 64 | $statsCollected = 0; |
||
| 65 | $statsSkipped = 0; |
||
| 66 | |||
| 67 | for ($i = $days - 1; $i >= 0; $i--) { |
||
| 68 | $date = Carbon::now()->subDays($i)->format('Y-m-d'); |
||
| 69 | |||
| 70 | // Check if stats already exist for this date |
||
| 71 | if (! $force && UserActivityStat::where('stat_date', $date)->exists()) { |
||
| 72 | $statsSkipped++; |
||
| 73 | $progressBar->advance(); |
||
| 74 | |||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | // Count downloads for the date |
||
| 79 | $downloadsCount = UserDownload::query() |
||
| 80 | ->whereRaw('DATE(timestamp) = ?', [$date]) |
||
| 81 | ->count(); |
||
| 82 | |||
| 83 | // Count API hits for the date |
||
| 84 | $apiHitsCount = UserRequest::query() |
||
| 85 | ->whereRaw('DATE(timestamp) = ?', [$date]) |
||
| 86 | ->count(); |
||
| 87 | |||
| 88 | // Store or update the stats |
||
| 89 | UserActivityStat::updateOrCreate( |
||
| 90 | ['stat_date' => $date], |
||
| 91 | [ |
||
| 92 | 'downloads_count' => $downloadsCount, |
||
| 93 | 'api_hits_count' => $apiHitsCount, |
||
| 94 | ] |
||
| 95 | ); |
||
| 96 | |||
| 97 | $statsCollected++; |
||
| 98 | $progressBar->advance(); |
||
| 99 | } |
||
| 100 | |||
| 101 | $progressBar->finish(); |
||
| 102 | $this->newLine(2); |
||
| 103 | |||
| 104 | $this->info('Daily backfill complete!'); |
||
| 105 | $this->info("Stats collected: {$statsCollected}"); |
||
| 106 | if ($statsSkipped > 0) { |
||
| 107 | $this->info("Stats skipped (already existed): {$statsSkipped}"); |
||
| 108 | } |
||
| 109 | |||
| 110 | return Command::SUCCESS; |
||
| 111 | } |
||
| 177 |