Building::copyDir()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 5
nop 2
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
1
<?php namespace XoopsModules\Tdmcreate;
2
3
use XoopsModules\Tdmcreate;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * Building class.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.x
22
 *
23
 * @author          TDM TEAM DEV MODULE
24
 *
25
 * @version         $Id: building.php 12425 2014-02-23 22:40:09Z timgno $
26
 */
27
28
/**
29
 * Class Building.
30
 */
31
class Building
32
{
33
    /**
34
     *  @static function getInstance
35
     *
36
     *  @param null
37
     *
38
     * @return Building
39
     */
40
    public static function getInstance()
41
    {
42
        static $instance = false;
43
        if (!$instance) {
44
            $instance = new self();
45
        }
46
47
        return $instance;
48
    }
49
50
    /**
51
     * @param bool $action
52
     *
53
     * @return \XoopsThemeForm
54
     */
55
    public function getForm($action = false)
56
    {
57
        $tc = Tdmcreate\Helper::getInstance();
58
        if (false === $action) {
59
            $action = $_SERVER['REQUEST_URI'];
60
        }
61
        xoops_load('XoopsFormLoader');
62
        $form = new \XoopsThemeForm(_AM_TDMCREATE_ADMIN_CONST, 'buildform', $action, 'post', true);
63
        $form->setExtra('enctype="multipart/form-data"');
64
        $moduleObj = $tc->getHandler('modules')->getObjects(null);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $moduleObj = $tc->getHandler('modules')->/** @scrutinizer ignore-call */ getObjects(null);
Loading history...
65
        $mod_select = new \XoopsFormSelect(_AM_TDMCREATE_CONST_MODULES, 'mod_id', 'mod_id');
66
        $mod_select->addOption('', _AM_TDMCREATE_BUILD_MODSELOPT);
67
        foreach ($moduleObj as $mod) {
68
            $mod_select->addOption($mod->getVar('mod_id'), $mod->getVar('mod_name'));
69
        }
70
        $form->addElement($mod_select, true);
71
72
        $form->addElement(new \XoopsFormHidden('op', 'build'));
73
        $form->addElement(new \XoopsFormButton(_REQUIRED . ' <sup class="red bold">*</sup>', 'submit', _SUBMIT, 'submit'));
74
75
        return $form;
76
    }
77
78
    /**
79
     * @param string $dir
80
     * @param string $pattern
81
     */
82
    public function clearDir($dir, $pattern = '*')
83
    {
84
        // Find all files and folders matching pattern
85
        $files = glob($dir . "/$pattern");
86
        // Interate thorugh the files and folders
87
        foreach ($files as $file) {
88
            // if it's a directory then re-call clearDir function to delete files inside this directory
89
            if (is_dir($file) && !in_array($file, ['..', '.'])) {
90
                // Remove the directory itself
91
                $this->clearDir($file, $pattern);
92
            } elseif ((__FILE__ != $file) && is_file($file)) {
93
                // Make sure you don't delete the current script
94
                unlink($file);
95
            }
96
        }
97
        rmdir($dir);
98
    }
99
100
    /**
101
     * @param string $src
102
     * @param string $dst
103
     */
104
    public function copyDir($src, $dst)
105
    {
106
        $dir = opendir($src);
107
        if (!mkdir($dst) && !is_dir($dst)) {
108
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $dst));
109
        }
110
        while (false !== ($file = readdir($dir))) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        while (false !== ($file = readdir(/** @scrutinizer ignore-type */ $dir))) {
Loading history...
111
            if (('.' !== $file) && ('..' !== $file)) {
112
                if (is_dir($src . '/' . $file)) {
113
                    // Copy the directory itself
114
                    $this->copyDir($src . '/' . $file, $dst . '/' . $file);
115
                } else {
116
                    // Make sure you copy the current script
117
                    copy($src . '/' . $file, $dst . '/' . $file);
118
                }
119
            }
120
        }
121
        closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
122
    }
123
}
124