Passed
Pull Request — 0.3 (#20)
by jean
03:19
created

StatsCalculator::isEventfull()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 4
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ProcessBundle\StatDumper;
4
5
class StatsCalculator
6
{
7 1
    public function calcul(array $dataCollected): array
8
    {
9 1
        $calculatedStats = [];
10
11 1
        foreach ($dataCollected as $class => $stat) {
12 1
            $calculatedStats[$class] = $this->calculForOneStep($stat);
13
        }
14
15
        return [
16 1
            'stats' => $calculatedStats,
17 1
            'total' => $this->calculTotals($calculatedStats),
18
        ];
19
    }
20
21 1
    private function isEventfull($times)
22
    {
23 1
        $moved = false;
24 1
        $last = null;
25 1
        foreach ($times as $time) {
26 1
            $time = (int) $time;
27 1
            if (null !== $last && $last != $time) {
28 1
                $moved = true;
29 1
                break;
30
            }
31 1
            $last = $time;
32
        }
33
34 1
        return $moved;
35
    }
36
37 1
    private function calculForOneStep($stat)
38
    {
39 1
        $calculatedStat = [];
40 1
        $calculatedStat['global'] = array_sum($stat['time']);
41
42 1
        $calculatedStat['tendance'] = (!$this->isEventfull($stat['time'])) ? '~~~' : '/\/';
43 1
        $calculatedStat['global_wait'] = array_sum($stat['wait']);
44 1
        asort($stat['time'], SORT_NUMERIC);
45 1
        $calculatedStat['best_times'] = array_slice($stat['time'], 0, 3, true);
46 1
        arsort($stat['time'], SORT_NUMERIC);
47 1
        $calculatedStat['bad_times'] = array_slice($stat['time'], 0, 3, true);
48 1
        $calculatedStat['potential_rate'] = '??';
49 1
        $calculatedStat['position'] = $stat['position'];
50 1
        $calculatedStat['count_iteration'] = count($stat['time']);
51
52 1
        return $calculatedStat;
53
    }
54
55 1
    private function calculTotals($calculatedStats)
56
    {
57
        return [
58 1
            'global' => array_sum(array_column($calculatedStats, 'global')),
59
            'best_times' => [0, 0, 0],
60
            'bad_times' => [0, 0, 0],
61 1
            'global_wait' => array_sum(array_column($calculatedStats, 'global_wait')),
62 1
            'position' => ' ',
63 1
            'potential_rate' => '??',
64
        ];
65
    }
66
}
67