Passed
Branch tests1.5 (81ba23)
by Wanderson
01:11
created

Directory::rename()   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 1
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 extends DirectoryItem {
13
14
	/**
15
	 * Instância um diretório
16
	 * @param string $path Caminho relativo
17
	 */
18
	public function __construct($path) {
19
		$this->setPath($path);
20
	}
21
22
	/** @return string */
23
	public function __toString() {
24
		return $this->toString();
25
	}
26
27
	/**
28
	 * @param string $path Caminho relativo
29
	 * @throws Exception
30
	 */
31
	protected function setPath($path) {
32
		if (!preg_match(static::REGEXP_PATH, $path . DIRECTORY_SEPARATOR)) {
33
			throw new Exception($path . ' is a invalid directory path.');
34
		}
35
		parent::setPath($path);
36
	}
37
38
	/** @return string */
39
	public function getName() {
40
		return pathinfo($this->getPath(), PATHINFO_BASENAME);
41
	}
42
43
	/** @return string */
44
	public function toString() {
45
		return $this->getName();
46
	}
47
48
	/** @return boolean */
49
	public function exists() {
50
		return is_dir($this->getAbsolutePath());
51
	}
52
53
	/**
54
	 * Exclui o diretório e o seu conteúdo
55
	 * @return boolean
56
	 */
57
	public function delete() {
58
		$success = false;
59
		if ($this->exists()) {
60
			$this->clear();
61
			$success = rmdir($this->getAbsolutePath());
62
		}
63
		return $success;
64
	}
65
66
	/**
67
	 * Exclui apenas o conteúdo do diretório
68
	 */
69
	public function clear() {
70
		foreach ($this->getItemsName() as $content) {
71
			if (is_dir($this->getAbsolutePath() . DIRECTORY_SEPARATOR . $content)) {
72
				$subDirectory = new Directory($this->getPath() . DIRECTORY_SEPARATOR . $content);
73
				$subDirectory->delete();
74
			} else {
75
				unlink($this->getAbsolutePath() . DIRECTORY_SEPARATOR . $content);
76
			}
77
		}
78
	}
79
80
	/**
81
	 * Cria o diretório
82
	 * @param int $chmod
83
	 * @return boolean
84
	 * @throws Exception
85
	 */
86
	public function create($chmod = 0755) {
87
		if (!$this->exists()) {
88
			if (@mkdir($this->getAbsolutePath(), $chmod, (boolean) STREAM_MKDIR_RECURSIVE) === false) {
89
				throw new Exception('The directory ' . $this->getPath() . ' could not be created.');
90
			}
91
			$this->setChmod($chmod);
92
		}
93
		return $this->exists();
94
	}
95
96
	/** @return boolean */
97
	public function isEmpty() {
98
		return (count($this->getItemsName()) == 0);
99
	}
100
101
	/**
102
	 * Retorna nome dos itens dentro do diretório (em ordem alfabética)
103
	 * @return string[]
104
	 */
105
	public function getItemsName() {
106
		return array_values(array_diff(scandir($this->getAbsolutePath()), ['.', '..']));
107
	}
108
109
	/**
110
	 * Retorna os itens dentro do diretório (em ordem alfabética)
111
	 * @return Directory[]|File[]
112
	 */
113
	public function getItems() {
114
		$items = [];
115
		foreach ($this->getItemsName() as $itemName) {
116
			$itemPath = $this->getPath() . DIRECTORY_SEPARATOR . $itemName;
117
			if (is_dir(BASE_PATH . DIRECTORY_SEPARATOR . $itemPath)) {
118
				$items[] = new Directory($itemPath);
119
			} else {
120
				$items[] = new File($itemPath);
121
			}
122
		}
123
		return $items;
124
	}
125
126
}
127