Passed
Push — master ( 170dc8...c9950a )
by mon
02:34
created

Extension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 61
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultType() 0 4 1
A getTypes() 0 12 3
1
<?php
2
3
namespace FileEye\MimeMap;
4
5
/**
6
 * Class for mapping file extensions to MIME types.
7
 */
8
class Extension
9
{
10
    /**
11
     * The file extension.
12
     *
13
     * @var string
14
     */
15
    protected $extension;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param string $extension A file extension.
21
     */
22 9
    public function __construct($extension)
23
    {
24 9
        $this->extension = strtolower($extension);
25 9
    }
26
27
    /**
28
     * Returns the file extension's preferred MIME type.
29
     *
30
     * @param bool $strict
31
     *   (Optional) if true a MappingException is thrown when no mapping is
32
     *   found, if false it returns 'application/octet-stream' as a default.
33
     *   Defaults to true.
34
     *
35
     * @throws MappingException if no mapping found and $strict is true.
36
     *
37
     * @return string
38
     */
39 5
    public function getDefaultType($strict = true)
40
    {
41 5
        return $this->getTypes($strict)[0];
42
    }
43
44
    /**
45
     * Returns all the MIME types related to the file extension.
46
     *
47
     * @param bool $strict
48
     *   (Optional) if true a MappingException is thrown when no mapping is
49
     *   found, if false it returns ['application/octet-stream'] as a default.
50
     *   Defaults to true.
51
     *
52
     * @throws MappingException if no mapping found and $strict is true.
53
     *
54
     * @return string[]
55
     */
56 9
    public function getTypes($strict = true)
57
    {
58 9
        $map = new MapHandler();
59 9
        if (!isset($map->get()['extensions'][$this->extension])) {
60 5
            if ($strict) {
61 2
                throw new MappingException('No MIME type mapped to extension ' . $this->extension);
62
            } else {
63 3
                return ['application/octet-stream'];
64
            }
65
        }
66 5
        return $map->get()['extensions'][$this->extension];
67
    }
68
}
69