xoops_module_update_cardealer()   D
last analyzed

Complexity

Conditions 18
Paths 33

Size

Total Lines 81
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

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

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