Completed
Push — master ( 7c757d...667317 )
by William
02:47
created

ReportsHelper::getChartArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
ccs 14
cts 14
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
5
namespace App\View\Helper;
6
7
use App\Utility\Sanitize;
8
use App\View\Helper\AppHelper;
9
use Cake\Utility\Inflector;
10
use Cake\View\View;
11
12
class ReportsHelper extends AppHelper
13
{
14
    public $helpers = ['Incidents'];
15
16 7
    public function __construct(View $view, $settings = [])
17
    {
18 7
        parent::__construct($view, $settings);
19 7
    }
20
21 2
    public function entriesFromIncidents($entries, $totalCount, $key)
22
    {
23
        //$entries = Sanitize::clean($entries);
24 2
        $values = [];
25 2
        foreach ($entries as $entry) {
26 2
            $values[] = "$entry[$key] <span class='count'>("
27 2
                . $entry['count'] . ')</span>';
28
        }
29 2
        $fullString = implode(', ', $values);
30 2
        $remaining = $totalCount - count($values);
31 2
        if ($remaining) {
32
            $fullString .= " <small>and $remaining others</small>";
33
        }
34
35 2
        return $fullString;
36
    }
37
38 1
    public function createReportsLinks($reports)
39
    {
40 1
        $links = [];
41 1
        foreach ($reports as $report) {
42 1
            $links[] = $this->linkToReport($report);
43
        }
44 1
        $string = implode(', ', $links);
45
46 1
        return $string;
47
    }
48
49 3
    public function linkToReport($report)
50
    {
51 3
        $reportId = $report['id'];
52 3
        $link = '<a href=/' . BASE_DIR . "reports/view/$reportId>#$reportId</a>";
53
54 3
        return $link;
55
    }
56
57 1
    public function linkToReportFromIncident($incident)
58
    {
59 1
        $reportId = $incident['report_id'];
60 1
        $link = '<a href=/' . BASE_DIR . "reports/view/$reportId>#$reportId</a>";
61
62 1
        return $link;
63
    }
64
65 1
    public function getStacktracesForIncidents($incidents)
66
    {
67 1
        $count = 0;
68 1
        $html = '<div class="row">';
69 1
        foreach ($incidents as $incident) {
70 1
            $class = 'well span5';
71
72 1
            if ($count % 2 == 1) {
73
                $class .= ' ';
74
            } else {
75 1
                $html .= "</div><div class='row'>";
76
            }
77
78 1
            $html .= $this->Incidents->getStacktrace($incident, $class);
79 1
            ++$count;
80
        }
81 1
        $html .= '</div>';
82
83 1
        return $html;
84
    }
85
86 1
    public function getChartArray($arrayName, $columns, $relatedEntries)
87
    {
88 1
        $html = "var $arrayName = [], chart = {};";
89 1
        foreach ($columns as $column) {
90 1
            $column = htmlspecialchars($column, ENT_QUOTES | ENT_HTML5);
91 1
            $html .= 'chart = {};';
92 1
            $html .= "chart.name = '$column';";
93 1
            $html .= "chart.title = '" . Inflector::humanize($column) . "';";
94 1
            $html .= 'chart.labels = []; chart.values = [];';
95 1
            foreach ($relatedEntries[$column] as $entry) {
96 1
                $count = $entry['count'];
97 1
                $html .= "chart.labels.push('$entry[$column] ($count)');";
98 1
                $html .= "chart.values.push($count);";
99
            }
100 1
            $html .= "$arrayName.push(chart);";
101
        }
102
103 1
        return $html;
104
    }
105
106
    public function getLineChartData($arrayName, $entries)
107
    {
108
        $html = "var $arrayName = [];";
109
        foreach ($entries as $entry) {
110
            $html .= "$arrayName.push(['" . $entry['date'] . "', "
111
                    . $entry['count'] . ']);';
112
        }
113
114
        return $html;
115
    }
116
}
117