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

File   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtension() 0 3 1
A setPath() 0 6 2
A exists() 0 3 1
A getSize() 0 8 2
A __construct() 0 3 1
A getType() 0 3 1
A getContent() 0 3 1
1
<?php
2
3
namespace Win\Repositories\Filesystem;
4
5
use Exception;
6
use Win\Repositories\Filesystem;
7
8
/**
9
 * Arquivo
10
 */
11
class File extends Storable
12
{
13
	/**
14
	 * Instância um novo arquivo
15
	 * @param string $path Caminho relativo
16
	 */
17
	public function __construct($path)
18
	{
19
		$this->setPath($path);
20
	}
21
22
	/** @return string */
23
	public function getExtension()
24
	{
25
		return pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
26
	}
27
28
	/** @return string */
29
	public function getType()
30
	{
31
		return mime_content_type($this->getAbsolutePath());
32
	}
33
34
	/** @return int|false */
35
	public function getSize()
36
	{
37
		$size = false;
38
		if ($this->exists()) {
39
			$size = filesize($this->getAbsolutePath());
40
		}
41
42
		return $size;
43
	}
44
45
	/** @return bool */
46
	public function exists()
47
	{
48
		return is_file($this->getAbsolutePath());
49
	}
50
51
	/**
52
	 * @param string $path Caminho relativo
53
	 * @throws Exception
54
	 */
55
	protected function setPath($path)
56
	{
57
		if (!preg_match(static::REGEXP_PATH, $path)) {
58
			throw new Exception($path . ' is a invalid file path.');
59
		}
60
		parent::setPath($path);
61
	}
62
63
	/**
64
	 * Retorna o conteúdo do arquivo
65
	 * @return string|false
66
	 */
67
	public function getContent()
68
	{
69
		return (new Filesystem())->read($this->getPath());
70
	}
71
}
72