Extension::getDefaultType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
ccs 2
cts 2
cp 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace FileEye\MimeMap;
4
5
use FileEye\MimeMap\Map\MimeMapInterface;
6
7
/**
8
 * Class for mapping file extensions to MIME types.
9
 */
10
class Extension implements ExtensionInterface
11
{
12
    /**
13
     * The file extension.
14
     *
15
     * @var string
16
     */
17
    protected $extension;
18
19
    /**
20
     * The MIME types map.
21
     *
22
     * @var MimeMapInterface
23
     */
24
    protected $map;
25
26 16
    public function __construct(string $extension, string $map_class = null)
27
    {
28 16
        $this->extension = strtolower($extension);
29 16
        $this->map = MapHandler::map($map_class);
30
    }
31
32 8
    public function getDefaultType(): string
33
    {
34 8
        return $this->getTypes()[0];
35
    }
36
37 16
    public function getTypes(): array
38
    {
39 16
        $types = $this->map->getExtensionTypes($this->extension);
40 16
        if (!empty($types)) {
41 8
            return $types;
42
        }
43 8
        throw new MappingException('No MIME type mapped to extension ' . $this->extension);
44
    }
45
}
46