eventCoreIncludeCommonAuthSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 3
b 0
f 0
1
<?php declare(strict_types=1);
2
3
//namespace XoopsModules\Xwhoops25;
4
5
use Xmf\Module\Helper\Permission;
6
use Whoops\Run;
7
use Whoops\Handler\PrettyPageHandler;
8
9
/**
10
 * @copyright 2019-2021 XOOPS Project (https://xoops.org)
11
 * @license   GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
12
 * @author    Richard Griffith <[email protected]>
13
 */
14
class Xwhoops25CorePreload extends \XoopsPreloadItem
15
{
16
    private const AUTOLOADER_PATH = '/vendor/autoload.php';
17
    private const PERMISSION_NAME = 'use_xwhoops';
18
    private const PERMISSION_ITEM_ID = 0;
19
20
    /**
21
     * eventCoreIncludeCommonAuthSuccess
22
     */
23
    public static function eventCoreIncludeCommonAuthSuccess(): void
24
    {
25
        self::initializeAutoloader();
26
        self::initializeWhoops();
27
    }
28
29
    /**
30
     * @return void
31
     */
32
    private static function initializeAutoloader(): void
33
    {
34
        $autoloader = \dirname(__DIR__) . self::AUTOLOADER_PATH;
35
36
        if (!\file_exists($autoloader)) {
37
            // Throw an exception for better error handling
38
            throw new \RuntimeException("xwhoops25/vendor/autoload.php not found, was 'composer install' done?");
39
        }
40
41
        require_once $autoloader;
42
    }
43
44
    /**
45
     * @return void
46
     */
47
    private static function initializeWhoops(): void
48
    {
49
        $permissionHelper = new Permission('xwhoops25');
50
        if ($permissionHelper->checkPermission(self::PERMISSION_NAME, self::PERMISSION_ITEM_ID, false)) {
51
            self::registerWhoops();
52
        }
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    private static function registerWhoops(): void
59
    {
60
        $whoops = new Run();
61
        $handler = new PrettyPageHandler();
62
63
        $whoops->pushHandler($handler);
64
        $whoops->register();
65
66
        $handler->addDataTableCallback(
67
            \_LOGGER_QUERIES,
68
            static function () {
69
                return self::getLoggerQueries();
70
            }
71
        );
72
    }
73
74
    /**
75
     * @return array|string[]
76
     */
77
    private static function getLoggerQueries(): array
78
    {
79
        $logger = \XoopsLogger::getInstance();
80
81
        if (false === $logger->renderingEnabled) {
82
            return ['XoopsLogger' => 'off'];
83
        }
84
85
        $queries = [];
86
        $count = 1;
87
88
        foreach ($logger->queries as $key => $query) {
89
            $queries[$count] = self::formatQuery($query, $count);
90
            $count++;
91
        }
92
93
        return $queries;
94
    }
95
96
    /**
97
     * @param array $query
98
     * @param int   $count
99
     *
100
     * @return string
101
     */
102
    private static function formatQuery(array $query, int $count): string
103
    {
104
        $error = (null === $query['errno'] ? '' : $query['errno'] . ' ') . ($query['error'] ?? '');
105
        $queryTime = isset($query['query_time']) ? \sprintf('%0.6f', $query['query_time']) : '';
106
        $queryKey = $count . ' - ' . $queryTime;
0 ignored issues
show
Unused Code introduced by
The assignment to $queryKey is dead and can be removed.
Loading history...
107
108
        if (null !== $query['errno']) {
109
            $queryKey = $count . ' - Error';
110
        }
111
112
        return \htmlentities($query['sql'], \ENT_QUOTES | \ENT_HTML5) . ' ' . $error;
113
    }
114
}
115