Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.36% covered (warning)
86.36%
19 / 22
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportHelper
86.36% covered (warning)
86.36%
19 / 22
50.00% covered (danger)
50.00%
1 / 2
14.50
0.00% covered (danger)
0.00%
0 / 1
 withMaxAndAverage
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
11
 formatTimeValue
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
3.47
1<?php
2
3namespace BO\Zmsstatistic\Helper;
4
5class ReportHelper
6{
7    public static function withMaxAndAverage($entity, $targetKey)
8    {
9        foreach ($entity->data as $date => $dateItems) {
10            $maxima = 0;
11            $total = 0;
12            $count = 0;
13            foreach ($dateItems as $hourItems) {
14                if (is_array($hourItems)) { // Check if $hourItems is an array
15                    foreach ($hourItems as $key => $value) {
16                        if (is_numeric($value) && $targetKey == $key && 0 < $value) {
17                            $total += $value;
18                            $count += 1;
19                            $maxima = ($maxima > $value) ? $maxima : $value;
20                        }
21                    }
22                }
23            }
24            $entity->data[$date]['max_' . $targetKey] = $maxima;
25            $entity->data[$date]['average_' . $targetKey] = (! $total || ! $count) ? 0 : $total / $count;
26        }
27        return $entity;
28    }
29
30    public static function formatTimeValue($value)
31    {
32        if (!is_numeric($value)) {
33            return $value;
34        }
35        $minutes = floor($value);
36        $seconds = round(($value - $minutes) * 60);
37        if ($seconds >= 60) {
38            $minutes += 1;
39            $seconds = 0;
40        }
41        return sprintf('%02d:%02d', $minutes, $seconds);
42    }
43}