Completed
Push — master ( 6ea9b4...222850 )
by Andreas
04:32
created

ExtensionMimeTypeGuesser::buildMap()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 1
crap 4
1
<?php
2
/**
3
 * Simple extension-based MIME type guesser.
4
 *
5
 * @copyright 2018 Institute of Legal Medicine, Medical University of Innsbruck
6
 * @author Andreas Erhard <[email protected]>
7
 * @license LGPL-3.0-only
8
 * @link http://www.gerichtsmedizin.at/
9
 *
10
 * @package fileinfo
11
 */
12
namespace Gmi\Toolkit\Fileinfo\Type;
13
14
/**
15
 * Guesses the MIME type for a file based on the file extension.
16
 */
17
class ExtensionMimeTypeGuesser
18
{
19
    /**
20
     * @var array Map of extensions to MIME types.
21
     */
22
    private $map;
23
24
    /**
25
     * @var string Fallback MIME type if it can't be determined via the extension.
26
     */
27
    private $fallbackType = 'application/octet-stream';
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param TypeMapInterface[] $typeMaps
33
     */
34 6
    public function __construct(array $typeMaps = null)
35
    {
36 6
        if (null === $typeMaps) {
37 5
            $typeMaps = static::getDefaultTypeMaps();
38 5
        }
39
40 6
        $this->map = $this->buildMap($typeMaps);
41 6
    }
42
43
    /**
44
     * Returns the MIME type for a given file extension.
45
     * For unknown extensions, "application/octet-stream" is returned.
46
     *
47
     * @param string $extension
48
     *
49
     * @return string
50
     */
51 6
    public function guess($extension)
52
    {
53 6
        if (isset($this->map[$extension])) {
54 5
            return $this->map[$extension];
55
        }
56
57 1
        return $this->fallbackType;
58
    }
59
60
    /**
61
     * Returns the default type maps.
62
     *
63
     * @return TypeMapInterface[]
64
     */
65 5
    public static function getDefaultTypeMaps()
66
    {
67
        return [
68 5
            new DocumentTypeMap(),
69 5
            new ImageTypeMap(),
70 5
            new VideoTypeMap(),
71 5
            new AudioTypeMap(),
72 5
            new OpenDocumentTypeMap(),
73 5
            new MsOfficeTypeMap(),
74 5
            new SerializationTypeMap(),
75 5
            new ArchiveTypeMap(),
76 5
            new WebApplicationTypeMap(),
77 5
        ];
78
    }
79
80
    /**
81
     * Builds a lookup array from the type maps.
82
     *
83
     * @param TypeMapInterface[] $typeMaps
84
     *
85
     * @return array flat map array with the file extension as key and the MIME type as value
86
     */
87 6
    private function buildMap($typeMaps)
88
    {
89 6
        $map = [];
90
91 6
        $allTypeMaps = [];
92
93 6
        foreach ($typeMaps as $typeMap) {
94 6
            $allTypeMaps = array_merge($allTypeMaps, $typeMap->getMap());
95 6
        }
96
97 6
        foreach ($allTypeMaps as $type) {
98 6
            foreach ($type->getExtensions() as $extension) {
99 6
                $map[$extension] = $type->getMimeType();
100 6
            }
101 6
        }
102
103 6
        return $map;
104
    }
105
}
106