Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.67% covered (warning)
56.67%
34 / 60
30.00% covered (danger)
30.00%
3 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
SysCheckReportFile
56.67% covered (warning)
56.67%
34 / 60
30.00% covered (danger)
30.00%
3 / 10
103.23
0.00% covered (danger)
0.00%
0 / 1
 __construct
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
10.20
 addEntry
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getReport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCheckLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCheckId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFlatReport
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 getDigest
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getValueIfExists
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getStatistics
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 getFileName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4
5
6class SysCheckReportFile {
7
8    const reportSections = [
9        'envData', 'netData', 'questData', 'unitData', // deprecated section names to maintain backwards compatibility
10        'fileData',
11        'environment', 'network', 'questionnaire', 'unit'
12    ];
13
14    private $report = [];
15    private $checkId = '--';
16    private $checkLabel = '--';
17    private $fileName = '';
18    private $title = '--';
19
20    function __construct($reportFilePath) {
21
22        if (!is_file($reportFilePath) or !(strtoupper(substr($reportFilePath, -5)) == '.JSON')) {
23            throw new HttpError("No SysCheck-Report File: $reportFilePath", 500);
24        }
25
26        $file = file_get_contents($reportFilePath);
27
28        if ($file === false) {
29            throw new HttpError("Could not read File: $reportFilePath", 500);
30        }
31
32        $this->report = JSON::decode($file, true);
33
34        if (isset($this->report['checkId']) and $this->report['checkId']) {
35            $this->checkId = $this->report['checkId'];
36        }
37
38        if (isset($this->report['checkLabel']) and $this->report['checkLabel']) {
39            $this->checkLabel = $this->report['checkLabel'];
40        }
41
42        if (isset($this->report['title']) and $this->report['title']) {
43            $this->title = $this->report['title'];
44        }
45
46        $this->fileName = basename($reportFilePath);
47
48        $this->addEntry('fileData', 'date', 'DatumTS', (string) FileTime::modification($reportFilePath));
49        $this->addEntry('fileData', 'datestr', 'Datum', TimeStamp::toSQLFormat(FileTime::modification($reportFilePath)));
50        $this->addEntry('fileData', 'filename', 'FileName', basename($reportFilePath));
51    }
52
53
54    function addEntry(string $section, string $id, string $label, string $value): void {
55
56        if (!isset($this->report[$section])) {
57            $this->report[$section] = [];
58        }
59
60        $this->report[$section][] = [
61            'id' => $id,
62            'label' => $label,
63            'value' => $value
64        ];
65    }
66
67
68    function getReport(): array {
69
70        return $this->report;
71    }
72
73
74    function getCheckLabel(): string {
75
76        return $this->checkLabel;
77    }
78
79
80    function getCheckId(): string {
81
82        return $this->checkId;
83    }
84
85
86    function getFlatReport(): array {
87
88        $flatReport = [
89            'Titel' => $this->title,
90            'SysCheck-Id' => $this->checkId,
91            'SysCheck' => $this->checkLabel,
92            'Responses' => $this->report['responses'] ? json_encode($this->report['responses']): ''
93        ];
94
95        foreach (SysCheckReportFile::reportSections as $section) {
96
97            if (!isset($this->report[$section])) {
98                continue;
99            }
100
101            foreach ($this->report[$section] as $id => $entry) {
102                $flatReport[$entry['label']] = $entry['value'];
103            }
104        }
105
106        return $flatReport;
107    }
108
109
110    // TODO unit Test
111    function getDigest(): array {
112
113        return [
114            'os' =>  $this->getValueIfExists('environment', 'Betriebsystem') . ' '
115                . $this->getValueIfExists('environment', 'Betriebsystem-Version'),
116            'browser' => $this->getValueIfExists('environment', 'Browser') . ' '
117                . $this->getValueIfExists('environment', 'Browser-Version'),
118        ];
119    }
120
121
122    // TODO unit Test
123    // TODO use ids instead of labels (but ids has to be set in FE)
124    private function getValueIfExists(string $section, string $field, string $default = '') {
125
126        $sectionEntries = isset($this->report[$section]) ? $this->report[$section] : [];
127
128        foreach ($sectionEntries as $entry) {
129
130            if ($entry['label'] == $field) {
131                return $entry['value'];
132            }
133        }
134
135        return $default;
136    }
137
138
139    // TODO unit Test
140    static function getStatistics(array $reportSet): array {
141
142        $digests = array_map(function(SysCheckReportFile $report) {return $report->getDigest();}, $reportSet);
143
144        return array_reduce($digests, function ($agg, $item) {
145            foreach ($item as $key => $value) {
146                if (!isset($agg[$key])) {
147                    $agg[$key] = [];
148                }
149
150                if (!isset($agg[$key][$value])) {
151                    $agg[$key][$value] = 0;
152                }
153                $agg[$key][$value] += 1;
154            }
155            return $agg;
156        }, []);
157    }
158
159
160    public function getFileName(): string {
161
162        return $this->fileName;
163    }
164
165}