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

DirectoryItem::setDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\File;
4
5
use const BASE_PATH;
6
7
/**
8
 * Item dentro do Diretório
9
 * São outros diretório, arquivos, etc
10
 */
11
abstract class DirectoryItem implements DirectoryItemInterface {
12
13
	const REGEXP_PATH = '@^(([a-z0-9._\-][\/]?))+$@';
14
	const REGEXP_NAME = '@^(([a-z0-9._\-]?))+$@';
15
16
	/** @var string */
17
	private $path;
18
19
	/** @var Directory */
20
	private $directory;
21
22
	/** @return string */
23
	public function __toString() {
24
		return $this->toString();
25
	}
26
27
	/** @return string */
28
	public function getPath() {
29
		return $this->path;
30
	}
31
32
	/** @return string */
33
	public function getAbsolutePath() {
34
		return BASE_PATH . DIRECTORY_SEPARATOR . $this->path;
35
	}
36
37
	/**
38
	 * Retorna o diretório pai
39
	 * @return Directory
40
	 */
41
	public function getDirectory() {
42
		if (is_null($this->directory)) {
43
			$this->directory = new Directory(pathinfo($this->getPath(), PATHINFO_DIRNAME));
44
		}
45
		return $this->directory;
46
	}
47
48
	/** @param string $path */
49
	protected function setPath($path) {
50
		$this->path = $path;
51
	}
52
53
	protected function setDirectory(Directory $directory) {
54
		$this->directory = $directory;
55
		$path = $directory->getPath() . DIRECTORY_SEPARATOR . $this->toString();
56
		$this->setPath($path);
57
	}
58
59
	/** @param string */
60
	protected function setName($name) {
61
		$path = $this->getDirectory()->getPath() . DIRECTORY_SEPARATOR . $name;
62
		$this->setPath($path);
63
	}
64
65
	/**
66
	 * Renomeia
67
	 * @param string $newName Novo nome
68
	 * @return boolean
69
	 */
70
	public function rename($newName) {
71
		$oldPath = $this->getAbsolutePath();
72
		$this->setName($newName);
73
		return rename($oldPath, $this->getAbsolutePath());
74
	}
75
76
	/**
77
	 * Move para um novo diretório
78
	 * @param Directory $newDirectory
79
	 * @return boolean
80
	 */
81
	public function move(Directory $newDirectory) {
82
		$oldPath = $this->getAbsolutePath();
83
		$this->setDirectory($newDirectory);
84
		if (!$this->getDirectory()->exists()) {
85
			$this->getDirectory()->create();
86
		}
87
		return rename($oldPath, $this->getAbsolutePath());
88
	}
89
90
	/**
91
	 * Define a permissão ao diretório
92
	 * @param int $chmod
93
	 * @return boolean
94
	 */
95
	public function setChmod($chmod = 0755) {
96
		return @chmod($this->getAbsolutePath(), $chmod);
97
	}
98
99
	/** @return string */
100
	public function getChmod() {
101
		clearstatcache();
102
		return substr(decoct(fileperms($this->getAbsolutePath())), 2);
103
	}
104
105
}
106