Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

ReportsHelper::linkToReportFromIncident()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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