Passed
Push — master ( c7e98d...e0d6a2 )
by Anton
04:56 queued 01:50
created

Uploader::save()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 46
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 14
nc 9
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Utils {
4
5
	use Utils\Messages, Explorer, Language, Request;
6
7
	abstract class Uploader {
8
9
		private static $base_name = null, $file_name = null;
10
11
		# Translate error code
12
13
		private static function translateError(int $error) {
14
15
			if ($error === UPLOAD_ERR_INI_SIZE)         return 'UPLOADER_ERROR_INI_SIZE';
16
17
			if ($error === UPLOAD_ERR_FORM_SIZE)        return 'UPLOADER_ERROR_FORM_SIZE';
18
19
			if ($error === UPLOAD_ERR_PARTIAL)          return 'UPLOADER_ERROR_PARTIAL';
20
21
			if ($error === UPLOAD_ERR_NO_FILE)          return 'UPLOADER_ERROR_NO_FILE';
22
23
			if ($error === UPLOAD_ERR_NO_TMP_DIR)       return 'UPLOADER_ERROR_NO_TMP_DIR';
24
25
			if ($error === UPLOAD_ERR_CANT_WRITE)       return 'UPLOADER_ERROR_CANT_WRITE';
26
27
			if ($error === UPLOAD_ERR_EXTENSION)        return 'UPLOADER_ERROR_EXTENSION';
28
29
			# ------------------------
30
31
			return 'UPLOADER_ERROR_UNKNOWN';
32
		}
33
34
		# Save uploaded file
35
36
		public static function save(string $name, string $dir_name) {
37
38
			if (false === ($file = Request::file($name))) return false;
39
40
			# Check for upload errors
41
42
			if ($file['error'] !== UPLOAD_ERR_OK) return self::translateError($file['error']);
43
44
			# Check for secure upload
45
46
			if (!is_uploaded_file($file['tmp_name'])) return 'UPLOADER_ERROR_SECURITY';
47
48
			# Check size
49
50
			if ($file['size'] > CONFIG_UPLOADS_MAX_SIZE) return 'UPLOADER_ERROR_SIZE';
51
52
			# Check file extension
53
54
			$extensions = ['php', 'phtml', 'php3', 'php4', 'php5', 'phps'];
55
56
			$extension = strtolower(Explorer::getExtension($file['name'], false));
57
58
			if (in_array($extension, $extensions, true)) return 'UPLOADER_ERROR_TYPE';
59
60
			# Check target directory
61
62
			if (!Explorer::isDir($dir_name) && !Explorer::createDir($dir_name)) return 'UPLOADER_ERROR_DIR';
63
64
			# Check target file
65
66
			$base_name = basename($file['name']); $file_name = ($dir_name . '/' . $base_name);
67
68
			if (Explorer::isDir($file_name) || Explorer::isFile($file_name)) return 'UPLOADER_ERROR_EXISTS';
69
70
			# Save uploaded file
71
72
			if (!@move_uploaded_file($file['tmp_name'], $file_name)) return 'UPLOADER_ERROR_SAVE';
73
74
			# Set upload data
75
76
			self::$base_name = $base_name; self::$file_name = $file_name;
77
78
			# ------------------------
79
80
			return true;
81
		}
82
83
		# Submit uploaded file
84
85
		public static function submit(string $name, string $dir_name) {
86
87
			$result = self::save($name, $dir_name);
88
89
			if (is_string($result)) { Messages::set('error', Language::get($result)); return false; }
90
91
			# ------------------------
92
93
			return $result;
94
		}
95
96
		# Get last upload base name
97
98
		public static function baseName() {
99
100
			return self::$base_name;
101
		}
102
103
		# Get last upload file name
104
105
		public static function fileName() {
106
107
			return self::$file_name;
108
		}
109
	}
110
}
111