xoops_module_update_xoopsmembers()   D
last analyzed

Complexity

Conditions 18
Paths 33

Size

Total Lines 80
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 18
eloc 39
c 3
b 0
f 0
nc 33
nop 2
dl 0
loc 80
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\Xoopsmembers;
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_xoopsmembers(\XoopsModule $module)
32
{
33
    /** @var Xoopsmembers\Utility $utility */
34
    $utility = new Xoopsmembers\Utility();
35
36
    $xoopsSuccess = $utility::checkVerXoops($module);
37
    $phpSuccess   = $utility::checkVerPhp($module);
38
39
    return $xoopsSuccess && $phpSuccess;
40
}
41
42
/**
43
 * Performs tasks required during update of the module
44
 * @param \XoopsModule $module {@link XoopsModule}
45
 * @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...
46
 *
47
 * @return bool true if update successful, false if not
48
 */
49
function xoops_module_update_xoopsmembers(\XoopsModule $module, $previousVersion = null)
50
{
51
    $moduleDirName      = \basename(\dirname(__DIR__));
52
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
53
54
    /** @var Xoopsmembers\Utility $utility */
55
    /** @var Xoopsmembers\Common\Configurator $configurator */
56
    $utility      = new Xoopsmembers\Utility();
57
    $configurator = new Xoopsmembers\Common\Configurator();
58
59
    if ($previousVersion < 105) {
60
        //delete old HTML templates
61
        if (count($configurator->templateFolders) > 0) {
62
            foreach ($configurator->templateFolders as $folder) {
63
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
64
                if (is_dir($templateFolder)) {
65
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
66
                    foreach ($templateList as $k => $v) {
67
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
68
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
69
                            if (is_file($templateFolder . $v)) {
70
                                unlink($templateFolder . $v);
71
                            }
72
                        }
73
                    }
74
                }
75
            }
76
        }
77
78
        //  ---  DELETE OLD FILES ---------------
79
        if (count($configurator->oldFiles) > 0) {
80
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
81
            foreach (array_keys($configurator->oldFiles) as $i) {
82
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
83
                if (is_file($tempFile)) {
84
                    unlink($tempFile);
85
                }
86
            }
87
        }
88
89
        //  ---  DELETE OLD FOLDERS ---------------
90
        xoops_load('XoopsFile');
91
        if (count($configurator->oldFolders) > 0) {
92
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
93
            foreach (array_keys($configurator->oldFolders) as $i) {
94
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
95
                /** @var XoopsObjectHandler $folderHandler */
96
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
97
                $folderHandler->delete($tempFolder);
98
            }
99
        }
100
101
        //  ---  CREATE FOLDERS ---------------
102
        if (count($configurator->uploadFolders) > 0) {
103
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
104
            foreach (array_keys($configurator->uploadFolders) as $i) {
105
                $utility::createFolder($configurator->uploadFolders[$i]);
106
            }
107
        }
108
109
        //  ---  COPY blank.png FILES ---------------
110
        if (count($configurator->copyBlankFiles) > 0) {
111
            $file = \dirname(__DIR__) . '/assets/images/blank.png';
112
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
113
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
114
                $utility::copyFile($file, $dest);
115
            }
116
        }
117
118
        //delete .html entries from the tpl table
119
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
120
        $GLOBALS['xoopsDB']->queryF($sql);
121
122
        /** @var \XoopsGroupPermHandler $grouppermHandler */
123
        $grouppermHandler = xoops_getHandler('groupperm');
124
125
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
126
    }
127
128
    return true;
129
}
130