xoops_module_update_suico()   D
last analyzed

Complexity

Conditions 20
Paths 36

Size

Total Lines 119
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 20
eloc 67
nc 36
nop 2
dl 0
loc 119
rs 4.1666
c 2
b 0
f 1

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
 * @category        Module
14
 * @copyright       {@link https://xoops.org/ XOOPS Project}
15
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
16
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
17
 */
18
19
use XoopsModules\Suico\{
20
    Common\Configurator,
21
    Common\Migrate,
22
    Helper,
23
    Utility
24
};
25
/** @var Helper $helper */
26
/** @var Utility $utility */
27
/** @var Configurator $configurator */
28
/** @var Migrate $migrator */
29
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
30
    || !$GLOBALS['xoopsUser']->isAdmin()) {
31
    exit('Restricted access' . PHP_EOL);
32
}
33
include \dirname(__DIR__) . '/preloads/autoloader.php';
34
35
/**
36
 * Prepares system prior to attempting to install module
37
 * @param \XoopsModule $module {@link \XoopsModule}
38
 * @return bool true if ready to install, false if not
39
 */
40
function xoops_module_pre_update_suico(
41
    \XoopsModule $module
42
) {
43
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
44
    $helper        = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
45
    $utility       = new Utility();
46
    $xoopsSuccess  = $utility::checkVerXoops($module);
47
    $phpSuccess    = $utility::checkVerPhp($module);
48
    $configurator  = new Configurator();
49
    $migrator      = new Migrate($configurator);
50
    $migrator->synchronizeSchema();
51
52
    return $xoopsSuccess && $phpSuccess;
53
}
54
55
/**
56
 * Performs tasks required during update of the module
57
 * @param \XoopsModule $module {@link XoopsModule}
58
 * @param null         $previousVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $previousVersion is correct as it would always require null to be passed?
Loading history...
59
 *
60
 * @return bool true if update successful, false if not
61
 */
62
function xoops_module_update_suico(
63
    XoopsModule $module,
64
    $previousVersion = null
65
) {
66
    $moduleDirName      = \basename(\dirname(__DIR__));
67
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
68
69
    $helper       = Helper::getInstance();
70
    $utility      = new Utility();
71
    $configurator = new Configurator();
72
    $helper->loadLanguage('common');
73
    $migrator = new Migrate($configurator);
74
    $migrator->synchronizeSchema();
75
    if ($previousVersion < 360) {
76
        //rename column EXAMPLE
77
        //        $tables = new Tables();
78
        //        $table = 'xxxx_categories';
79
        //        $column = 'order';
80
        //        $newName = 'order';
81
        //        $attributes = "INT(5) NOT NULL DEFAULT '0'";
82
        //        if ($tables->useTable($table)) {
83
        //            $tables->alterColumn($table, $column, $attributes, $newName);
84
        //            if (!$tables->executeQueue()) {
85
        //                echo '<br>' . constant('CO_' . $moduleDirNameUpper . '_UPGRADEFAILED0') . ' ' . $migrate->getLastError();
86
        //            }
87
        //        }
88
        //delete old HTML templates
89
        if (count($configurator->templateFolders) > 0) {
90
            foreach ($configurator->templateFolders as $folder) {
91
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
92
                if (is_dir($templateFolder)) {
93
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
94
                    foreach ($templateList as $k => $v) {
95
                        $fileInfo = new SplFileInfo($templateFolder . $v);
96
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
97
                            if (is_file($templateFolder . $v)) {
98
                                if (false === @\unlink($templateFolder . $v)) {
99
                                    throw new \RuntimeException('The file ' . $templateFolder . $v . ' could not be deleted.');
100
                                }
101
                            }
102
                        }
103
                    }
104
                }
105
            }
106
        }
107
        //  ---  DELETE OLD FILES ---------------
108
        if (count($configurator->oldFiles) > 0) {
109
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
110
            foreach (
111
                array_keys(
112
                    $configurator->oldFiles
113
                ) as $i
114
            ) {
115
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
116
                if (is_file($tempFile)) {
117
                    if (false === @\unlink($tempFile)) {
118
                        throw new \RuntimeException('The file ' . $tempFile . ' could not be deleted.');
119
                    }
120
                }
121
            }
122
        }
123
        //  ---  DELETE OLD FOLDERS ---------------
124
        xoops_load('XoopsFile');
125
        if (count($configurator->oldFolders) > 0) {
126
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
127
            foreach (
128
                array_keys(
129
                    $configurator->oldFolders
130
                ) as $i
131
            ) {
132
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
133
                /** @var XoopsObjectHandler $folderHandler */
134
                $folderHandler = XoopsFile::getHandler(
135
                    'folder',
136
                    $tempFolder
137
                );
138
                $folderHandler->delete($tempFolder);
139
            }
140
        }
141
        //  ---  CREATE UPLOAD FOLDERS ---------------
142
        if (count($configurator->uploadFolders) > 0) {
143
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
144
            foreach (
145
                array_keys(
146
                    $configurator->uploadFolders
147
                ) as $i
148
            ) {
149
                $utility::createFolder($configurator->uploadFolders[$i]);
150
            }
151
        }
152
        //  ---  COPY blank.png FILES ---------------
153
        if (count($configurator->copyBlankFiles) > 0) {
154
            $file = \dirname(__DIR__) . '/assets/images/blank.png';
155
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
156
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
157
                $utility::copyFile($file, $dest);
158
            }
159
        }
160
        //delete .html entries from the tpl table
161
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix(
162
                'tplfile'
163
            ) . " WHERE `tpl_module` = '" . $module->getVar(
164
                'dirname',
165
                'n'
166
            ) . "' AND `tpl_file` LIKE '%.html%'";
167
        $GLOBALS['xoopsDB']->queryF($sql);
168
        /** @var XoopsGroupPermHandler $grouppermHandler */
169
        $grouppermHandler = xoops_getHandler('groupperm');
170
171
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
172
    }
173
    $profileHandler = $helper->getHandler('Profile');
174
    $profileHandler->cleanOrphan($GLOBALS['xoopsDB']->prefix('users'), 'uid', 'profile_id');
175
    $fieldHandler = $helper->getHandler('Field');
176
    $user_fields  = $fieldHandler->getUserVars();
177
    $criteria     = new Criteria('field_name', "('" . implode("', '", $user_fields) . "')", 'IN');
178
    $fieldHandler->updateAll('field_config', 0, $criteria);
179
180
    return true;
181
}
182