Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LogReportOutput
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 generate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 generateCsvReportData
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5class LogReportOutput extends Report {
6
7  public function generate(bool $useNewVersion = false): bool {
8    $this->useNewVersion = $useNewVersion;
9    $adminDAO = new AdminDAO();
10    $logs = $adminDAO->getLogReportData($this->workspaceId, $this->dataIds);
11
12    if (empty($logs)) {
13      return false;
14
15    } else {
16      $this->reportData = $logs;
17
18      if ($this->format == ReportFormat::CSV) {
19        $this->csvReportData = $this->generateCsvReportData($logs);
20      }
21    }
22
23    return true;
24  }
25
26  private function generateCsvReportData(array $logData): string {
27    $columns = [
28      'groupname',
29      'loginname',
30      'code',
31      'bookletname',
32      'unitname',
33      'originalUnitId',
34      'timestamp',
35      'logentry'
36    ]; // TODO: Adjust column headers?
37    $csv[] = implode(self::DELIMITER, $columns);
38
39    foreach ($logData as $log) {
40      $csv[] = implode(
41        self::DELIMITER,
42        [
43          sprintf(self::CSV_CELL_FORMAT, $log['groupname']),
44          sprintf(self::CSV_CELL_FORMAT, $log['loginname']),
45          sprintf(self::CSV_CELL_FORMAT, $log['code']),
46          sprintf(self::CSV_CELL_FORMAT, $log['bookletname']),
47          sprintf(self::CSV_CELL_FORMAT, $log['unitname']),
48          sprintf(self::CSV_CELL_FORMAT, $log['originalUnitId']),
49          sprintf(self::CSV_CELL_FORMAT, $log['timestamp']),
50          preg_replace("/\\\\\"/", '""', $log['logentry'])   // TODO: adjust replacement & use cell enclosure ?
51        ]
52      );
53    }
54
55    $csv = implode(self::LINE_ENDING, $csv);
56
57    return self::BOM . $csv;
58  }
59}