Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

FileInfoMimeType::guess()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
/**
3
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\File\MimeType;
6
7
use Pivasic\Bundle\Common\File\Exception\FileNotFoundException;
8
use Pivasic\Bundle\Common\File\Exception\AccessDeniedException;
9
10
/**
11
 * Class FileInfoMimeType
12
 * @package Pivasic\Bundle\Common\File\MimeType
13
 */
14
class FileInfoMimeType
15
{
16
    /**
17
     * @return bool
18
     */
19
    public static function isSupported()
20
    {
21
        return function_exists('finfo_open');
22
    }
23
24
    /**
25
     * @param $path
26
     *
27
     * @return string|null
28
     *
29
     * @throws FileNotFoundException
30
     * @throws AccessDeniedException
31
     */
32
    public function guess($path)
33
    {
34
        if (!is_file($path)) {
35
            throw new FileNotFoundException($path);
36
        }
37
38
        if (!is_readable($path)) {
39
            throw new AccessDeniedException($path);
40
        }
41
42
        if (!self::isSupported()) {
43
            return null;
44
        }
45
46
        if (!$fileInfo = new \finfo(FILEINFO_MIME_TYPE)) {
47
            return null;
48
        }
49
50
        return $fileInfo->file($path);
51
    }
52
}