Passed
Pull Request — master (#1500)
by Michael
09:16
created

xoops_module_update_moduleinstaller()   D

Complexity

Conditions 18
Paths 33

Size

Total Lines 87
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 43
c 1
b 0
f 0
nc 33
nop 2
dl 0
loc 87
rs 4.8666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project (https://xoops.org)
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team
16
 */
17
18
use XoopsModules\Moduleinstaller;
19
20
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
21
    || !$GLOBALS['xoopsUser']->isAdmin()) {
22
    exit('Restricted access' . PHP_EOL);
23
}
24
25
/**
26
 * Prepares system prior to attempting to install module
27
 * @param \XoopsModule $module {@link XoopsModule}
28
 *
29
 * @return bool true if ready to install, false if not
30
 */
31
function xoops_module_pre_update_moduleinstaller(\XoopsModule $module)
32
{
33
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
34
    /** @var Moduleinstaller\Helper $helper */
35
    /** @var Moduleinstaller\Utility $utility */
36
    $helper  = Moduleinstaller\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
37
    $utility = new Moduleinstaller\Utility();
38
39
    $xoopsSuccess = $utility::checkVerXoops($module);
40
    $phpSuccess   = $utility::checkVerPhp($module);
41
42
    return $xoopsSuccess && $phpSuccess;
43
}
44
45
/**
46
 * Performs tasks required during update of the module
47
 * @param \XoopsModule $module {@link XoopsModule}
48
 * @param int|null  $previousVersion
49
 *
50
 * @return bool true if update successful, false if not
51
 */
52
function xoops_module_update_moduleinstaller(\XoopsModule $module, $previousVersion = null)
53
{
54
    $moduleDirName      = \basename(\dirname(__DIR__));
55
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
56
57
    /** @var Moduleinstaller\Helper $helper */ /** @var Moduleinstaller\Utility $utility */
58
    /** @var Moduleinstaller\Common\Configurator $configurator */
59
    $helper       = Moduleinstaller\Helper::getInstance();
60
    $utility      = new Moduleinstaller\Utility();
61
    $configurator = new Moduleinstaller\Common\Configurator();
62
63
    $helper->loadLanguage('common');
64
65
    if ($previousVersion < 240) {
66
        //delete old HTML templates
67
        if (count($configurator->templateFolders) > 0) {
68
            foreach ($configurator->templateFolders as $folder) {
69
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
70
                if (is_dir($templateFolder)) {
71
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
72
                    foreach ($templateList as $k => $v) {
73
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
74
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
75
                            if (\is_file($templateFolder . $v)) {
76
                                unlink($templateFolder . $v);
77
                            }
78
                        }
79
                    }
80
                }
81
            }
82
        }
83
84
        //  ---  DELETE OLD FILES ---------------
85
        if (count($configurator->oldFiles) > 0) {
86
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
87
            foreach (array_keys($configurator->oldFiles) as $i) {
88
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
89
                if (\is_file($tempFile)) {
90
                    unlink($tempFile);
91
                }
92
            }
93
        }
94
95
        //  ---  DELETE OLD FOLDERS ---------------
96
        xoops_load('XoopsFile');
97
        if (count($configurator->oldFolders) > 0) {
98
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
99
            foreach (array_keys($configurator->oldFolders) as $i) {
100
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
101
                /** @var XoopsObjectHandler $folderHandler */
102
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
103
                $folderHandler->delete($tempFolder);
104
            }
105
        }
106
107
        //  ---  CREATE FOLDERS ---------------
108
        if (count($configurator->uploadFolders) > 0) {
109
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
110
            foreach (array_keys($configurator->uploadFolders) as $i) {
111
                $utility::createFolder($configurator->uploadFolders[$i]);
112
            }
113
        }
114
115
        //  ---  COPY blank.png FILES ---------------
116
        if (count($configurator->copyBlankFiles) > 0) {
117
            $file = \dirname(__DIR__) . '/assets/images/blank.png';
118
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
119
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
120
                $utility::copyFile($file, $dest);
121
            }
122
        }
123
124
        //delete .html entries from the tpl table
125
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
126
        $GLOBALS['xoopsDB']->queryF($sql);
127
128
        //delete old .html entries from the newblocks table
129
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('newblocks') . " WHERE `mid` = '" . $module->getVar('mid') . "' AND `template` LIKE '%.html%'";
130
        $GLOBALS['xoopsDB']->queryF($sql);
131
132
        /** @var \XoopsGroupPermHandler $grouppermHandler */
133
        $grouppermHandler = xoops_getHandler('groupperm');
134
135
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
136
    }
137
138
    return true;
139
}
140