Passed
Pull Request — master (#11)
by Michael
01:53
created

xoops_module_update_xsitemap()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 89
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 39
nc 4
nop 2
dl 0
loc 89
rs 6.6166
c 2
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
 * 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
 * @package
16
 * @since
17
 * @author       XOOPS Development Team
18
 */
19
20
use XoopsModules\Xsitemap;
21
22
// require_once  dirname(__DIR__) . '/class/Utility.php';
23
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
24
    || !$GLOBALS['xoopsUser']->isAdmin()) {
25
    exit('Restricted access' . PHP_EOL);
26
}
27
/**
28
 * Prepares system prior to attempting to update module
29
 *
30
 * @param \XoopsModule $module
31
 *
32
 * @return bool true if successfully ready to update module, false if not
33
 */
34
function xoops_module_pre_update_xsitemap(\XoopsModule $module)
35
{
36
    /** @var Xsitemap\Helper $helper */
37
    /** @var Xsitemap\Utility $utility */
38
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
39
    $helper        = Xsitemap\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
40
    $utility       = new Xsitemap\Utility();
41
    $xoopsSuccess  = $utility::checkVerXoops($module);
42
    $phpSuccess    = $utility::checkVerPhp($module);
43
    return $xoopsSuccess && $phpSuccess;
44
}
45
46
/**
47
 * Functions to upgrade from previous version of the module
48
 *
49
 * @param \XoopsModule $module
50
 * @param int|null     $previousVersion
51
 * @return bool true if successfully updated module, false if not
52
 * @internal param int $curr_version version number of module currently installed
53
 */
54
function xoops_module_update_xsitemap(\XoopsModule $module, $previousVersion = null)
55
{
56
    /*======================================================================
57
        //----------------------------------------------------------------
58
        // Remove xSitemap uploads folder (and all subfolders) if they exist
59
        //----------------------------------------------------------------*
60
        $utility = new Xsitemap\Utility();
61
        if (!class_exists($utility)) {
62
            xoops_load('utility', $moduleDirName);
63
        }
64
65
        // Recursively delete directories
66
        $xsUploadDir = realpath(XOOPS_UPLOAD_PATH . "/" . $module->dirname());
67
        $success = $utility::rrmdir($xsUploadDir);
68
        if (true !== $success) {
69
            \Xmf\Language::load('admin', $module->dirname());
70
            $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $xsUploadDir));
71
        }
72
        return $success;
73
    ======================================================================*/
74
    $moduleDirName      = basename(dirname(__DIR__));
75
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
76
    /** @var Xsitemap\Helper $helper */ /** @var Xsitemap\Utility $utility */
77
    /** @var Xsitemap\Common\Configurator $configurator */
78
    $helper       = Xsitemap\Helper::getInstance();
79
    $utility      = new Xsitemap\Utility();
80
    $configurator = new Xsitemap\Common\Configurator();
0 ignored issues
show
Unused Code introduced by
The assignment to $configurator is dead and can be removed.
Loading history...
81
    //-----------------------------------------------------------------------
82
    // Upgrade for Xsitemap < 1.54
83
    //-----------------------------------------------------------------------
84
    $success = true;
85
    $helper->loadLanguage('modinfo');
86
    $helper->loadLanguage('admin');
87
    if ($previousVersion < 154) {
88
        //----------------------------------------------------------------
89
        // Remove previous css & images directories since they've been relocated to ./assets
90
        // Also remove uploads directories since they're no longer used
91
        //----------------------------------------------------------------
92
        $old_directories = [
93
            $helper->path('css/'),
94
            $helper->path('js/'),
95
            $helper->path('images/'),
96
            XOOPS_UPLOAD_PATH . '/' . $module->dirname(),
97
        ];
98
        foreach ($old_directories as $old_dir) {
99
            $dirInfo = new \SplFileInfo($old_dir);
100
            if ($dirInfo->isDir()) {
101
                // The directory exists so delete it
102
                if (false === $utility::rrmdir($old_dir)) {
103
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir));
104
                    return false;
105
                }
106
            }
107
            unset($dirInfo);
108
        }
109
        //-----------------------------------------------------------------------
110
        // Remove ./template/*.html (except index.html) files since they've
111
        // been replaced by *.tpl files
112
        // Note: this will also remove /template/xsitemap_style.html since it's no longer used
113
        //-----------------------------------------------------------------------
114
        $path       = $helper->path('templates/');
115
        $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
116
        $iterator   = new RegexIterator($unfiltered, "/.*\.html/");
117
        foreach ($iterator as $name => $fObj) {
118
            if ($fObj->isFile() && ('index.html' !== $fObj->getFilename())) {
119
                if (false === ($success = unlink($fObj->getPathname()))) {
120
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $fObj->getPathname()));
121
                    return false;
122
                }
123
            }
124
        }
125
        //-----------------------------------------------------------------------
126
        // Now remove a some misc files that were renamed or deprecated
127
        //-----------------------------------------------------------------------
128
        $oldFiles = [
129
            $helper->path('include/install.php'),
130
            $helper->path('class/module.php'),
131
            $helper->path('class/menu.php'),
132
        ];
133
        foreach ($oldFiles as $file) {
134
            if (is_file($file)) {
135
                if (false === ($delOk = unlink($file))) {
136
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $file));
137
                }
138
                $success = $success && $delOk;
139
            }
140
        }
141
    }
142
    return $success;
143
}
144