Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
12.24% |
6 / 49 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
SysChecksFolder | |
12.24% |
6 / 49 |
|
16.67% |
1 / 6 |
236.96 | |
0.00% |
0 / 1 |
findAvailableSysChecks | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getSysCheckReportList | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
collectSysCheckReports | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
getSysCheckReportsPath | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
deleteSysCheckReports | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
saveSysCheckReport | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | |
5 | class SysChecksFolder extends Workspace { |
6 | public function findAvailableSysChecks(): array { |
7 | $sysChecks = []; |
8 | |
9 | foreach (Folder::glob($this->getOrCreateSubFolderPath('SysCheck'), "*.[xX][mM][lL]") as $fullFilePath) { |
10 | $xFile = new XMLFileSysCheck($fullFilePath); |
11 | |
12 | if ($xFile->isValid()) { |
13 | $sysChecks[] = $xFile; |
14 | } |
15 | } |
16 | |
17 | return $sysChecks; |
18 | } |
19 | |
20 | // TODO unit test |
21 | public function getSysCheckReportList(): array { |
22 | $allReports = $this->collectSysCheckReports(); |
23 | |
24 | $allReportsByCheckIds = array_reduce($allReports, function($agg, SysCheckReportFile $report) { |
25 | if (!isset($agg[$report->getCheckId()])) { |
26 | $agg[$report->getCheckId()] = [$report]; |
27 | } else { |
28 | $agg[$report->getCheckId()][] = $report; |
29 | } |
30 | return $agg; |
31 | }, []); |
32 | |
33 | return array_map(function(array $reportSet, string $checkId) { |
34 | return [ |
35 | 'id' => $checkId, |
36 | 'count' => count($reportSet), |
37 | 'label' => $reportSet[0]->getCheckLabel(), |
38 | 'details' => SysCheckReportFile::getStatistics($reportSet) |
39 | ]; |
40 | }, $allReportsByCheckIds, array_keys($allReportsByCheckIds)); |
41 | } |
42 | |
43 | // TODO unit test |
44 | |
45 | /** |
46 | * @return SysCheckReportFile[] |
47 | */ |
48 | public function collectSysCheckReports(array $filterCheckIds = null): array { |
49 | $reportFolderName = $this->getSysCheckReportsPath(); |
50 | $reportDir = opendir($reportFolderName); |
51 | $reports = []; |
52 | |
53 | while (($reportFileName = readdir($reportDir)) !== false) { |
54 | $reportFilePath = $reportFolderName . '/' . $reportFileName; |
55 | |
56 | if (!is_file($reportFilePath) or !(strtoupper(substr($reportFileName, -5)) == '.JSON')) { |
57 | continue; |
58 | } |
59 | |
60 | $report = new SysCheckReportFile($reportFilePath); |
61 | |
62 | if (($filterCheckIds === null) or (in_array($report->getCheckId(), $filterCheckIds))) { |
63 | $reports[] = $report; |
64 | } |
65 | } |
66 | |
67 | return $reports; |
68 | } |
69 | |
70 | private function getSysCheckReportsPath(): string { |
71 | $sysCheckPath = $this->workspacePath . '/SysCheck'; |
72 | if (!file_exists($sysCheckPath)) { |
73 | mkdir($sysCheckPath); |
74 | } |
75 | $sysCheckReportsPath = $sysCheckPath . '/reports'; |
76 | if (!file_exists($sysCheckReportsPath)) { |
77 | mkdir($sysCheckReportsPath); |
78 | } |
79 | return $sysCheckReportsPath; |
80 | } |
81 | |
82 | // TODO unit test |
83 | public function deleteSysCheckReports(array $checkIds): FileDeletionReport { |
84 | $reports = $this->collectSysCheckReports($checkIds); |
85 | |
86 | $deletionReport = new FileDeletionReport(); |
87 | |
88 | foreach ($reports as $report) { |
89 | /* @var SysCheckReportFile $report */ |
90 | $fullPath = "$this->workspacePath/SysCheck/reports/{$report->getFileName()}"; |
91 | $fieldName = $this->deleteFileFromFs($fullPath); |
92 | $deletionReport->$fieldName[] = $report->getCheckId(); |
93 | } |
94 | |
95 | return $deletionReport; |
96 | } |
97 | |
98 | // TODO unit test |
99 | public function saveSysCheckReport(SysCheckReport $report): void { |
100 | $reportFilename = $this->getSysCheckReportsPath() . '/' . uniqid('report_', true) . '.json'; |
101 | |
102 | if (!file_put_contents($reportFilename, json_encode((array) $report))) { |
103 | throw new Exception("Could not write to file `$reportFilename`"); |
104 | } |
105 | } |
106 | |
107 | } |