Completed
Pull Request — master (#2233)
by Revin
64:12
created

UninstallerTrait::deleteModuleExtra()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 8.5806
cc 4
eloc 16
nc 4
nop 2
1
<?php
2
3
namespace Backend\Core\Installer;
4
5
/*
6
 * This file is part of Fork CMS.
7
 *
8
 * For the full copyright and license information, please view the license
9
 * file that was distributed with this source code.
10
 */
11
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Trait UninstallerTrait
17
 */
18
trait UninstallerTrait
19
{
20
21
    private $tables;
22
23
    /**
24
     * @return \SpoonDatabase
25
     */
26
    abstract protected function getDatabase(): \SpoonDatabase;
27
28
    /**
29
     * @return string
30
     */
31
    abstract protected function getModule(): string;
32
33
    /**
34
     * @return null|\Symfony\Component\Console\Input\InputInterface
35
     */
36
    abstract public function getInput(): ?InputInterface;
37
38
    /**
39
     * @return null|\Symfony\Component\Console\Output\OutputInterface
40
     */
41
    abstract public function getOutput(): ?OutputInterface;
42
43
    /**
44
     * @param string|array $table
45
     * @return bool
46
     */
47
    protected function tableExists($table): bool
48
    {
49
        if (null === $this->tables) {
50
            $this->tables = $this->getDatabase()->getTables();
51
        }
52
53
        $result = true;
54
55
        foreach ((array)$table as $tableName) {
56
            if (!in_array($tableName, $this->tables, true)) {
57
                $result = false;
58
                break;
59
            }
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Full delete module
67
     *
68
     * @param string|null $module
69
     */
70
    protected function dropModule(string $module = null): void
71
    {
72
        $module = $module ?? $this->getModule();
73
74
        $this->deleteModuleExtra($module);
75
76
        $this->deleteModuleRights($module);
77
        $this->deleteActionRights($module);
78
79
        $this->deleteSettings($module);
80
        $this->deleteLocale($module);
81
        $this->deleteSearchable($module);
82
        $this->deleteModule($module);
83
    }
84
85
    /**
86
     * @param array|string $tables
87
     */
88
    protected function dropDatabase($tables)
89
    {
90
        $drop = [];
91
92
        foreach ((array)$tables as $table) {
93
            if (!empty($table) && $this->tableExists($table)) {
94
                $drop[] = $table;
95
            }
96
        }
97
98
        if (!empty($drop)) {
99
            $this->getDatabase()->drop($drop);
100
        }
101
102
        // clear exists cache
103
        $this->tables = null;
104
    }
105
106
    /**
107
     * Delete a module.
108
     *
109
     * @param string $module The name of the module.
110
     */
111
    protected function deleteModule(string $module): void
112
    {
113
        if ($this->tableExists('modules')) {
114
            $this->getDatabase()->delete(
115
                'modules',
116
                'name = ?',
117
                [$module]
118
            );
119
        }
120
    }
121
122
    /**
123
     * Delete searchable mark for module.
124
     *
125
     * @param string $module The name of the module.
126
     */
127
    protected function deleteSearchable(string $module): void
128
    {
129
        if ($this->tableExists('search_modules')) {
130
            $this->getDatabase()->delete(
131
                'search_modules',
132
                'module = ?',
133
                [$module]
134
            );
135
        }
136
    }
137
138
    /**
139
     * Delete module locale.
140
     *
141
     * @param string $module The name of the module.
142
     */
143
    protected function deleteLocale(string $module): void
144
    {
145
146
        if ($this->tableExists('locale')) {
147
            $this->getDatabase()->delete(
148
                'locale',
149
                'module = ?',
150
                [$module]
151
            );
152
        }
153
    }
154
155
    /**
156
     * Delete module settings.
157
     *
158
     * @param string $module The name of the module.
159
     * @param string|null $name The name of the option.
160
     */
161
    protected function deleteSettings(string $module, string $name = null): void
162
    {
163
        if ($this->tableExists('modules_settings')) {
164
            $where = 'module = ?';
165
            $parameters = [$module];
166
167
            if (null !== $name) {
168
                $where .= ' AND name = ?';
169
                $parameters[] = $name;
170
            }
171
172
            $this->getDatabase()->delete(
173
                'modules_settings',
174
                $where,
175
                $parameters
176
            );
177
        }
178
    }
179
180
    /**
181
     * Inserts a new module.
182
     *
183
     * @param string $module The name of the module.
184
     * @param array|null $labels
185
     */
186
    protected function deleteModuleExtra(string $module, array $labels = null): void
187
    {
188
        if ($this->tableExists('modules_extras')) {
189
            $where = 'module = ?';
190
            $parameters = [$module];
191
192
            if (null !== $labels) {
193
                if (count($labels) === 1) {
194
                    $where .= ' AND label = ?';
195
                    $parameters[] = array_shift($labels);
196
                } else {
197
                    $query = str_repeat('?, ', count($labels) - 1) . '?';
198
199
                    $where .= " AND label IN ($query)";
200
                    $parameters = array_merge($parameters, $labels);
201
                }
202
            }
203
204
            $this->getDatabase()->delete(
205
                'modules_extras',
206
                $where,
207
                $parameters
208
            );
209
        }
210
    }
211
212
    /**
213
     * Get a navigation item.
214
     *
215
     * @param int|null $parentId Id of the navigation item under we should add this.
216
     * @param string $label Label for the item.
217
     * @param string|null $url Url for the item. If omitted the first child is used.
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
218
     * @return int|null
219
     */
220
    protected function getNavigation(
221
        $parentId,
222
        string $label = null
223
    ): ?int
224
    {
225
        if ($this->tableExists('backend_navigation')) {
226
            // get the id for this url
227
            return (int)$this->getDatabase()->getVar(
228
                'SELECT id
229
             FROM backend_navigation
230
             WHERE parent_id = ? AND label = ?',
231
                [$parentId, $label]
232
            );
233
        }
234
235
        return null;
236
    }
237
238
    /**
239
     * Delete a navigation item.
240
     *
241
     * @param string $path path of removed navigation
242
     */
243
    protected function deleteNavigation($path): void
244
    {
245
        if ($this->tableExists('backend_navigation')) {
246
            $output = $this->getOutput();
247
248
            $chunks = explode('.', $path);
249
250
            if ($output->isVerbose()) {
251
                $output->writeln('Check navigation: ' . $path);
252
            }
253
254
            if (!empty($chunks)) {
255
                $lastNavItemId = 0;
256
257
                foreach ($chunks as $item) {
258
                    if ($output->isVeryVerbose()) {
259
                        $output->write('Getting [' . ($lastNavItemId ?? 'null') . ' -> ' . $item . ']: ');
260
                    }
261
262
                    $lastNavItemId = $this->getNavigation($lastNavItemId, $item);
263
264
                    if ($output->isVeryVerbose()) {
265
                        $output->writeln($lastNavItemId ?? 'null');
266
                    }
267
                }
268
269
                if (0 !== $lastNavItemId) {
270
                    if ($output->isVerbose()) {
271
                        $output->writeln('Remove navigation: ' . $lastNavItemId ?? 'null');
272
                    }
273
274
                    $this->getDatabase()->delete(
275
                        'backend_navigation',
276
                        'id = ?',
277
                        [$lastNavItemId]
278
                    );
279
                }
280
            }
281
        }
282
    }
283
284
    /**
285
     * Delete the rights for an action
286
     *
287
     * @param string $module The module wherein the action appears.
288
     */
289
    protected function deleteActionRights(string $module): void
290
    {
291
        if ($this->tableExists('groups_rights_actions')) {
292
            $this->getDatabase()->delete(
293
                'groups_rights_actions',
294
                'module = ?',
295
                [$module]
296
            );
297
        }
298
    }
299
300
    /**
301
     * Delete the rights for a module
302
     *
303
     * @param string $module The module too set the rights for.
304
     */
305
    protected function deleteModuleRights(string $module): void
306
    {
307
        if ($this->tableExists('groups_rights_modules')) {
308
            $this->getDatabase()->delete(
309
                'groups_rights_modules',
310
                'module = ?',
311
                [$module]
312
            );
313
        }
314
    }
315
316
    /**
317
     * Delete dashboard widgets
318
     *
319
     * @param string $module
320
     * @param array $widgets
321
     */
322
    protected function deleteDashboardWidgets(string $module, array $widgets): void
323
    {
324
        if ($this->tableExists(['groups_settings', 'users_settings'])) {
325
// get database
326
            $database = $this->getDatabase();
327
328
            // fetch current settings
329
            $groupSettings = (array)$database->getRecords(
330
                'SELECT * FROM groups_settings WHERE name = ?',
331
                ['dashboard_sequence']
332
            );
333
            $userSettings = (array)$database->getRecords(
334
                'SELECT * FROM users_settings WHERE name = ?',
335
                ['dashboard_sequence']
336
            );
337
338
            // loop group settings
339
            foreach ($groupSettings as $settings) {
340
                // unserialize data
341
                $settings['value'] = unserialize($settings['value']);
342
343
                foreach ($widgets as $widget) {
344
                    $settings['value'][$module] = array_filter(
345
                        $settings['value'][$module],
346
                        function ($item) use ($widget) {
347
                            return $widget !== $item;
348
                        });
349
                }
350
351
                // re-serialize value
352
                $settings['value'] = serialize($settings['value']);
353
354
                // update in database
355
                $database->update(
356
                    'groups_settings',
357
                    $settings,
358
                    'group_id = ? AND name = ?',
359
                    [$settings['group_id'], $settings['name']]
360
                );
361
            }
362
363
            // loop user settings
364
            foreach ($userSettings as $settings) {
365
                // unserialize data
366
                $settings['value'] = unserialize($settings['value']);
367
368
                foreach ($widgets as $widget) {
369
                    $settings['value'][$module] = array_filter(
370
                        $settings['value'][$module],
371
                        function ($item) use ($widget) {
372
                            return $widget !== $item;
373
                        });
374
                }
375
376
                // re-serialize value
377
                $settings['value'] = serialize($settings['value']);
378
379
                // update in database
380
                $database->update(
381
                    'users_settings',
382
                    $settings,
383
                    'user_id = ? AND name = ?',
384
                    [$settings['user_id'], $settings['name']]
385
                );
386
            }
387
        }
388
    }
389
390
    /**
391
     * Delete pages.
392
     *
393
     * @param array $pages
394
     */
395
    protected function deletePages(array $pages): void
396
    {
397
        if ($this->tableExists('pages')) {
398
            $this->getDatabase()->delete(
399
                'pages',
400
                'title IN (' . str_repeat('?, ', count($pages) - 1) . ' ?)',
401
                $pages
402
            );
403
        }
404
    }
405
}
406