Passed
Branch tests1.5 (1e45fa)
by Wanderson
01:17
created

Directory::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\File;
4
5
use Exception;
6
use const BASE_PATH;
7
8
/**
9
 * Diretório de Arquivos
10
 *
11
 */
12
class Directory {
13
14
	/** @var string */
15
	private $path;
16
17
	/**
18
	 * Instância um diretório
19
	 * @param string $path
20
	 */
21
	public function __construct($path) {
22
		$this->path = static::toAbsolutePath($path);
23
	}
24
25
	/** @return string */
26
	public function getPath() {
27
		return $this->path;
28
	}
29
30
	/** @return string */
31
	public function getRelativePath() {
32
		return str_replace(BASE_PATH . DIRECTORY_SEPARATOR, '', $this->path);
33
	}
34
35
	/**
36
	 * Converte o caminho relativo para absoluto
37
	 * @param $relativePath
38
	 * @return string
39
	 */
40
	public static function toAbsolutePath($relativePath) {
41
		if (!preg_match('@^(([a-z0-9._\-][\/]?))+$@', $relativePath . DIRECTORY_SEPARATOR)) {
42
			throw new Exception($relativePath . ' is a invalid path.');
43
		}
44
		return BASE_PATH . DIRECTORY_SEPARATOR . $relativePath;
45
	}
46
47
	/** @return boolean */
48
	public function exists() {
49
		return is_dir($this->path);
50
	}
51
52
	/**
53
	 * Renomeia o diretório
54
	 * @param string $newPath Caminho para o novo diretório
55
	 * @return boolean
56
	 */
57
	public function rename($newPath) {
58
		$newFullPath = static::toAbsolutePath($newPath);
59
		$oldFullPath = $this->path;
60
		$this->path = $newFullPath;
61
		return rename($oldFullPath, $newFullPath);
62
	}
63
64
	/**
65
	 * Exclui o diretório e o seu conteúdo
66
	 * @return boolean
67
	 */
68
	public function delete() {
69
		$return = false;
70
		if ($this->exists()) {
71
			$this->deleteContent();
72
			$return = rmdir($this->path);
73
		}
74
		return $return;
75
	}
76
77
	/**
78
	 * Exclui o conteúdo do diretório
79
	 */
80
	protected function deleteContent() {
81
		foreach ($this->scan() as $content) {
82
			if (is_dir($this->path . DIRECTORY_SEPARATOR . $content)) {
83
				$subDirectory = new Directory($this->getRelativePath() . DIRECTORY_SEPARATOR . $content);
84
				$subDirectory->delete();
85
			} else {
86
				unlink($this->path . DIRECTORY_SEPARATOR . $content);
87
			}
88
		}
89
	}
90
91
	/**
92
	 * Cria o diretório
93
	 * @param int $chmod
94
	 * @return boolean
95
	 */
96
	public function create($chmod = 0755) {
97
		if (!$this->exists()) {
98
			@mkdir($this->path, $chmod, (boolean) STREAM_MKDIR_RECURSIVE);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

98
			/** @scrutinizer ignore-unhandled */ @mkdir($this->path, $chmod, (boolean) STREAM_MKDIR_RECURSIVE);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
			$this->chmod($chmod);
100
		}
101
		return $this->exists();
102
	}
103
104
	/**
105
	 * Retorna o conteúdo do diretório
106
	 * @return string[]
107
	 */
108
	public function scan() {
109
		return array_diff(scandir($this->path), ['.', '..']);
110
	}
111
112
	/**
113
	 * Define a permissão ao diretório
114
	 * @param int $chmod
115
	 * @return boolean
116
	 */
117
	public function chmod($chmod = 0755) {
118
		return @chmod($this->path, $chmod);
119
	}
120
121
	/** @return string */
122
	public function getPermission() {
123
		clearstatcache();
124
		return substr(decoct(fileperms($this->path)), 2);
125
	}
126
127
}
128