FileAnalysisTool   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B analyse() 0 32 6
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