Completed
Push — develop ( 6f95b1...70d143 )
by Schlaefer
02:49
created

AdminHelper::formatCakeLog()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 1
dl 0
loc 46
rs 8.5559
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 method Cake\Cache\Cache::engine() has been deprecated with message: 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.

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

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class 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
Documentation introduced by
$e->time() is of type string, but the function expects a object<DateTimeInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
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.

This check looks from 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
    /**
182
     * accession to roles
183
     *
184
     * @param int $accession accession
185
     * @return string
186
     */
187 View Code Duplication
    public function accessionToRoles($accession)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189
        switch ($accession) {
190
            case (0):
191
                return __('user.type.anon');
192
            case (1):
193
                return __('user.type.user');
194
            case (2):
195
                return __('user.type.mod');
196
            case (3):
197
                return __('user.type.admin');
198
        }
199
    }
200
}
201