Passed
Push — master ( 443a01...603337 )
by William
02:49
created

ReportsHelper::createReportsLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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