CreateStructure::makeDir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files;
4
5
use XoopsModules\Modulebuilder;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * modulebuilder module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops https://xoops.org 
25
 *                  Goffy https://myxoops.org
26
 *
27
 */
28
29
/**
30
 * Class ModuleBuilderStructure.
31
 */
32
class CreateStructure
33
{
34
    /**
35
     * @var string
36
     */
37
    private $moduleName;
38
    /**
39
     * @var string
40
     */
41
    private $folderName;
42
    /**
43
     * @var string
44
     */
45
    private $fileName;
46
    /**
47
     * @var mixed
48
     */
49
    private $uploadPath;
50
51
    /**
52
     * @public function constructor class
53
     *
54
     * @param null
55
     */
56
    public function __construct()
57
    {
58
    }
59
60
    /**
61
     * @static function getInstance
62
     *
63
     * @param null
64
     *
65
     * @return Modulebuilder\Files\CreateStructure
66
     */
67
    public static function getInstance()
68
    {
69
        static $instance = false;
70
        if (!$instance) {
71
            $instance = new self();
72
        }
73
74
        return $instance;
75
    }
76
77
    /**
78
     * @protected function setUploadPath
79
     *
80
     * @param $path
81
     */
82
    protected function setUploadPath($path)
83
    {
84
        $this->uploadPath = $path;
85
    }
86
87
    /**
88
     * @protected function getUploadPath
89
     *
90
     * @return string $path
91
     */
92
    protected function getUploadPath()
93
    {
94
        return $this->uploadPath;
95
    }
96
97
    /**
98
     * @protected function setModuleName
99
     * @param $moduleName
100
     */
101
    protected function setModuleName($moduleName)
102
    {
103
        $this->moduleName = $moduleName;
104
    }
105
106
    /**
107
     * @protected function getModuleName
108
     *
109
     * @return string $moduleName
110
     */
111
    protected function getModuleName()
112
    {
113
        return $this->moduleName;
114
    }
115
116
    /**
117
     * @private function setFolderName
118
     *
119
     * @param $folderName
120
     */
121
    private function setFolderName($folderName)
122
    {
123
        $this->folderName = $folderName;
124
    }
125
126
    /**
127
     * @private function getFolderName
128
     * @return string $folderName
129
     */
130
    private function getFolderName()
131
    {
132
        return $this->folderName;
133
    }
134
135
    /**
136
     * @private function setFileName
137
     *
138
     * @param $fileName
139
     */
140
    private function setFileName($fileName)
141
    {
142
        $this->fileName = $fileName;
143
    }
144
145
    /**
146
     * @private function getFileName
147
     *
148
     * @return string $fileName
149
     */
150
    private function getFileName()
151
    {
152
        return $this->fileName;
153
    }
154
155
    /**
156
     * @public function isDir
157
     *
158
     * @param $dname
159
     */
160
    public function isDir($dname)
161
    {
162
        if (!\is_dir($dname)) {
163
            if (!\mkdir($dname, 0755) && !\is_dir($dname)) {
164
                throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dname));
165
            }
166
            chmod($dname, 0755);
167
        } else {
168
            chmod($dname, 0755);
169
        }
170
    }
171
172
    /**
173
     * @protected function makeDir
174
     *
175
     * @param string $dir
176
     */
177
    protected function makeDir($dir)
178
    {
179
        $this->isDir(\trim($dir));
180
    }
181
182
    /**
183
     * @protected function isDirEmpty
184
     *
185
     * @param string $dir
186
     *
187
     * @return string
188
     */
189
    public function isDirEmpty($dir)
190
    {
191
        $content = [];
192
        $handle  = \opendir($dir);
193
        while (false !== ($entry = \readdir($handle))) {
194
            if ('.' !== $entry && '..' !== $entry) {
195
                $content[] = $entry;
196
            }
197
        }
198
        \closedir($handle);
199
        if (\count($content) > 0) {
200
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type string.
Loading history...
201
        }
202
203
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
204
    }
205
206
    /**
207
     * @public function addFolderPath
208
     *
209
     * @param string      $folderName
210
     * @param bool|string $fileName
211
     * @return string
212
     */
213
    private function addFolderPath($folderName, $fileName = false)
214
    {
215
        $this->setFolderName($folderName);
216
        if ($fileName) {
217
            $this->setFileName($fileName);
218
            $ret = $this->getUploadPath() . DS . $this->getModuleName() . DS . $this->getFolderName() . DS . $this->getFileName();
219
        } else {
220
            $ret = $this->getUploadPath() . DS . $this->getModuleName() . DS . $this->getFolderName();
221
        }
222
223
        return $ret;
224
    }
225
226
    /**
227
     * @public function makeDirInModule
228
     *
229
     * @param string $dirName
230
     */
231
    public function makeDirInModule($dirName)
232
    {
233
        $fname = $this->addFolderPath($dirName);
234
        $this->makeDir($fname);
235
    }
236
237
    /**
238
     * @public function makeDir & copy file
239
     *
240
     * @param string $folderName
241
     * @param string $fromFile
242
     * @param string $toFile
243
     */
244
    public function makeDirAndCopyFile($folderName, $fromFile, $toFile)
245
    {
246
        $dname = $this->addFolderPath($folderName);
247
        $this->makeDir($dname);
248
        $this->copyFile($folderName, $fromFile, $toFile);
249
    }
250
251
    /**
252
     * @public function copy file
253
     *
254
     * @param string $folderName
255
     * @param string $fromFile
256
     * @param string $toFile
257
     */
258
    public function copyFile($folderName, $fromFile, $toFile)
259
    {
260
        $dname = $this->addFolderPath($folderName);
261
        $fname = $this->addFolderPath($folderName, $toFile);
262
        $this->setCopy($dname, $fromFile, $fname);
263
    }
264
265
    /**
266
     * @public function setCopy
267
     *
268
     * @param string $dname
269
     * @param string $fromFile
270
     * @param string $fname
271
     */
272
    public function setCopy($dname, $fromFile, $fname)
273
    {
274
        if (\is_dir($dname)) {
275
            chmod($dname, 0777);
276
            \copy($fromFile, $fname);
277
        } else {
278
            $this->makeDir($dname);
279
            \copy($fromFile, $fname);
280
        }
281
    }
282
}
283