Test Failed
Pull Request — master (#13)
by mon
02:18
created

MapUpdater   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 129
rs 10
c 0
b 0
f 0
ccs 28
cts 28
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultMapBuildFile() 0 4 1
A __construct() 0 4 1
A loadMapFromApacheFile() 0 16 4
B loadMapFromFreedesktopFile() 0 22 7
A applyOverrides() 0 7 2
A writeMapToPhpClassFile() 0 10 1
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
    public static function getDefaultMapBuildFile()
29
    {
30 1
        return __DIR__ . '/../resources/default_map_build.yml';
31
    }
32 1
33
    /**
34
     * Constructor.
35
     *
36
     * @param AbstractMap $map
37
     *   The map.
38
     */
39
    public function __construct(AbstractMap $map)
40
    {
41
        $this->map = $map;
42
    }
43
44
    /**
45
     * Loads a new type-to-extension map reading from a file in Apache format.
46
     *
47 3
     * @param string $source_file
48
     *   The source file. The file must conform to the format in the Apache
49 3
     *   source code repository file where MIME types and file extensions are
50 3
     *   associated.
51 3
     *
52 2
     * @throws \RuntimeException if file I/O error occurs.
53 2
     *
54
     * @return $this
55 2
     */
56 2
    public function loadMapFromApacheFile($source_file)
57 2
    {
58 2
        $lines = file($source_file);
59 2
        foreach ($lines as $line) {
60
            if ($line{0} == '#') {
61
                continue;
62 3
            }
63 3
            $line = preg_replace("#\\s+#", ' ', trim($line));
64 1
            $parts = explode(' ', $line);
65
            $type = array_shift($parts);
66 2
            foreach ($parts as $extension) {
67
                $this->map->addMapping($type, $extension);
68
            }
69
        }
70
        return $this;
71
    }
72
73
    /**
74
     * Loads a new type-to-extension map reading from a Freedesktop.org file.
75
     *
76
     * @param string $source_file
77
     *   The source file. The file must conform to the format in the
78
     *   Freedesktop.org database.
79 1
     *
80
     * @throws \RuntimeException if file I/O error occurs.
81 1
     *
82 1
     * @return $this
83
     */
84 1
    public function loadMapFromFreedesktopFile($source_file)
85
    {
86
        $xml = simplexml_load_string(file_get_contents($source_file));
87
        foreach ($xml as $node) {
88
            $exts = [];
89
            foreach ($node->glob as $glob) {
90
                $pattern = (string) $glob['pattern'];
91
                if ('*' != $pattern[0] || '.' != $pattern[1]) {
92
                    continue;
93
                }
94 1
                $exts[] = substr($pattern, 2);
95
            }
96 1
            if (empty($exts)) {
97 1
                continue;
98 1
            }
99 1
            $mt = (string) $node['type'];
100
            foreach ($exts as $ext) {
101 1
                $this->map->addMapping($mt, $ext);
102 1
            }
103
        }
104
        return $this;
105
    }
106
107
    /**
108
     * Applies to the map an array of overrides.
109
     *
110
     * @param array $overrides
111
     *   The overrides to be applied.
112
     *
113
     * @return $this
114
     */
115
    public function applyOverrides(array $overrides)
116
    {
117
        foreach ($overrides as $command) {
118
            call_user_func_array([$this->map, $command[0]], $command[1]);
119
        }
120
        return $this;
121
    }
122
123
    /**
124
     * Updates the map at a destination PHP file.
125
     *
126
     * @return $this
127
     */
128
    public function writeMapToPhpClassFile($file)
129
    {
130
        $content = preg_replace(
131
            '#protected static \$map = (.+?);#s',
132
            "protected static \$map = " . var_export($this->map->getMapArray(), true) . ";",
133
            file_get_contents($file)
134
        );
135
        file_put_contents($file, $content);
136
        return $this;
137
    }
138
}
139