Completed
Push — master ( 7d085e...e849bf )
by Schlaefer
15:17 queued 07:36
created

AdminHelper::badgeForCache()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 1
dl 0
loc 22
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Admin\View\Helper;
14
15
use Admin\Lib\CakeLogEntry;
16
use App\View\Helper\AppHelper;
17
use App\View\Helper\TimeHHelper;
18
use Cake\Cache\Cache;
19
use Cake\View\Helper\BreadcrumbsHelper;
20
use Cake\View\Helper\HtmlHelper;
21
use SaitoHelp\View\Helper\SaitoHelpHelper;
22
23
/**
24
 * @property BreadcrumbsHelper $Breadcrumbs
25
 * @property HtmlHelper $Html
26
 * @property SaitoHelpHelper $SaitoHelp
27
 * @property TimeHHelper $TimeH
28
 */
29
class AdminHelper extends AppHelper
30
{
31
    public $helpers = [
32
        'Breadcrumbs',
33
        'SaitoHelp',
34
        'Html',
35
        'TimeH'
36
    ];
37
38
    /**
39
     * help
40
     *
41
     * @param string $id id
42
     * @return mixed
43
     */
44
    public function help($id)
45
    {
46
        return $this->SaitoHelp->icon($id, ['style' => 'float: right;']);
47
    }
48
49
    /**
50
     * Get badge type for an engine
51
     *
52
     * @param string $engine engine-Id
53
     * @return string
54
     */
55
    public function badgeForCache(string $engine): string
56
    {
57
        $class = get_class(Cache::engine($engine));
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Cache\Cache::engine() has been deprecated: 3.7.0 Use Cache::pool() instead. In 4.0 all cache engines will implement the PSR16 interface and this method does not return objects implementing that interface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
        $class = get_class(/** @scrutinizer ignore-deprecated */ Cache::engine($engine));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
58
        $class = explode('\\', $class);
59
        $class = str_replace('Engine', '', end($class));
60
61
        switch ($class) {
62
            case 'File':
63
                $type = 'warning';
64
                break;
65
            case 'Apc':
66
            case 'Apcu':
67
                $type = 'success';
68
                break;
69
            case 'Debug':
70
                $type = 'important';
71
                break;
72
            default:
73
                $type = 'info';
74
        }
75
76
        return $this->badge($class, $type);
77
    }
78
79
    /**
80
     * badge
81
     *
82
     * @param string $text text
83
     * @param string $badge type
84
     * @return string
85
     */
86
    public function badge(string $text, string $badge = 'info'): string
87
    {
88
        return $this->Html->tag(
89
            'span',
90
            $text,
91
            ['class' => "badge badge-$badge"]
92
        );
93
    }
94
95
    /**
96
     * format cake log
97
     *
98
     * @bogus the ability to see logs isn't in Saito 5 anymore
99
     *
100
     * @param string $log log
101
     * @return string
102
     */
103
    public function formatCakeLog($log)
104
    {
105
        $_nErrorsToShow = 20;
106
        $errors = preg_split(
107
            '/(?=^\d{4}-\d{2}-\d{2})/m',
108
            $log,
109
            -1,
110
            PREG_SPLIT_NO_EMPTY
111
        );
112
        if (empty($errors)) {
113
            return '<p>' . __('No log file found.') . '</p>';
114
        }
115
116
        $out = '';
117
        $k = 0;
118
        $errors = array_reverse($errors);
119
        foreach ($errors as $error) {
120
            $e = new CakeLogEntry($error);
121
            $_i = self::tagId();
122
            $_details = $e->details();
123
            if (!empty($_details)) {
124
                $out .= '<button class="btn btn-mini" style="float:right;" onclick="$(\'#' . $_i . '\').toggle(); return false;">' . __(
125
                    'Details'
126
                ) . '</button>' . "\n";
127
            }
128
            $out .= '<pre style="font-size: 10px;">' . "\n";
129
            $out .= '<div class="row"><div class="span2" style="text-align: right">';
130
            $out .= $this->TimeH->formatTime($e->time(), 'eng');
0 ignored issues
show
Bug introduced by
$e->time() of type string is incompatible with the type DateTimeInterface expected by parameter $timestamp of App\View\Helper\TimeHHelper::formatTime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
            $out .= $this->TimeH->formatTime(/** @scrutinizer ignore-type */ $e->time(), 'eng');
Loading history...
131
132
            $out .= '</div>';
133
            $out .= '<div class="span7">';
134
            $out .= $e->message();
135
            if (!empty($_details)) {
136
                $out .= '<span id="' . $_i . '" style="display: none;">' . "\n";
137
                $out .= $_details;
138
                $out .= '</span>';
139
            }
140
            $out .= '</div></div>';
141
            $out .= '</pre>' . "\n";
142
            if ($k++ > $_nErrorsToShow) {
143
                break;
144
            }
145
        }
146
147
        return $out;
148
    }
149
150
    /**
151
     * jquery table
152
     *
153
     * @param string $selector selector
154
     * @param string $sort sort
155
     *
156
     * @return void
157
     */
158
    public function jqueryTable($selector, $sort)
0 ignored issues
show
Unused Code introduced by
The parameter $sort is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

158
    public function jqueryTable($selector, /** @scrutinizer ignore-unused */ $sort)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        $this->Html->css(
161
            '../js/node_modules/datatables.net-bs4/css/dataTables.bootstrap4.css',
162
            ['block' => 'script']
163
        );
164
        $this->Html->script(
165
            [
166
                '../js/node_modules/datatables.net/js/jquery.dataTables.js',
167
                '../js/node_modules/datatables.net-bs4/js/dataTables.bootstrap4.js'
168
            ],
169
            ['block' => 'script']
170
        );
171
172
        $script = <<<EOF
173
$(function() {
174
    var userTable = $('{$selector}').DataTable();
175
});
176
EOF;
177
178
        $this->Html->scriptBlock($script, ['block' => 'script']);
179
    }
180
}
181