Completed
Push — master ( 07d0c9...44d352 )
by Marco
01:39
created

ManagerTools::recursiveUnlink()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
1
<?php namespace Comodojo\Zip;
2
3
use \Comodojo\Foundation\Utils\UniqueId;
4
use \RecursiveIteratorIterator;
5
use \RecursiveDirectoryIterator;
6
use \FilesystemIterator;
7
use \Exception;
8
9
/**
10
 * comodojo/zip - ZipArchive toolbox
11
 *
12
 * @package     Comodojo Spare Parts
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @license     MIT
15
 *
16
 * LICENSE:
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
class ManagerTools {
28
29
    public static function getTemporaryFolder(): string {
30
31
        // return UniqueId::generateCustom("zip-temp-folder");
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
33
        return "zip-temp-folder-".UniqueId::generate();
34
35
    }
36
37
    /**
38
     * @param string $folder
39
     */
40
    public static function recursiveUnlink(string $folder, bool $remove_folder = true): bool {
41
42
        try {
43
44
            self::emptyFolder($folder);
45
46
            if ( $remove_folder && rmdir($folder) === false ) {
47
                throw new Exception("Error deleting folder ".$folder);
48
            }
49
50
        } catch (Exception $e) {
51
52
            throw $e;
53
54
        }
55
56
        return true;
57
58
    }
59
60
    protected static function emptyFolder(string $folder): bool {
61
62
        $iterator = new RecursiveIteratorIterator(
63
            new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS),
64
            RecursiveIteratorIterator::CHILD_FIRST
65
        );
66
67
        foreach ( $iterator as $path ) {
68
69
            $pathname = $path->getPathname();
70
71
            if ( $path->isDir() ) {
72
73
                $action = rmdir($pathname);
74
75
            } else {
76
77
                $action = unlink($pathname);
78
79
            }
80
81
            if ( $action === false ) throw new Exception("Error deleting ".$pathname." during recursive unlink of folder ".$folder);
82
83
        }
84
85
        return true;
86
87
    }
88
89
}
90