ServerFacade::getProcesses()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 8
nop 0
dl 0
loc 21
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Facades;
4
5
use function array_filter;
6
use function array_intersect;
7
use function array_key_exists;
8
use function array_keys;
9
use function array_values;
10
use function compact;
11
use function is_array;
12
use function is_string;
13
14
/**
15
 * Facade to server functions
16
 */
17
class ServerFacade extends AbstractFacade
18
{
19
    /**
20
     * The final database list
21
     *
22
     * @var array|null
23
     */
24
    protected $finalDatabases = null;
25
26
    /**
27
     * The databases the user has access to
28
     *
29
     * @var array|null
30
     */
31
    protected $userDatabases = null;
32
33
    /**
34
     * The constructor
35
     *
36
     * @param AbstractFacade $dbFacade
37
     * @param array $options    The server config options
38
     */
39
    public function __construct(AbstractFacade $dbFacade, array $options)
40
    {
41
        parent::__construct($dbFacade);
42
        // Set the user databases, if defined.
43
        if (is_array(($userDatabases = $options['access']['databases'] ?? null))) {
44
            $this->userDatabases = $userDatabases;
45
        }
46
    }
47
48
    /**
49
     * Check if a feature is supported
50
     *
51
     * @param string $feature
52
     *
53
     * @return bool
54
     */
55
    public function support(string $feature)
56
    {
57
        return $this->driver->support($feature);
58
    }
59
60
    /**
61
     * Get the databases from the connected server
62
     *
63
     * @param bool $schemaAccess
64
     *
65
     * @return array
66
     */
67
    protected function databases(bool $schemaAccess): array
68
    {
69
        if ($this->finalDatabases === null) {
70
            // Get the database lists
71
            // Passing false as parameter to this call prevent from using the slow_query() function,
72
            // which outputs data to the browser are prepended to the Jaxon response.
73
            $this->finalDatabases = $this->driver->databases(false);
74
            if (is_array($this->userDatabases)) {
75
                // Only keep databases that appear in the config.
76
                $this->finalDatabases = array_values(array_intersect($this->finalDatabases, $this->userDatabases));
77
            }
78
        }
79
        return $schemaAccess ? $this->finalDatabases : array_filter($this->finalDatabases,
80
            fn($database) => !$this->driver->isSystemSchema($database));
81
    }
82
83
    /**
84
     * Connect to a database server
85
     *
86
     * @return array
87
     */
88
    public function getServerInfo(): array
89
    {
90
        $server = $this->utils->trans->lang(
91
            '%s version: %s. PHP extension %s.',
92
            $this->driver->name(),
93
            "<b>" . $this->utils->str->html($this->driver->serverInfo()) . "</b>",
0 ignored issues
show
Unused Code introduced by
The call to Lagdo\DbAdmin\Driver\Uti...slatorInterface::lang() has too many arguments starting with '<b>' . $this->utils->st...>serverInfo()) . '</b>'. ( Ignorable by Annotation )

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

93
        /** @scrutinizer ignore-call */ 
94
        $server = $this->utils->trans->lang(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
94
            "<b>{$this->driver->extension()}</b>"
95
        );
96
        $user = $this->utils->trans->lang('Logged as: %s.', "<b>" . $this->utils->str->html($this->driver->user()) . "</b>");
97
98
        return compact('server', 'user');
99
    }
100
101
    /**
102
     * Create a database
103
     *
104
     * @param string $database  The database name
105
     * @param string $collation The database collation
106
     *
107
     * @return bool
108
     */
109
    public function createDatabase(string $database, string $collation = ''): bool
110
    {
111
        return $this->driver->createDatabase($database, $collation);
112
    }
113
114
    /**
115
     * Drop a database
116
     *
117
     * @param string $database  The database name
118
     *
119
     * @return bool
120
     */
121
    public function dropDatabase(string $database): bool
122
    {
123
        return $this->driver->dropDatabase($database);
124
    }
125
126
    /**
127
     * Get the collation list
128
     *
129
     * @return array
130
     */
131
    public function getCollations(): array
132
    {
133
        return $this->driver->collations();
134
    }
135
136
    /**
137
     * Get the database list
138
     *
139
     * @param bool $schemaAccess
140
     *
141
     * @return array
142
     */
143
    public function getDatabases(bool $schemaAccess): array
144
    {
145
        $headers = [
146
            $this->utils->trans->lang('Database'),
147
            $this->utils->trans->lang('Collation'),
148
            $this->utils->trans->lang('Tables'),
149
            $this->utils->trans->lang('Size'),
150
            '',
151
        ];
152
153
        // Get the database list
154
        $databases = $this->databases($schemaAccess);
155
        $tables = $this->driver->countTables($databases);
156
        $collations = $this->driver->collations();
157
        $details = [];
158
        foreach ($databases as $database) {
159
            $details[] = [
160
                'name' => $this->utils->str->html($database),
161
                'collation' => $this->utils->str->html($this->driver->databaseCollation($database, $collations)),
162
                'tables' => array_key_exists($database, $tables) ? $tables[$database] : 0,
163
                'size' => $this->utils->trans->formatNumber($this->driver->databaseSize($database)),
0 ignored issues
show
Bug introduced by
The method formatNumber() does not exist on Lagdo\DbAdmin\Driver\Utils\TranslatorInterface. It seems like you code against a sub-type of Lagdo\DbAdmin\Driver\Utils\TranslatorInterface such as Lagdo\DbAdmin\Translator. ( Ignorable by Annotation )

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

163
                'size' => $this->utils->trans->/** @scrutinizer ignore-call */ formatNumber($this->driver->databaseSize($database)),
Loading history...
164
            ];
165
        }
166
167
        return compact('headers', 'databases', 'details');
168
    }
169
170
    /**
171
     * Get the processes
172
     *
173
     * @return array
174
     */
175
    public function getProcesses(): array
176
    {
177
        // From processlist.inc.php
178
        $processes = $this->driver->processes();
179
180
        // From processlist.inc.php
181
        // TODO: Add a kill column in the headers
182
        $headers = [];
183
        $details = [];
184
        if (($process = reset($processes)) !== false) {
185
            // Set the keys of the first entry as headers
186
            $headers = array_keys($process);
187
        }
188
        foreach ($processes as $process) {
189
            $attrs = [];
190
            foreach ($process as $key => $val) {
191
                $attrs[] = is_string($val) ? $this->driver->processAttr($process, $key, $val) : '(null)';
192
            }
193
            $details[] = $attrs;
194
        }
195
        return compact('headers', 'details');
196
    }
197
198
    /**
199
     * Get the variables
200
     *
201
     * @return array
202
     */
203
    public function getVariables(): array
204
    {
205
        // From variables.inc.php
206
        $variables = $this->driver->variables();
207
208
        $headers = false;
209
210
        $details = [];
211
        // From variables.inc.php
212
        foreach ($variables as $key => $val) {
213
            $details[] = [$this->utils->str->html($key), is_string($val) ? $this->utils->str->shortenUtf8($val, 50) : '(null)'];
214
        }
215
216
        return compact('headers', 'details');
217
    }
218
219
    /**
220
     * Get the server status
221
     *
222
     * @return array
223
     */
224
    public function getStatus(): array
225
    {
226
        // From variables.inc.php
227
        $status = $this->driver->statusVariables();
228
229
        $headers = false;
230
        $details = [];
231
        // From variables.inc.php
232
        foreach ($status as $key => $val) {
233
            $details[] = [$this->utils->str->html($key), is_string($val) ? $this->utils->str->html($val) : '(null)'];
234
        }
235
236
        return compact('headers', 'details');
237
    }
238
}
239