Directory   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 68.89%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 64
ccs 31
cts 45
cp 0.6889
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 5 1
A clean() 0 10 3
A create() 0 11 3
B cleanFile() 0 19 7
A checkPath() 0 6 2
1
<?php
2
3
namespace Genesis\Commands\Filesystem;
4
5
6
use Genesis\Commands\Command;
7
8
/**
9
 * @author Adam Bisek <[email protected]>
10
 */
11
class Directory extends Command
12
{
13
14 7
	public function read($directory)
15
	{
16 7
		$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
17 7
		return $files;
18
	}
19
20
21 7
	public function clean($directory)
22
	{
23 7
		if (!is_dir($directory)) {
24
			$this->error("'$directory' is not an directory.");
25
		}
26 7
		foreach ($this->read($directory) as $fileInfo) {
27 6
			$this->checkPath($fileInfo->getPathName(), $directory);
28 6
			$this->cleanFile($fileInfo);
29 7
		}
30 7
	}
31
32
33 3
	public function create($dir, $chmod = '0777')
34
	{
35 3
		if (is_dir($dir)) {
36
			$this->error("Dir '$dir' already exists.");
37
		}
38 3
		$result = mkdir($dir, $chmod, TRUE);
39 3
		exec('chmod ' . escapeshellarg($chmod) . ' ' . escapeshellarg($dir)); // TODO: find workaround - native PHP chmod didnt work
40 3
		if (!$result) {
41
			$this->error("Cannot create dir '$dir'.");
42
		}
43 3
	}
44
45
46 6
	private function cleanFile(\SplFileInfo $fileInfo)
47
	{
48 6
		if ($fileInfo->isLink()) {
49 4
			$result = @unlink($fileInfo->getPathname());
50 4
			if (!$result) {
51
				$this->error("Cannot delete symlink '{$fileInfo->getPathname()}'.");
52
			}
53 6
		} elseif ($fileInfo->isDir()) {
54 5
			$result = @rmdir($fileInfo->getPathname());
55 5
			if (!$result) {
56
				$this->error("Cannot delete file '{$fileInfo->getPathname()}'.");
57
			}
58 5
		} elseif ($fileInfo->isFile()) {
59 5
			$result = @unlink($fileInfo->getPathname());
60 5
			if (!$result) {
61
				$this->error("Cannot delete file '{$fileInfo->getPathname()}'.");
62
			}
63 5
		}
64 6
	}
65
66
67 6
	private function checkPath($current, $directory)
68
	{
69 6
		if (strpos($current, $directory) !== 0) {
70
			$this->error("Cannot access directory '$current' outside working directory '$directory'.");
71
		}
72 6
	}
73
74
}