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

DirectoryItem   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A rename() 0 5 1
A getPath() 0 2 1
A getChmod() 0 3 1
A getAbsolutePath() 0 2 1
A move() 0 8 2
A strToValidName() 0 2 1
A setChmod() 0 2 1
A setPath() 0 2 1
A getDirectory() 0 2 1
A __toString() 0 2 1
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