Test Failed
Pull Request — master (#15)
by mon
01:56
created

MapUpdater   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 166
rs 10
c 0
b 0
f 0
ccs 62
cts 62
cp 1
wmc 23

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getDefaultMapBuildFile() 0 3 1
A writeMapToPhpClassFile() 0 9 1
A applyOverrides() 0 7 2
D loadMapFromFreedesktopFile() 0 59 14
A loadMapFromApacheFile() 0 17 4
1
<?php
2
3
namespace FileEye\MimeMap;
4
5
use FileEye\MimeMap\Map\AbstractMap;
6
7
/**
8
 * Compiles the MIME type to file extension map.
9
 */
10
class MapUpdater
11
{
12
    /**
13
     * The AbstractMap object to update.
14
     *
15
     * @var AbstractMap
16
     */
17
    protected $map;
18
19
    /**
20
     * Returns the default file with override commands to be executed.
21
     *
22
     * The YAML file provides an array of calls to MapHandler methods to be
23
     * executed sequentially. Each entry indicates the method to be invoked and
24
     * the arguments to be passed in.
25
     *
26
     * @return string
27
     */
28 1
    public static function getDefaultMapBuildFile()
29
    {
30 1
        return __DIR__ . '/../resources/default_map_build.yml';
31
    }
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param AbstractMap $map
37
     *   The map.
38
     */
39 7
    public function __construct(AbstractMap $map)
40
    {
41 7
        $this->map = $map;
42 7
    }
43
44
    /**
45
     * Loads a new type-to-extension map reading from a file in Apache format.
46
     *
47
     * @param string $source_file
48
     *   The source file. The file must conform to the format in the Apache
49
     *   source code repository file where MIME types and file extensions are
50
     *   associated.
51
     *
52
     * @return string[]
53
     *   An array of error messages.
54
     */
55 3
    public function loadMapFromApacheFile($source_file)
56
    {
57 3
        $errors = [];
58 3
        $lines = file($source_file);
59 3
        foreach ($lines as $line) {
60 2
            if ($line{0} == '#') {
61 2
                continue;
62
            }
63 2
            $line = preg_replace("#\\s+#", ' ', trim($line));
64 2
            $parts = explode(' ', $line);
65 2
            $type = array_shift($parts);
66 2
            foreach ($parts as $extension) {
67 2
                $this->map->addTypeExtensionMapping($type, $extension);
68
            }
69
        }
70 3
        $this->map->sort();
71 3
        return $errors;
72
    }
73
74
    /**
75
     * Loads a new type-to-extension map reading from a Freedesktop.org file.
76
     *
77
     * @param string $source_file
78
     *   The source file. The file must conform to the format in the
79
     *   Freedesktop.org database.
80
     *
81
     * @return string[]
82
     *   An array of error messages.
83
     */
84 2
    public function loadMapFromFreedesktopFile($source_file)
85
    {
86 2
        $errors = [];
87 2
        $xml = simplexml_load_string(file_get_contents($source_file));
88 2
        $aliases = [];
89
90 2
        foreach ($xml as $node) {
91 1
            $exts = [];
92 1
            foreach ($node->glob as $glob) {
93 1
                $pattern = (string) $glob['pattern'];
94 1
                if ('*' != $pattern[0] || '.' != $pattern[1]) {
95 1
                    continue;
96
                }
97 1
                $exts[] = substr($pattern, 2);
98
            }
99 1
            if (empty($exts)) {
100 1
                continue;
101
            }
102
103 1
            $type = (string) $node['type'];
104
105
            // Add description.
106 1
            if (isset($node->comment)) {
107 1
                $this->map->addTypeDescription($type, (string) $node->comment[0]);
108
            }
109 1
            if (isset($node->acronym)) {
110 1
                $acronym = (string) $node->acronym;
111 1
                if (isset($node->{'expanded-acronym'})) {
112 1
                    $acronym .= ': ' . (string) $node->{'expanded-acronym'};
113
                }
114 1
                $this->map->addTypeDescription($type, $acronym);
115
            }
116
117
            // Add extensions.
118 1
            foreach ($exts as $ext) {
119 1
                $this->map->addTypeExtensionMapping($type, $ext);
120
            }
121
122
            // All aliases are accumulated and processed at the end of the
123
            // cycle to allow proper consistency checking on the completely
124
            // developed list of types.
125 1
            foreach ($node->alias as $alias) {
126 1
                $aliases[$type][] = (string) $alias['type'];
127
            }
128
        }
129
130
        // Add all the aliases, provide logging of errors.
131 2
        foreach ($aliases as $type => $a) {
132 1
            foreach ($a as $alias) {
133
                try {
134 1
                    $this->map->addTypeAlias($type, $alias);
135 1
                } catch (MappingException $e) {
136 1
                    $errors[] = $e->getMessage();
137
                }
138
            }
139
        }
140
141 2
        $this->map->sort();
142 2
        return $errors;
143
    }
144
145
    /**
146
     * Applies to the map an array of overrides.
147
     *
148
     * @param array $overrides
149
     *   The overrides to be applied.
150
     *
151
     * @return $this
152
     */
153 2
    public function applyOverrides(array $overrides)
154
    {
155 2
        $errors = [];
156 2
        foreach ($overrides as $command) {
157 2
            call_user_func_array([$this->map, $command[0]], $command[1]);
158
        }
159 2
        return $errors;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $errors returns the type array which is incompatible with the documented return type FileEye\MimeMap\MapUpdater.
Loading history...
160
    }
161
162
    /**
163
     * Updates the map at a destination PHP file.
164
     *
165
     * @return $this
166
     */
167 1
    public function writeMapToPhpClassFile($file)
168
    {
169 1
        $content = preg_replace(
170 1
            '#protected static \$map = (.+?);#s',
171 1
            "protected static \$map = " . var_export($this->map->getMapArray(), true) . ";",
172 1
            file_get_contents($file)
173
        );
174 1
        file_put_contents($file, $content);
175 1
        return $this;
176
    }
177
}
178