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