ReportsHelper   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 81.03%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 135
ccs 47
cts 58
cp 0.8103
rs 10
c 3
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getLineChartData() 0 12 2
A createReportsLinks() 0 8 2
A linkToReportFromIncident() 0 5 1
A entriesFromIncidents() 0 16 3
A getChartArray() 0 24 3
A getStacktracesForIncidents() 0 20 3
A linkToReport() 0 5 1
1
<?php
2
3
namespace App\View\Helper;
4
5
use Cake\Utility\Inflector;
6
7
use function count;
8
use function implode;
9
use function json_encode;
10
11
use const JSON_UNESCAPED_SLASHES;
12
use const JSON_UNESCAPED_UNICODE;
13
14
class ReportsHelper extends AppHelper
15
{
16
    /** @var string */
17
    public $helpers = ['Incidents'];
18
19
    /**
20
     * @param array[] $entries    Entries
21
     * @param int     $totalCount Total count
22
     * @param string  $key        Key
23
     * @return string HTML
24
     */
25 11
    public function entriesFromIncidents(array $entries, int $totalCount, string $key): string
26
    {
27
        //$entries = Sanitize::clean($entries);
28 11
        $values = [];
29 11
        foreach ($entries as $entry) {
30 11
            $values[] = $entry[$key] . ' <span class="count">('
31 11
                . $entry['count'] . ')</span>';
32
        }
33
34 11
        $fullString = implode(', ', $values);
35 11
        $remaining = $totalCount - count($values);
36 11
        if ($remaining) {
37
            $fullString .= ' <small>and ' . $remaining . ' others</small>';
38
        }
39
40 11
        return $fullString;
41
    }
42
43
    /**
44
     * @param mixed $reports The reports
45
     * @return string comma separated list
46
     */
47 7
    public function createReportsLinks($reports): string
48
    {
49 7
        $links = [];
50 7
        foreach ($reports as $report) {
51 7
            $links[] = $this->linkToReport($report);
52
        }
53
54 7
        return implode(', ', $links);
55
    }
56
57
    /**
58
     * @param mixed $report The report
59
     * @return string HTML <a> link
60
     */
61 21
    public function linkToReport($report): string
62
    {
63 21
        $reportId = $report['id'];
64
65 21
        return '<a href="/' . BASE_DIR . 'reports/view/' . $reportId . '">#' . $reportId . '</a>';
66
    }
67
68
    /**
69
     * @param mixed $incident The incident
70
     * @return string HTML <a> link
71
     */
72 7
    public function linkToReportFromIncident($incident): string
73
    {
74 7
        $reportId = $incident['report_id'];
75
76 7
        return '<a href="/' . BASE_DIR . 'reports/view/' . $reportId . '">#' . $reportId . '</a>';
77
    }
78
79
    /**
80
     * @param mixed $incidents The incidents
81
     * @return string HTML
82
     */
83 7
    public function getStacktracesForIncidents($incidents): string
84
    {
85 7
        $count = 0;
86 7
        $html = '<div class="row">';
87 7
        foreach ($incidents as $incident) {
88 7
            $class = 'well span5';
89
90 7
            if ($count % 2 === 1) {
91
                $class .= ' ';
92
            } else {
93 7
                $html .= '</div><div class="row">';
94
            }
95
96 7
            $html .= $this->Incidents->getStacktrace($incident, $class);
97 7
            ++$count;
98
        }
99
100 7
        $html .= '</div>';
101
102 7
        return $html;
103
    }
104
105
    /**
106
     * @return string HTML code
107
     */
108 7
    public function getChartArray(string $arrayName, array $columns, array $relatedEntries): string
109
    {
110 7
        $finalData = [];
111 7
        foreach ($columns as $column) {
112 2
            $data = [
113 7
                'name' => $column,
114 7
                'title' => Inflector::humanize($column),
115
                'labels' => [],
116
                'values' => [],
117
            ];
118 7
            foreach ($relatedEntries[$column] as $entry) {
119 7
                $count = $entry['count'];
120 7
                $data['labels'][] = $entry[$column] . ' (' . $count . ')';
121 7
                $data['values'][] = $count;
122
            }
123
124 7
            $finalData[] = $data;
125
        }
126
127 7
        return 'var ' . $arrayName . ' = '
128 7
            . json_encode(
129 7
                $finalData,
130 7
                JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
131 7
            ) . ';';
132
    }
133
134
    /**
135
     * @return string HTML
136
     */
137
    public function getLineChartData(string $arrayName, array $entries): string
138
    {
139
        $data = [];
140
        foreach ($entries as $entry) {
141
            $data[] = [$entry['date'], $entry['count']];
142
        }
143
144
        return 'var ' . $arrayName . ' = '
145
        . json_encode(
146
            $data,
147
            JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
148
        ) . ';';
149
    }
150
}
151