FileAnalysisTool::analyse()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
cc 6
nc 8
nop 1
1
<?php
2
/**
3
 * @author Threema GmbH
4
 * @copyright Copyright (c) 2015-2016 Threema GmbH
5
 */
6
7
8
namespace Threema\MsgApi\Tools;
9
10
final class FileAnalysisTool {
11
	/**
12
	 * @param string $file
13
	 * @return FileAnalysisResult
14
	 */
15
	public static function analyse($file) {
16
		//check if file exists
17
		if(false === file_exists($file)) {
18
			return null;
19
		}
20
21
		//is not a file
22
		if(false === is_file($file)) {
23
			return null;
24
		}
25
26
		//get file size
27
		$size = filesize($file);
28
29
		$mimeType = null;
30
		//mime type getter
31
		if(function_exists('finfo_open')) {
32
			$finfo = finfo_open(FILEINFO_MIME_TYPE);
33
			$mimeType = finfo_file($finfo, $file);
34
		}
35
		else if(function_exists('mime_content_type')) {
36
			$mimeType = mime_content_type($file);
37
		}
38
39
		//default mime type
40
		if(strlen($mimeType) == 0) {
41
			//default mime type
42
			$mimeType = 'application/octet-stream';
43
		}
44
45
		return new FileAnalysisResult($mimeType, $size, $file);
46
	}
47
}
48