Completed
Pull Request — master (#168)
by Deven
03:04
created

IncidentsHelper::createIncidentsLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Incidents View helper.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace app\View\Helper;
21
22
use App\View\Helper\AppHelper;
23
use Cake\View\View;
24
25
/**
26
 * Incidents View helper.
27
 */
28
class IncidentsHelper extends AppHelper
29
{
30 2
    public function __construct(View $view, $settings = array())
31
    {
32 2
        parent::__construct($view, $settings);
33 2
    }
34
35
    public function createIncidentsLinks($incidents)
36
    {
37
        $links = array();
38
        foreach ($incidents as $incident) {
39
            $links[] = $this->linkToIncident($incident);
40
        }
41
        $string = implode(', ', $links);
42
43
        return $string;
44
    }
45
46
    public function linkToIncident($incident)
47
    {
48
        $incidentId = $incident['id'];
49
        $link = "<a href='/" . BASE_DIR . "incidents/view/$incidentId'>#$incidentId</a>";
50
51
        return $link;
52
    }
53
54
    public function incidentsDescriptions($incidents)
55
    {
56
        $descriptions = '';
57
        foreach ($incidents as $incident) {
58
            $descriptions .= '<span>Incident ';
59
            $descriptions .= $this->linkToIncident($incident);
60
            $descriptions .= ':</span>';
61
            $descriptions .= '<pre>';
62
            $descriptions .= htmlspecialchars($incident['steps']);
63
            $descriptions .= '</pre>';
64
        }
65
66
        return $descriptions;
67
    }
68
69 1
    public function getStacktrace($incident, $divClass)
70
    {
71 1
        $html = '';
72 1
        $html .= "<div class='$divClass'>";
73
74 1
        if (is_string($incident['stacktrace'])) {
75
            $incident['stacktrace'] =
76
                    json_decode($incident['stacktrace'], true);
77
        }
78
79 1
        foreach ($incident['stacktrace'] as $level) {
80 1
            $exception_type = (($incident['exception_type']) ? ('php') : ('js'));
81 1
            $html .= $this->_getStackLevelInfo($level, $exception_type);
82
            $html .= "<pre class='brush: "
83 1
                . $exception_type
84 1
                . '; tab-size: 2';
85 1
            if (isset($level['line']) && $level['line']) {
86
                if ($incident['exception_type']) {
87
                    $html .= '; first-line: ' . (int) $level['line'];
88
                } elseif ((int) $level['line'] > 5) {
89
                    $html .= '; first-line: ' . ((int) $level['line'] - 5);
90
                }
91
                $html .= '; highlight: [' . (int) $level['line'] . ']';
92
            }
93 1
            $html .= "'>";
94
95 1
            if ($exception_type == 'js') {
96 1
                $html .= htmlspecialchars(join("\n", $level['context']));
97
            } else {
98
                $html .= htmlspecialchars($level['function']);
99
                $html .= '(';
100
                $argList = '';
101
                if (count($level['args']) > 0) {
102
                    foreach ($level['args'] as $arg) {
103
                        $argList .= "\n"
104
                            . gettype($arg)
105
                            . ' => '
106
                            . $arg;
107
                        $argList .= ',';
108
                    }
109
                    $argList = substr($argList, 0, (strlen($argList) - 1));
110
                    $argList .= "\n";
111
                }
112
                $html .= htmlspecialchars($argList);
113
                $html .= ')';
114
            }
115 1
            $html .= '</pre>';
116
        }
117 1
        $html .= '</div>';
118
119 1
        return $html;
120
    }
121
122 1
    protected function _getStackLevelInfo($level, $exception_type = 'js')
123
    {
124 1
        $html = '<span>';
125 1
        if ($exception_type == 'js') {
126 1
            $elements = array('filename', 'scriptname', 'line', 'func', 'column');
127
        } else {
128
            $elements = array('file', 'line', 'function', 'class');
129
        }
130 1
        foreach ($elements as $element) {
131 1
            if (isset($level[$element])) {
132 1
                $html .= "$element: " . $level[$element] . '; ';
133
            }
134
        }
135 1
        $html .= '</span>';
136
137 1
        return $html;
138
    }
139
}
140