Passed
Branch tests1.5 (0c23dc)
by Wanderson
01:17
created

DirectoryItem::getPath()   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 const BASE_PATH;
6
use function strToURL;
7
8
/**
9
 * Item dentro do Diretório
10
 * São outros diretório, arquivos, etc
11
 */
12
abstract class DirectoryItem implements DirectoryItemInterface {
13
14
	/** @var string */
15
	private $path;
16
17
	/** @return string */
18
	public function __toString() {
19
		return $this->toString();
20
	}
21
22
	/** @return string */
23
	public function getPath() {
24
		return $this->path;
25
	}
26
27
	/** @param string $path */
28
	protected function setPath($path) {
29
		$this->path = $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
		return new Directory(pathinfo($this->getPath(), PATHINFO_DIRNAME));
43
	}
44
45
	/**
46
	 * Renomeia
47
	 * @param string $newName Novo nome
48
	 * @return boolean
49
	 */
50
	public function rename($newName) {
51
		$oldPath = $this->getAbsolutePath();
52
		$path = $this->getDirectory()->getPath() . DIRECTORY_SEPARATOR . $newName;
53
		$this->setPath($path);
54
		return rename($oldPath, $this->getAbsolutePath());
55
	}
56
57
	/**
58
	 * Move para um novo diretório
59
	 * @param Directory $newDirectory
60
	 * @return boolean
61
	 */
62
	public function move(Directory $newDirectory) {
63
		$oldPath = $this->getAbsolutePath();
64
		$path = $newDirectory->getPath() . DIRECTORY_SEPARATOR . $this->toString();
65
		$this->setPath($path);
66
		if (!$newDirectory->exists()) {
67
			$newDirectory->create();
68
		}
69
		return rename($oldPath, $this->getAbsolutePath());
70
	}
71
72
	/**
73
	 * Converte uma string para um nome válido
74
	 * @param string $string
75
	 * @return string
76
	 */
77
	public static function strToValidName($string) {
78
		return trim(strToURL($string), '-');
79
	}
80
81
	/**
82
	 * Define a permissão ao diretório
83
	 * @param int $chmod
84
	 * @return boolean
85
	 */
86
	public function setChmod($chmod = 0755) {
87
		return @chmod($this->getAbsolutePath(), $chmod);
88
	}
89
90
	/** @return string */
91
	public function getChmod() {
92
		clearstatcache();
93
		return substr(decoct(fileperms($this->getAbsolutePath())), 2);
94
	}
95
96
}
97