Passed
Pull Request — master (#21)
by Wanderson
03:27
created

Directory::setPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Repositories\Filesystem;
4
5
use const BASE_PATH;
6
use Exception;
7
8
/**
9
 * Diretório de Arquivos
10
 */
11
class Directory extends Storable
12
{
13
	/**
14
	 * Instância um diretório
15
	 * @param string $path Caminho relativo
16
	 */
17
	public function __construct($path)
18
	{
19
		$this->setPath($path);
20
	}
21
22
	/** @return bool */
23
	public function exists()
24
	{
25
		return is_dir($this->getAbsolutePath());
26
	}
27
28
	/**
29
	 * @param string $path Caminho relativo
30
	 * @throws Exception
31
	 */
32
	protected function setPath($path)
33
	{
34
		if (!preg_match(static::REGEXP_PATH, $path . static::DS)) {
35
			throw new Exception($path . ' is a invalid directory path.');
36
		}
37
		parent::setPath($path);
38
	}
39
40
	/** @return bool */
41
	public function isEmpty()
42
	{
43
		return count($this->getChildrenNames()) == 0;
44
	}
45
46
	/**
47
	 * Retorna nome dos itens dentro do diretório (em ordem alfabética)
48
	 * @return string[]
49
	 */
50
	public function getChildrenNames()
51
	{
52
		$items = (array) scandir($this->getAbsolutePath());
53
54
		return array_values(array_diff($items, ['.', '..']));
55
	}
56
57
	/**
58
	 * Retorna os itens dentro do diretório (em ordem alfabética)
59
	 * @return Storable[]
60
	 */
61
	public function getChildren()
62
	{
63
		$items = [];
64
		foreach ($this->getChildrenNames() as $itemName) {
65
			$itemPath = $this->getPath() . static::DS . $itemName;
66
			if (is_dir(BASE_PATH . static::DS . $itemPath)) {
67
				$items[] = new Directory($itemPath);
68
			} else {
69
				$items[] = new File($itemPath);
70
			}
71
		}
72
73
		return $items;
74
	}
75
}
76