Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

Uploader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUploaded() 0 2 1
A __construct() 0 3 1
A upload() 0 9 3
A prepare() 0 8 2
1
<?php
2
3
namespace Win\File\Upload;
4
5
use Win\File\Directory;
6
use Win\File\File;
7
8
/**
9
 * Auxilia fazer upload de Arquivos
10
 */
11
class Uploader {
12
13
	/** @var string[] */
14
	protected static $validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'csv', 'doc', 'docx', 'odt', 'pdf', 'txt', 'md', 'mp3', 'wav', 'mpeg'];
15
16
	/** @var Directory */
17
	protected $destination;
18
19
	/** @var TempFile */
20
	protected $temp;
21
22
	/** @var File */
23
	protected $uploaded;
24
25
	/**
26
	 * Inicializa o upload para o diretorio de destino
27
	 * @param Directory $destination
28
	 */
29
	public function __construct(Directory $destination) {
30
		$this->destination = $destination;
31
		$this->destination->create(0777);
32
	}
33
34
	/**
35
	 * Retorna a instância do arquivo que foi enviado
36
	 * @return File
37
	 */
38
	public function getUploaded() {
39
		return $this->uploaded;
40
	}
41
42
	/**
43
	 * Prepara o upload
44
	 * @param TempFile $temp
45
	 * @return boolean
46
	 */
47
	public function prepare(TempFile $temp) {
48
		$success = false;
49
		$this->temp = null;
50
		if ($temp->exists()) {
51
			$success = true;
52
			$this->temp = $temp;
53
		}
54
		return $success;
55
	}
56
57
	/**
58
	 * Faz o upload para o diretório final
59
	 * @param string $name
60
	 * @return boolean
61
	 */
62
	public function upload($name = '') {
63
		$success = false;
64
		if (!is_null($this->temp)) {
65
			$success = $this->temp->move($this->destination, $name);
66
		}
67
		if ($success) {
68
			$this->uploaded = new File($this->temp->getPath());
69
		}
70
		return $success;
71
	}
72
73
}
74