Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.59% |
65 / 68 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
ReviewReportOutput | |
95.59% |
65 / 68 |
|
66.67% |
4 / 6 |
24 | |
0.00% |
0 / 1 |
generate | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
transformReviewData | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
extractCategoryKeys | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
fillCategories | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
8.03 | |||
returnArrayWithSeperateEntry | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
2.06 | |||
generateCsvReportData | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | class ReviewReportOutput extends Report { |
6 | |
7 | public function generate(bool $useNewVersion = false): bool { |
8 | $this->useNewVersion = $useNewVersion; |
9 | |
10 | $adminDAO = new AdminDAO(); |
11 | $reviewData = $adminDAO->getReviewReportData($this->workspaceId, $this->dataIds); |
12 | $reviewData = $this->transformReviewData($reviewData); |
13 | |
14 | if (empty($reviewData)) { |
15 | return false; |
16 | |
17 | } else { |
18 | $this->reportData = $reviewData; |
19 | |
20 | if ($this->format == ReportFormat::CSV) { |
21 | $this->csvReportData = $this->generateCsvReportData($reviewData); |
22 | } |
23 | } |
24 | |
25 | return true; |
26 | } |
27 | |
28 | private function transformReviewData(array $reviewData): array { |
29 | $transformedReviewData = []; |
30 | $categoryKeys = $this->extractCategoryKeys($reviewData); |
31 | |
32 | foreach ($reviewData as $review) { |
33 | $offset = array_search('categories', array_keys($review)); |
34 | $transformedReview = |
35 | array_slice($review, 0, $offset) + |
36 | $this->fillCategories($categoryKeys, explode(" ", trim($review['categories']) ?? '')) + |
37 | array_slice($review, $offset + 1, null); |
38 | |
39 | $transformedReviewData[] = $this->useNewVersion ? |
40 | $this->returnArrayWithSeperateEntry($transformedReview) |
41 | : $transformedReview; |
42 | } |
43 | |
44 | return $transformedReviewData; |
45 | } |
46 | |
47 | private function extractCategoryKeys(array $reviewData): array { |
48 | $categoryMap = []; |
49 | |
50 | foreach ($reviewData as $reviewEntry) { |
51 | if (!empty($reviewEntry['categories'])) { |
52 | $categories = explode(" ", trim($reviewEntry['categories'])); |
53 | |
54 | foreach ($categories as $category) { |
55 | if (0 === count(array_keys($categoryMap, $category))) { |
56 | $categoryMap[] = $category; |
57 | } |
58 | } |
59 | } |
60 | } |
61 | asort($categoryMap); |
62 | |
63 | return $categoryMap; |
64 | } |
65 | |
66 | private function fillCategories(array $categoryKeys, array $categoryValues): array { |
67 | $categories = []; |
68 | |
69 | foreach ($categoryKeys as $categoryKey) { |
70 | $isMatch = false; |
71 | |
72 | foreach ($categoryValues as $categoryValue) { |
73 | if ($categoryKey === $categoryValue) { |
74 | $isMatch = true; |
75 | break; |
76 | } |
77 | } |
78 | if ($this->useNewVersion) { |
79 | if ($this->format == ReportFormat::CSV) { |
80 | $categories["category_" . $categoryKey] = $isMatch ? 'TRUE' : 'FALSE'; |
81 | } else { |
82 | $categories["category_" . $categoryKey] = $isMatch; |
83 | } |
84 | } else { |
85 | $categories["category: " . $categoryKey] = $isMatch ? 'X' : null; |
86 | } |
87 | } |
88 | |
89 | return $categories; |
90 | } |
91 | |
92 | private function returnArrayWithSeperateEntry(array $reviewData): array { |
93 | [$reviewer, $entry] = explode(': ', $reviewData['entry'], 2); |
94 | unset($reviewData['entry']); |
95 | // if delimiter is not found, because of empty reviewer, the exploded string will be saved in $reviewer only |
96 | if ($entry) { |
97 | $reviewData['reviewer'] = $reviewer; |
98 | $reviewData['entry'] = $entry; |
99 | } else { |
100 | $reviewData['reviewer'] = null; |
101 | $reviewData['entry'] = $reviewer; |
102 | } |
103 | |
104 | return $reviewData; |
105 | } |
106 | |
107 | private function generateCsvReportData(array $reviewData): string { |
108 | $csv[] = implode( |
109 | self::DELIMITER, |
110 | CSV::collectColumnNamesFromHeterogeneousObjects($reviewData) |
111 | ); // TODO: Adjust column headers? |
112 | |
113 | foreach ($reviewData as $review) { |
114 | $csv[] = implode( |
115 | self::DELIMITER, |
116 | array_map( |
117 | function ($reviewProperty) { |
118 | return isset($reviewProperty) ? sprintf(self::CSV_CELL_FORMAT, $reviewProperty) : $reviewProperty; |
119 | }, |
120 | $review |
121 | ) |
122 | ); |
123 | } |
124 | |
125 | $csv = implode(self::LINE_ENDING, $csv); |
126 | |
127 | return self::BOM . $csv; |
128 | } |
129 | } |