Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
42 / 45 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ReportHelper | |
93.33% |
42 / 45 |
|
83.33% |
5 / 6 |
28.23 | |
0.00% |
0 / 1 |
| withMaxAndAverage | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
11 | |||
| formatTimeValue | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
3.47 | |||
| extractSelectedScopes | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| extractDateRange | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| isValidDateFormat | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getYearsForDateRange | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsstatistic\Helper; |
| 4 | |
| 5 | use DateTime; |
| 6 | |
| 7 | class ReportHelper |
| 8 | { |
| 9 | public static function withMaxAndAverage($entity, $targetKey) |
| 10 | { |
| 11 | foreach ($entity->data as $date => $dateItems) { |
| 12 | $maxima = 0; |
| 13 | $total = 0; |
| 14 | $count = 0; |
| 15 | foreach ($dateItems as $hourItems) { |
| 16 | if (is_array($hourItems)) { // Check if $hourItems is an array |
| 17 | foreach ($hourItems as $key => $value) { |
| 18 | if (is_numeric($value) && $targetKey == $key && 0 < $value) { |
| 19 | $total += $value; |
| 20 | $count += 1; |
| 21 | $maxima = ($maxima > $value) ? $maxima : $value; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | $entity->data[$date]['max_' . $targetKey] = $maxima; |
| 27 | $entity->data[$date]['average_' . $targetKey] = (! $total || ! $count) ? 0 : $total / $count; |
| 28 | } |
| 29 | return $entity; |
| 30 | } |
| 31 | |
| 32 | public static function formatTimeValue($value) |
| 33 | { |
| 34 | if (!is_numeric($value)) { |
| 35 | return $value; |
| 36 | } |
| 37 | $minutes = floor($value); |
| 38 | $seconds = round(($value - $minutes) * 60); |
| 39 | if ($seconds >= 60) { |
| 40 | $minutes += 1; |
| 41 | $seconds = 0; |
| 42 | } |
| 43 | return sprintf('%02d:%02d', $minutes, $seconds); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Extract selected scope IDs from request parameters |
| 48 | */ |
| 49 | public function extractSelectedScopes(array $scopes): array |
| 50 | { |
| 51 | if (!empty($scopes)) { |
| 52 | $validScopes = array_filter($scopes, function ($scopeId) { |
| 53 | return is_numeric($scopeId) && $scopeId > 0; |
| 54 | }); |
| 55 | |
| 56 | if (!empty($validScopes)) { |
| 57 | return array_map('intval', $validScopes); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return []; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Extract and validate date range from request parameters |
| 66 | */ |
| 67 | public function extractDateRange(?string $fromDate, ?string $toDate): ?array |
| 68 | { |
| 69 | if ($fromDate && $toDate && $this->isValidDateFormat($fromDate) && $this->isValidDateFormat($toDate)) { |
| 70 | return [ |
| 71 | 'from' => $fromDate, |
| 72 | 'to' => $toDate |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Validate if the given string is a valid date format (YYYY-MM-DD) |
| 81 | */ |
| 82 | public function isValidDateFormat(string $date): bool |
| 83 | { |
| 84 | if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | $dateTime = DateTime::createFromFormat('Y-m-d', $date); |
| 89 | return $dateTime && $dateTime->format('Y-m-d') === $date; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get all years that need to be fetched for a date range |
| 94 | */ |
| 95 | public function getYearsForDateRange(string $fromDate, string $toDate): array |
| 96 | { |
| 97 | $fromYear = (int) substr($fromDate, 0, 4); |
| 98 | $toYear = (int) substr($toDate, 0, 4); |
| 99 | |
| 100 | $years = []; |
| 101 | for ($year = $fromYear; $year <= $toYear; $year++) { |
| 102 | $years[] = $year; |
| 103 | } |
| 104 | |
| 105 | return $years; |
| 106 | } |
| 107 | } |