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

MapHandler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace FileEye\MimeMap;
4
5
/**
6
 * Class for mapping file extensions to MIME types.
7
 */
8
class MapHandler
9
{
10
    protected $map;
11
12 12
    public function __construct(array $map = null)
13
    {
14 12
        if (is_null($map)) {
15 12
            $this->map = &TypeExtensionMap::$map;
16
        } else {
17
            $this->map = $map;
18
        }
19 12
    }
20
21 12
    public function get()
22
    {
23 12
        return $this->map;
24
    }
25
26
    public function sort()
27
    {
28
        ksort($this->map['types']);
29
        ksort($this->map['extensions']);
30
    }
31
32 1
    public function addMapping($type, $extension)
33
    {
34 1
        $type = strtolower($type);
35 1
        $extension = strtolower($extension);
36
37
        // Add entry to 'types'.
38 1 View Code Duplication
        if (!isset($this->map['types'][$type])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39 1
            $this->map['types'][$type] = [$extension];
40
        } else {
41 1
            if (array_search($extension, $this->map['types'][$type]) === false) {
42
                $this->map['types'][$type][] = $extension;
43
            }
44
        }
45
46
        // Add entry to 'extensions'.
47 1 View Code Duplication
        if (!isset($this->map['extensions'][$extension])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48 1
            $this->map['extensions'][$extension] = [$type];
49
        } else {
50 1
            if (array_search($type, $this->map['extensions'][$extension]) === false) {
51
                $this->map['extensions'][$extension][] = $type;
52
            }
53
        }
54
55 1
        return $this;
56
    }
57
58 1
    public function removeMapping($type, $extension)
59
    {
60 1
        $type = strtolower($type);
61 1
        $extension = strtolower($extension);
62
63 1
        $ret = false;
64
65
        // Remove entry from 'types'.
66 1 View Code Duplication
        if (isset($this->map['types'][$type])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67 1
            $key = array_search($extension, $this->map['types'][$type]);
68 1
            if ($key !== false) {
69 1
                $ret = true;
70 1
                unset($this->map['types'][$type][$key]);
71 1
                $tmp = [];
72 1
                foreach ($this->map['types'][$type] as $v) {
73 1
                    $tmp[] = $v;
74
                }
75 1
                $this->map['types'][$type] = $tmp;
76 1
                if (empty($this->map['types'][$type])) {
77 1
                    unset($this->map['types'][$type]);
78
                }
79
            }
80
        }
81
82
        // Remove entry from 'extensions'.
83 1 View Code Duplication
        if (isset($this->map['extensions'][$extension])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84 1
            $key = array_search($type, $this->map['extensions'][$extension]);
85 1
            if ($key !== false) {
86 1
                $ret = true;
87 1
                unset($this->map['extensions'][$extension][$key]);
88 1
                $tmp = [];
89 1
                foreach ($this->map['extensions'][$extension] as $v) {
90
                    $tmp[] = $v;
91
                }
92 1
                $this->map['extensions'][$extension] = $tmp;
93 1
                if (empty($this->map['extensions'][$extension])) {
94 1
                    unset($this->map['extensions'][$extension]);
95
                }
96
            }
97
        }
98
99 1
        return $ret;
100
    }
101
102 1
    public function removeType($type)
103
    {
104 1
        $type = strtolower($type);
105
106 1
        if (!isset($this->map['types'][$type])) {
107
            return false;
108
        }
109 1
        foreach ($this->map['types'][$type] as $extension) {
110 1
            $this->removeMapping($type, $extension);
111
        }
112 1
        return true;
113
    }
114
115 1 View Code Duplication
    public function setTypeDefaultExtension($type, $extension)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117 1
        $type = strtolower($type);
118 1
        $extension = strtolower($extension);
119
120 1
        if (!isset($this->map['types'][$type])) {
121
            throw new MappingException('Cannot set ' . $extension . ' as default extension for type ' . $type . ', type not defined');
122
        }
123 1
        $key = array_search($extension, $this->map['types'][$type]);
124 1
        if ($key === false) {
125
            throw new MappingException('Cannot set ' . $extension . ' as default extension for type ' . $type . ', extension not associated to this type');
126
        }
127 1
        $tmp = [$extension];
128 1
        foreach ($this->map['types'][$type] as $k => $v) {
129 1
            if ($k === $key) {
130 1
                continue;
131
            }
132 1
            $tmp[] = $v;
133
        }
134 1
        $this->map['types'][$type] = $tmp;
135 1
    }
136
137 1 View Code Duplication
    public function setExtensionDefaultType($extension, $type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139 1
        $type = strtolower($type);
140 1
        $extension = strtolower($extension);
141
142 1
        if (!isset($this->map['extensions'][$extension])) {
143
            throw new MappingException('Cannot set ' . $type . ' as default type for extension ' . $extension . ', extension not defined');
144
        }
145 1
        $key = array_search($type, $this->map['extensions'][$extension]);
146 1
        if ($key === false) {
147
            throw new MappingException('Cannot set ' . $type . ' as default type for extension ' . $extension . ', type not associated to this extension');
148
        }
149 1
        $tmp = [$type];
150 1
        foreach ($this->map['extensions'][$extension] as $k => $v) {
151 1
            if ($k === $key) {
152 1
                continue;
153
            }
154 1
            $tmp[] = $v;
155
        }
156 1
        $this->map['extensions'][$extension] = $tmp;
157 1
    }
158
}
159