Folder::deleteContents()   C
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
c 5
b 3
f 1
dl 0
loc 39
rs 5.3846
cc 8
eloc 26
nc 8
nop 1
1
<?php
2
namespace nochso\Omni;
3
4
/**
5
 * Folder handles file system folders.
6
 */
7
final class Folder {
8
	/**
9
	 * Ensure a folder exists by creating it if missing and throw an exception on failure.
10
	 *
11
	 * @param string $path
12
	 * @param int    $mode Optional, defaults to 0777.
13
	 *
14
	 * @throws \RuntimeException
15
	 */
16 View Code Duplication
	public static function ensure($path, $mode = 0777) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
		if (is_dir($path)) {
18
			return;
19
		}
20
		if (@mkdir($path, $mode, true)) {
21
			return;
22
		}
23
		throw new \RuntimeException(sprintf("Unable to create folder '%s': %s", $path, error_get_last()['message']));
24
	}
25
26
	/**
27
	 * Delete a directory and all of its contents recursively.
28
	 *
29
	 * @param string $path Path of folder to delete
30
	 *
31
	 * @throws \RuntimeException Thrown when something could not be deleted.
32
	 */
33 View Code Duplication
	public static function delete($path) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
		if (!is_dir($path)) {
35
			return;
36
		}
37
		self::deleteContents($path);
38
		if (@rmdir($path) === false) {
39
			throw new \RuntimeException(sprintf("Unable to delete folder '%s': %s", $path, error_get_last()['message']));
40
		}
41
	}
42
43
	/**
44
	 * deleteContents of a folder recursively, but not the folder itself.
45
	 *
46
	 * On Windows systems this will try to remove the read-only attribute if needed.
47
	 *
48
	 * @param string $path Path of folder whose contents will be deleted
49
	 *
50
	 * @throws \RuntimeException Thrown when something could not be deleted.
51
	 */
52
	public static function deleteContents($path) {
53
		if (!is_dir($path)) {
54
			return;
55
		}
56
		/** @var \SplFileInfo[] $files */
57
		$files = new \RecursiveIteratorIterator(
58
			new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
59
			\RecursiveIteratorIterator::CHILD_FIRST
60
		);
61
		foreach ($files as $fileInfo) {
62
			if ($fileInfo->isDir()) {
63
				if (@rmdir($fileInfo->getRealPath()) === false) {
64
					throw new \RuntimeException(
65
						sprintf(
66
							"Unable to delete child folder '%s' in '%s': %s",
67
							$fileInfo->getFilename(),
68
							$fileInfo->getPathname(),
69
							error_get_last()['message']
70
						)
71
					);
72
				}
73
			} elseif (@unlink($fileInfo->getRealPath()) === false) {
74
				if (OS::isWindows()) {
75
					Exec::create('attrib', '-R', $fileInfo->getRealPath())->run();
76
					if (@unlink($fileInfo->getPathname())) {
77
						continue;
78
					}
79
				}
80
				throw new \RuntimeException(
81
					sprintf(
82
						"Unable to delete file '%s' in folder '%s': %s",
83
						$fileInfo->getFilename(),
84
						$fileInfo->getPathname(),
85
						error_get_last()['message']
86
					)
87
				);
88
			}
89
		}
90
	}
91
}
92