Completed
Pull Request — master (#1)
by Carlos C
01:18
created

FileMimeChecker   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 43
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMimeType() 0 7 4
A check() 0 4 1
A checkMime() 0 11 3
A cast() 0 4 1
A getMimeFileInfo() 0 7 2
1
<?php
2
namespace XmlSchemaValidator;
3
4
/**
5
 * This is an utility class to check a file mime against a set of mimes
6
 *
7
 * @access private
8
 * @package XmlSchemaValidator
9
 */
10
class FileMimeChecker extends SetStrings
11
{
12
    /** @var \finfo */
13
    private $getMimeFileInfo;
14
15 10
    protected function getMimeFileInfo()
16
    {
17 10
        if (null === $this->getMimeFileInfo) {
18 10
            $this->getMimeFileInfo = new \finfo(FILEINFO_SYMLINK);
19
        }
20 10
        return $this->getMimeFileInfo;
21
    }
22
23 11
    public function getMimeType($filename)
24
    {
25 11
        if (! (is_file($filename) || is_link($filename)) || ! is_readable($filename)) {
26 1
            return '';
27
        }
28 10
        return (string) $this->getMimeFileInfo()->file($filename, FILEINFO_MIME_TYPE);
29
    }
30
31 7
    public function check($filename)
32
    {
33 7
        return $this->checkMime($this->getMimeType($filename));
34
    }
35
36 10
    public function checkMime($mimetype)
37
    {
38 10
        $mimetype = $this->cast($mimetype);
39 10
        if (0 === $this->count()) {
40 8
            return true;
41
        }
42 4
        if ('' === $mimetype) {
43
            return false;
44
        }
45 4
        return array_key_exists($mimetype, $this->members);
46
    }
47
48 23
    public function cast($member)
49
    {
50 23
        return strtolower(trim(parent::cast($member)));
51
    }
52
}
53