Passed
Branch tests1.5 (2e49f7)
by Wanderson
01:30
created

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