Completed
Push — master ( 89f95d...43ee50 )
by Wanderson
02:15
created

Directory::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\File;
4
5
use Win\Mvc\Application;
6
7
/**
8
 * Diretorio de arquivos
9
 */
10
class Directory {
11
12
	private $path;
13
14
	public function __construct($path) {
15
		$this->path = str_replace(['///', '//'], ['/', '/'], $path . '/');
16
	}
17
18
	public function __toString() {
19
		return $this->path;
20
	}
21
22
	public function getPath() {
23
		return $this->path;
24
	}
25
26
	public function setPath($path) {
27
		$this->path = $path;
28
	}
29
30
	/**
31
	 * Cria o diretorio para salvar a imagem
32
	 * @param string $pathetorio Caminho do novo diretorio
0 ignored issues
show
Bug introduced by
There is no parameter named $pathetorio. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
	 * @param int $chmod permissões deste diretório
34
	 * @return boolean Retora TRUE caso obtenha algum sucesso
35
	 */
36
	public function create($chmod = 0755) {
37
		if (!file_exists($this->path)) {
38
			$success = @mkdir($this->path, $chmod, STREAM_MKDIR_RECURSIVE);
39
			return $success;
40
		}
41
		if (Application::app()->isLocalHost()) {
0 ignored issues
show
Bug introduced by
The method isLocalHost() does not seem to exist on object<Win\Mvc\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
			chmod($this->path, 0777);
43
		}
44
		return false;
45
	}
46
47
	/**
48
	 * Renomeia o diretorio
49
	 * @param string $newName Caminho para o novo diretorio
50
	 * @return boolean
51
	 */
52
	public function rename($newName) {
53
		if ($this->path != $newName) {
54
			rename($this->path, $newName);
55
			return true;
56
		}
57
		return false;
58
	}
59
60
	/**
61
	 * Exclui o diretorio, e os arquivos dentro dele
62
	 * @param string $path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
	 * @return boolean
64
	 */
65
	public function remove() {
66
		$path = str_replace('//', '/', $this->path);
67
		if (is_dir($path)):
68
			$this->removeAllFiles();
69
			return rmdir($path);
70
		else:
71
			return false;
72
		endif;
73
	}
74
75
	/** Exclui os arquivos deste diretorio */
76
	protected function removeAllFiles() {
77
		$path = str_replace('//', '/', $this->path);
78
		$files = array_diff(scandir($path), ['.', '..']);
79
		foreach ($files as $file) {
80
			if (is_dir("$path/$file")) {
81
				$subDirectory = new Directory("$path/$file");
82
				$subDirectory->remove();
83
			} else {
84
				unlink("$path/$file");
85
			}
86
		}
87
	}
88
89
}
90