| Conditions | 6 |
| Paths | 17 |
| Total Lines | 57 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 26 | public function __invoke($cid): JsonResponse |
||
| 27 | { |
||
| 28 | $courseId = (int) $cid; |
||
| 29 | |||
| 30 | $course = $this->courseRepository->find($courseId); |
||
| 31 | if (null === $course) { |
||
| 32 | return new JsonResponse(['error' => 'Course not found'], 404); |
||
| 33 | } |
||
| 34 | |||
| 35 | // Resolve quota in MB safely (avoid "($x * ...) ?? fallback"). |
||
| 36 | $quotaMb = (int) ($course->getDiskQuota() ?? 0); |
||
| 37 | if ($quotaMb <= 0) { |
||
| 38 | $quotaMb = self::DEFAULT_QUOTA_MB; |
||
| 39 | } |
||
| 40 | |||
| 41 | $totalQuotaBytes = $quotaMb * 1024 * 1024; |
||
| 42 | |||
| 43 | // Compute usage using repository logic (deduplicated). |
||
| 44 | $usage = $this->documentRepository->getDocumentUsageBreakdownByCourse($course); |
||
| 45 | |||
| 46 | $bytesCourse = (int) ($usage['course'] ?? 0); |
||
| 47 | $bytesSessions = (int) ($usage['sessions'] ?? 0); |
||
| 48 | $bytesGroups = (int) ($usage['groups'] ?? 0); |
||
| 49 | |||
| 50 | $usedBytes = (int) ($usage['used'] ?? ($bytesCourse + $bytesSessions + $bytesGroups)); |
||
| 51 | |||
| 52 | // Keep the pie meaningful even when used > quota. |
||
| 53 | $denomBytes = max($totalQuotaBytes, $usedBytes, 1); |
||
| 54 | |||
| 55 | $availableBytes = max($totalQuotaBytes - $usedBytes, 0); |
||
| 56 | |||
| 57 | $labels = []; |
||
| 58 | $data = []; |
||
| 59 | |||
| 60 | if ($bytesCourse > 0) { |
||
| 61 | $labels[] = get_lang('Course').' ('.$this->formatBytes($bytesCourse).')'; |
||
| 62 | $data[] = $this->pct($bytesCourse, $denomBytes); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($bytesSessions > 0) { |
||
| 66 | $labels[] = get_lang('Session').' ('.$this->formatBytes($bytesSessions).')'; |
||
| 67 | $data[] = $this->pct($bytesSessions, $denomBytes); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($bytesGroups > 0) { |
||
| 71 | $labels[] = get_lang('Group').' ('.$this->formatBytes($bytesGroups).')'; |
||
| 72 | $data[] = $this->pct($bytesGroups, $denomBytes); |
||
| 73 | } |
||
| 74 | |||
| 75 | $labels[] = get_lang('Available space').' ('.$this->formatBytes($availableBytes).')'; |
||
| 76 | $data[] = $this->pct($availableBytes, $denomBytes); |
||
| 77 | |||
| 78 | return new JsonResponse([ |
||
| 79 | 'datasets' => [ |
||
| 80 | ['data' => $data], |
||
| 81 | ], |
||
| 82 | 'labels' => $labels, |
||
| 83 | ]); |
||
| 111 |