Passed
Branch tests1.5 (af713c)
by Wanderson
02:17
created

TempFile   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setExtension() 0 2 1
A getBaseName() 0 2 1
A move() 0 6 1
A isTemporary() 0 2 1
A getAbsolutePath() 0 5 2
A getExtension() 0 2 1
A getName() 0 2 1
A create() 0 3 1
A setName() 0 2 2
A randomName() 0 2 1
A fromFiles() 0 9 3
A __construct() 0 3 1
1
<?php
2
3
namespace Win\File\Upload;
4
5
use Win\File\Directory;
6
use Win\File\File;
7
8
/**
9
 * Arquivos Temporários
10
 *
11
 */
12
class TempFile extends File implements UploadbleInterface {
13
14
	const REGEXP_PATH = '@^(([a-zA-Z0-9._\-\/]))+$@';
15
	const REGEXP_NAME = '@^(([a-zA-Z0-9._\-]?))+$@';
16
17
	protected $name;
18
	protected $baseName;
19
	protected $extension;
20
	protected $newName;
21
22
	/**
23
	 * Instância o arquivo temporário
24
	 * @param string $path
25
	 * @param string $name
26
	 */
27
	public function __construct($path) {
28
		$this->name = pathinfo($path, PATHINFO_FILENAME);
29
		parent::__construct($path);
30
	}
31
32
	/** @return string */
33
	public function getName() {
34
		return $this->name;
35
	}
36
37
	/** @return string */
38
	public function getBaseName() {
39
		return $this->getName() . $this->getExtensionDot();
40
	}
41
42
	/** @return string */
43
	public function getExtension() {
44
		return $this->extension;
45
	}
46
47
	/** @param string $name */
48
	public function setName($name) {
49
		$this->name = ($name) ? $name : $this->randomName();
50
	}
51
52
	/** @param string $extension */
53
	public function setExtension($extension) {
54
		$this->extension = $extension;
55
	}
56
57
	/** @return string */
58
	public function getAbsolutePath() {
59
		if ($this->isTemporary()) {
60
			return $this->getPath();
61
		} else {
62
			return parent::getAbsolutePath();
63
		}
64
	}
65
66
	/** @return boolean */
67
	public function isTemporary() {
68
		return (strpos($this->getPath(), sys_get_temp_dir()) === 0);
69
	}
70
71
	/** Retorna um nome aleatório */
72
	protected function randomName() {
73
		return md5($this->newName . '_' . time());
74
	}
75
76
	/**
77
	 * @param Directory $destination
78
	 * @param string $name
79
	 * @return boolean
80
	 */
81
	public function move(Directory $destination, $name = '') {
82
		$this->setName($name);
83
		$oldPath = $this->getAbsolutePath();
84
		$newPath = $destination->getPath() . DIRECTORY_SEPARATOR . $this->getBaseName();
85
		$this->setPath($newPath);
86
		return move_uploaded_file($oldPath, $this->getAbsolutePath());
87
	}
88
89
	/**
90
	 * Cria uma instância a partir da variável $_FILES
91
	 * @param type $name
0 ignored issues
show
Bug introduced by
The type Win\File\Upload\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
	 * @return static|null
93
	 */
94
	public static function fromFiles($name) {
95
		if (key_exists($name, $_FILES) && key_exists('tmp_name', $_FILES[$name])) {
96
			$tmp = new static($_FILES[$name]['tmp_name']);
97
			$tmp->newName = $_FILES[$name]['name'];
98
			$tmp->setExtension(pathinfo($_FILES[$name]['name'], PATHINFO_EXTENSION));
99
		} else {
100
			$tmp = new TempFile('error');
101
		}
102
		return $tmp;
103
	}
104
105
	/**
106
	 * Cria um arquivo no diretório temporário
107
	 * @param string $prefixName prefixName
108
	 * @return static
109
	 */
110
	public static function create($prefixName = '') {
111
		$tmpfname = tempnam(null, $prefixName);
112
		return new static($tmpfname);
113
	}
114
115
}
116