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

FileMimeChecker::checkMime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.0261
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