Failed Conditions
Pull Request — master (#3)
by Chad
01:44
created

File::delete()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 7
nop 1
1
<?php
2
/**
3
 * Defines the \TraderInteractive\Util\File class.
4
 */
5
6
namespace TraderInteractive\Util;
7
8
/**
9
 * Class of static file utility functions.
10
 */
11
final class File
12
{
13
    /**
14
     * Recursively deletes directory contents
15
     *
16
     * @param string $directoryPath absolute path of directory
17
     *
18
     * @return void
19
     *
20
     * @throws \InvalidArgumentException if $directoryPath is not a string
21
     * @throws \Exception if file cannot be deleted
22
     * @throws \Exception if directory cannot be deleted
23
     * @throws \Exception if $directoryPath cannot be listed
24
     */
25
    public static function deleteDirectoryContents($directoryPath)
26
    {
27
        if (!is_string($directoryPath)) {
28
            throw new \InvalidArgumentException('$directoryPath is not a string');
29
        }
30
31
        $paths = scandir($directoryPath);
32
        if ($paths === false) {
33
            throw new \Exception("cannot list directory '{$directoryPath}'");
34
        }
35
36
        foreach ($paths as $path) {
37
            if ($path === '.' || $path === '..') {
38
                continue;
39
            }
40
41
            $fullPath = "{$directoryPath}/{$path}";
42
43
            if (is_dir($fullPath)) {
44
                self::deleteDirectoryContents($fullPath);//RECURSIVE CALL
45
                if (!rmdir($fullPath)) {
46
                    throw new \Exception("cannot delete '{$fullPath}'", 1);
47
                }
48
            } else {
49
                if (!unlink($fullPath)) {
50
                    throw new \Exception("cannot delete '{$fullPath}'", 2);
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * Deletes the given file specified by $path
58
     *
59
     * @param string $path path to the file to be deleted
60
     *
61
     * @return void
62
     *
63
     * @throws \InvalidArgumentException if $path is not a string or is whitespace
64
     * throws \Exception if unlink returns false
65
     */
66
    public static function delete($path)
67
    {
68
        if (!is_string($path) || trim($path) === '') {
69
            throw new \InvalidArgumentException('$path is not a string or is whitespace');
70
        }
71
72
        if (!file_exists($path)) {
73
            return;
74
        }
75
76
        try {
77
            if (unlink($path) === false) {
78
                throw new \Exception("unlink returned false for '{$path}'");
79
            }
80
        } catch (\Exception $e) {
81
            if (file_exists($path)) {
82
                throw $e;
83
            }
84
        }
85
    }
86
87
    /**
88
     * Recursively deletes the given directory path until a non-empty directory is found or the $stopAtPath is reached.
89
     *
90
     * @param string $deletePath The empty directory path to delete.
91
     * @param string $stopAtPath The point at which the deletion should stop. Defaults to /.
92
     *
93
     * @return void
94
     */
95
    public static function deletePathIfEmpty($deletePath, $stopAtPath = '/')
96
    {
97
        if (!file_exists($deletePath)) {
98
            return;
99
        }
100
101
        if (realpath($deletePath) === realpath($stopAtPath)) {
102
            return;
103
        }
104
105
        $handle = dir($deletePath);
106
        for ($entry = $handle->read(); $entry !== false; $entry = $handle->read()) {
107
            if ($entry == '.' || $entry == '..') {
108
                continue;
109
            }
110
111
            //dir not empty
112
            $handle->close();
113
            return;
114
        }
115
116
        rmdir($deletePath);
117
        $handle->close();
118
119
        //RECURSION!!!
120
        self::deletePathIfEmpty(dirname($deletePath), $stopAtPath);
121
    }
122
}
123