xoops_module_update_xlanguage()   D
last analyzed

Complexity

Conditions 19
Paths 33

Size

Total Lines 79
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 19
eloc 39
c 5
b 0
f 1
nc 33
nop 2
dl 0
loc 79
rs 4.5166

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\Xlanguage;
19
use XoopsModules\Xlanguage\{Common,
20
    Common\Configurator,
21
    Helper,
22
    Utility
23
};
24
25
/** @var Helper $helper */
26
/** @var Utility $utility */
27
/** @var Configurator $configurator */
28
/** @var Migrate $migrator */
29
30
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
31
    || !$GLOBALS['xoopsUser']->isAdmin()) {
32
    exit('Restricted access' . PHP_EOL);
33
}
34
35
36
37
/**
38
 * Prepares system prior to attempting to install module
39
 * @param \XoopsModule $module {@link XoopsModule}
40
 *
41
 * @return bool true if ready to install, false if not
42
 */
43
function xoops_module_pre_update_xlanguage(\XoopsModule $module)
44
{
45
    $utility = new Utility();
46
47
    $xoopsSuccess = $utility::checkVerXoops($module);
48
    $phpSuccess   = $utility::checkVerPhp($module);
49
50
    return $xoopsSuccess && $phpSuccess;
51
}
52
53
/**
54
 * Performs tasks required during update of the module
55
 * @param \XoopsModule    $module {@link XoopsModule}
56
 * @param null|string|int $previousVersion
57
 *
58
 * @return bool true if update successful, false if not
59
 */
60
function xoops_module_update_xlanguage(\XoopsModule $module, $previousVersion = null)
61
{
62
    $moduleDirName = \basename(\dirname(__DIR__));
63
64
    $utility      = new Utility();
65
    $configurator = new Configurator();
66
67
    if ($previousVersion < 310) {
68
        //delete old HTML templates
69
        if (count($configurator->templateFolders) > 0) {
70
            foreach ($configurator->templateFolders as $folder) {
71
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
72
                if (is_dir($templateFolder)) {
73
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
74
                    if (is_array($templateList)) {
75
                        foreach ($templateList as $k => $v) {
76
                            $fileInfo = new \SplFileInfo($templateFolder . $v);
77
                            if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
78
                                if (is_file($templateFolder . $v)) {
79
                                    unlink($templateFolder . $v);
80
                                }
81
                            }
82
                        }
83
                    }
84
                }
85
            }
86
        }
87
88
        //  ---  DELETE OLD FILES ---------------
89
        if (count($configurator->oldFiles) > 0) {
90
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
91
            foreach (array_keys($configurator->oldFiles) as $i) {
92
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
93
                if (is_file($tempFile)) {
94
                    unlink($tempFile);
95
                }
96
            }
97
        }
98
99
        //  ---  DELETE OLD FOLDERS ---------------
100
        xoops_load('XoopsFile');
101
        if (count($configurator->oldFolders) > 0) {
102
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
103
            foreach (array_keys($configurator->oldFolders) as $i) {
104
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
105
                /* @var XoopsObjectHandler $folderHandler */
106
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
107
                $folderHandler->delete($tempFolder);
108
            }
109
        }
110
111
        //  ---  CREATE FOLDERS ---------------
112
        if (count($configurator->uploadFolders) > 0) {
113
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
114
            foreach (array_keys($configurator->uploadFolders) as $i) {
115
                $utility::createFolder($configurator->uploadFolders[$i]);
116
            }
117
        }
118
119
        //  ---  COPY blank.png FILES ---------------
120
        if (count($configurator->copyBlankFiles) > 0) {
121
            $file = \dirname(__DIR__) . '/assets/images/blank.png';
122
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
123
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
124
                $utility::copyFile($file, $dest);
125
            }
126
        }
127
128
        //delete .html entries from the tpl table
129
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` 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