Completed
Push — master ( e47338...72b7b1 )
by Alexander
05:50 queued 03:00
created

UploadFile::save()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.2
cc 4
eloc 7
nc 8
nop 0
crap 4.0466
1
<?php
2
/**
3
 * Class for organize uploads storage
4
 *
5
 * @file      UploadFile.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2017 Alexander Yancharuk
11
 * @date      2013-07-27 22:06
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Tools;
17
18
/**
19
 * Class UploadFile
20
 *
21
 * @author  Yancharuk Alexander <alex at itvault dot info>
22
 */
23
class UploadFile extends File
24
{
25
	protected $tmp_path;
26
	protected $hash;
27
	protected $sub_dir;
28
	protected $orig_name;
29
	protected $www_path;
30
	protected $dir_mask = 0755;
31
	protected $hash_algorithm = 'sha1';
32
33
	/**
34
	 * Method created for test purpose
35
	 *
36
	 * @codeCoverageIgnore
37
	 *
38
	 * @param string $filename    File name
39
	 * @param string $destination Save destination
40
	 *
41
	 * @return bool
42
	 */
43
	protected function moveUploadedFile($filename, $destination)
44
	{
45
		return move_uploaded_file($filename, $destination);
46
	}
47
48
	/**
49
	 * Set upload directory mask
50
	 *
51
	 * @param int $dir_mask Value must be octal. Examples: 0755, 02755
52
	 *
53
	 * @return UploadFile
54
	 */
55 2
	public function setDirMask($dir_mask)
56
	{
57 2
		$this->dir_mask = $dir_mask;
58
59 2
		return $this;
60
	}
61
62
	/**
63
	 * Det upload directory mask
64
	 *
65
	 * @return int
66
	 */
67 2
	public function getDirMask()
68
	{
69 2
		return $this->dir_mask;
70
	}
71
72
	/**
73
	 * Get uploaded file origin name
74
	 *
75
	 * @return string
76
	 */
77 2
	public function getOrigName()
78
	{
79 2
		return $this->orig_name;
80
	}
81
82
	/**
83
	 * Set uploaded file origin name
84
	 *
85
	 * @param string $orig_name Origin file name
86
	 *
87
	 * @return UploadFile
88
	 */
89 2
	public function setOrigName($orig_name)
90
	{
91 2
		$this->orig_name = $orig_name;
92
93 2
		return $this;
94
	}
95
96
	/**
97
	 * Generate path for uploaded file
98
	 */
99 2
	public function initStorageName()
0 ignored issues
show
Coding Style introduced by
initStorageName uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
	{
101
		// initialize storage name only once
102 2
		if (null !== $this->getHash()) {
103 2
			return;
104
		}
105
106 2
		$array     = explode('.', $this->getOrigName());
107 2
		$extension = strtolower(end($array));
108
109 2
		$this->setHash(
110 2
			hash_file($this->getHashAlgorithm(), $this->getTmpPath())
111
		)
112 2
			->setSubDir(substr($this->getHash(), 0, 2))
113 2
			->setName(substr($this->getHash(), 2) . '.' . $extension)
114 2
			->setWwwPath(
115 2
				str_replace($_SERVER['DOCUMENT_ROOT'], '', $this->getDir())
116 2
				. DIRECTORY_SEPARATOR
117 2
				. $this->getSubDir()
118 2
				. DIRECTORY_SEPARATOR
119 2
				. $this->getName()
120
			)
121 2
			->setPath(
122 2
				$this->getDir()
123 2
				. DIRECTORY_SEPARATOR
124 2
				. $this->getSubDir()
125 2
				. DIRECTORY_SEPARATOR
126 2
				. $this->getName()
127
			);
128 2
	}
129
130
	/**
131
	 * Get uploaded file name hash
132
	 *
133
	 * @return string
134
	 */
135 2
	public function getHash()
136
	{
137 2
		return $this->hash;
138
	}
139
140
	/**
141
	 * Set uploaded file name hash
142
	 *
143
	 * @param string $hash
144
	 *
145
	 * @return UploadFile
146
	 */
147 2
	public function setHash($hash)
148
	{
149 2
		$this->hash = $hash;
150
151 2
		return $this;
152
	}
153
154
	/**
155
	 * Get uploaded file sub-dir
156
	 *
157
	 * @return string
158
	 */
159 2
	public function getSubDir()
160
	{
161 2
		return $this->sub_dir;
162
	}
163
164
	/**
165
	 * Set uploaded file sub-dir
166
	 *
167
	 * @param string $sub_dir
168
	 *
169
	 * @return UploadFile
170
	 */
171 2
	public function setSubDir($sub_dir)
172
	{
173 2
		$this->sub_dir = $sub_dir;
174
175 2
		return $this;
176
	}
177
178
	/**
179
	 * Save uploaded file
180
	 *
181
	 * @return bool
182
	 */
183 8
	public function save()
184
	{
185 8
		$dir = $this->getDir();
186
187 8
		is_dir($dir) || mkdir($dir, $this->getDirMask(), true);
188 8
		is_writable($dir) || chmod($dir, $this->getDirMask());
189
190 8
		return file_exists($this->getPath())
191
			? true
192 8
			: $this->moveUploadedFile($this->getTmpPath(), $this->getPath());
193
	}
194
195
	/**
196
	 * Get uploaded file temporary path
197
	 *
198
	 * @return string
199
	 */
200 2
	public function getTmpPath()
201
	{
202 2
		return $this->tmp_path;
203
	}
204
205
	/**
206
	 * Set uploaded file temporary path
207
	 *
208
	 * @param string $tmp_path Uploaded file temp path
209
	 *
210
	 * @return UploadFile
211
	 */
212 2
	public function setTmpPath($tmp_path)
213
	{
214 2
		$this->tmp_path = $tmp_path;
215
216 2
		return $this;
217
	}
218
219
	/**
220
	 * Get uploaded file www-path
221
	 *
222
	 * @return string
223
	 */
224 2
	public function getWwwPath()
225
	{
226 2
		return $this->www_path;
227
	}
228
229
	/**
230
	 * Set uploaded file www-path
231
	 *
232
	 * @param string $www_path www-path to file
233
	 *
234
	 * @return UploadFile
235
	 */
236 2
	public function setWwwPath($www_path)
237
	{
238 2
		$this->www_path = $www_path;
239
240 2
		return $this;
241
	}
242
243
	/**
244
	 * Get uploaded file name hashing algorithm
245
	 *
246
	 * @see hash_algos()
247
	 * @return string
248
	 */
249 2
	public function getHashAlgorithm()
250
	{
251 2
		return $this->hash_algorithm;
252
	}
253
254
	/**
255
	 * Set uploaded file name hashing algorithm
256
	 *
257
	 * @param string $hash_algorithm Hash algorithm
258
	 *
259
	 * @see hash_algos()
260
	 * @return UploadFile
261
	 */
262 2
	public function setHashAlgorithm($hash_algorithm)
263
	{
264 2
		$this->hash_algorithm = $hash_algorithm;
265
266 2
		return $this;
267
	}
268
}
269