| Conditions | 11 |
| Paths | 8 |
| Total Lines | 84 |
| Code Lines | 62 |
| 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 |
||
| 160 | public function statistics(Request $request): View |
||
| 161 | { |
||
| 162 | // Get date range filter (default to last 30 days) |
||
| 163 | $endDate = Carbon::now(); |
||
| 164 | $startDate = Carbon::now()->subDays(30); |
||
| 165 | |||
| 166 | if ($request->has('start_date') && $request->has('end_date')) { |
||
| 167 | $startDate = Carbon::parse($request->input('start_date')); |
||
| 168 | $endDate = Carbon::parse($request->input('end_date')); |
||
| 169 | } elseif ($request->has('period')) { |
||
| 170 | switch ($request->input('period')) { |
||
| 171 | case '7days': |
||
| 172 | $startDate = Carbon::now()->subDays(7); |
||
| 173 | break; |
||
| 174 | case '30days': |
||
| 175 | $startDate = Carbon::now()->subDays(30); |
||
| 176 | break; |
||
| 177 | case '90days': |
||
| 178 | $startDate = Carbon::now()->subDays(90); |
||
| 179 | break; |
||
| 180 | case 'year': |
||
| 181 | $startDate = Carbon::now()->subYear(); |
||
| 182 | break; |
||
| 183 | case 'all': |
||
| 184 | $startDate = null; |
||
| 185 | break; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | // Get all promotions with their statistics |
||
| 190 | $promotions = RolePromotion::withCount(['statistics' => function ($query) use ($startDate, $endDate) { |
||
| 191 | if ($startDate) { |
||
| 192 | $query->whereBetween('applied_at', [$startDate, $endDate]); |
||
| 193 | } |
||
| 194 | }]) |
||
| 195 | ->with(['statistics' => function ($query) use ($startDate, $endDate) { |
||
| 196 | if ($startDate) { |
||
| 197 | $query->whereBetween('applied_at', [$startDate, $endDate]); |
||
| 198 | } |
||
| 199 | $query->with(['user', 'role']); |
||
| 200 | }]) |
||
| 201 | ->get(); |
||
| 202 | |||
| 203 | // Calculate overall statistics |
||
| 204 | $overallStats = [ |
||
| 205 | 'total_promotions' => $promotions->count(), |
||
| 206 | 'active_promotions' => $promotions->where('is_active', true)->count(), |
||
| 207 | 'total_applications' => $promotions->sum('statistics_count'), |
||
| 208 | 'unique_users' => RolePromotionStat::query() |
||
| 209 | ->when($startDate, fn($q) => $q->whereBetween('applied_at', [$startDate, $endDate])) |
||
| 210 | ->distinct('user_id') |
||
| 211 | ->count('user_id'), |
||
| 212 | 'total_days_added' => RolePromotionStat::query() |
||
| 213 | ->when($startDate, fn($q) => $q->whereBetween('applied_at', [$startDate, $endDate])) |
||
| 214 | ->sum('days_added'), |
||
| 215 | ]; |
||
| 216 | |||
| 217 | // Get top promotions by usage |
||
| 218 | $topPromotions = $promotions->sortByDesc('statistics_count')->take(5); |
||
| 219 | |||
| 220 | // Get recent activity |
||
| 221 | $recentActivity = RolePromotionStat::with(['user', 'promotion', 'role']) |
||
| 222 | ->when($startDate, fn($q) => $q->whereBetween('applied_at', [$startDate, $endDate])) |
||
| 223 | ->latest('applied_at') |
||
| 224 | ->limit(10) |
||
| 225 | ->get(); |
||
| 226 | |||
| 227 | // Get statistics by role |
||
| 228 | $statsByRole = RolePromotionStat::query() |
||
| 229 | ->selectRaw('role_id, COUNT(*) as total_upgrades, SUM(days_added) as total_days, COUNT(DISTINCT user_id) as unique_users') |
||
| 230 | ->when($startDate, fn($q) => $q->whereBetween('applied_at', [$startDate, $endDate])) |
||
| 231 | ->groupBy('role_id') |
||
| 232 | ->with('role') |
||
| 233 | ->get(); |
||
| 234 | |||
| 235 | return view('admin.promotions.statistics', [ |
||
| 236 | 'promotions' => $promotions, |
||
| 237 | 'overallStats' => $overallStats, |
||
| 238 | 'topPromotions' => $topPromotions, |
||
| 239 | 'recentActivity' => $recentActivity, |
||
| 240 | 'statsByRole' => $statsByRole, |
||
| 241 | 'startDate' => $startDate, |
||
| 242 | 'endDate' => $endDate, |
||
| 243 | 'selectedPeriod' => $request->input('period', '30days'), |
||
| 244 | ]); |
||
| 313 |