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

MapUpdater::createMapFromSourceFile()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.2728
c 0
b 0
f 0
cc 5
nc 8
nop 1
crap 5
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
     * @throws \RuntimeException if file I/O error occurs.
53
     *
54
     * @return $this
55
     */
56 3
    public function loadMapFromApacheFile($source_file)
57
    {
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->addMapping($type, $extension);
68
            }
69
        }
70 3
        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
     *
80
     * @throws \RuntimeException if file I/O error occurs.
81
     *
82
     * @return $this
83
     */
84 2
    public function loadMapFromFreedesktopFile($source_file)
85
    {
86 2
        $xml = simplexml_load_string(file_get_contents($source_file));
87 2
        foreach ($xml as $node) {
88 1
            $exts = [];
89 1
            foreach ($node->glob as $glob) {
90 1
                $pattern = (string) $glob['pattern'];
91 1
                if ('*' != $pattern[0] || '.' != $pattern[1]) {
92 1
                    continue;
93
                }
94 1
                $exts[] = substr($pattern, 2);
95
            }
96 1
            if (empty($exts)) {
97 1
                continue;
98
            }
99 1
            $mt = (string) $node['type'];
100 1
            foreach ($exts as $ext) {
101 1
                $this->map->addMapping($mt, $ext);
102
            }
103
        }
104 2
        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 1
    public function applyOverrides(array $overrides)
116
    {
117 1
        foreach ($overrides as $command) {
118 1
            call_user_func_array([$this->map, $command[0]], $command[1]);
119
        }
120 1
        return $this;
121
    }
122
123
    /**
124
     * Updates the map at a destination PHP file.
125
     *
126
     * @return $this
127
     */
128 1
    public function writeMapToPhpClassFile($file)
129
    {
130 1
        $content = preg_replace(
131 1
            '#protected static \$map = (.+?);#s',
132 1
            "protected static \$map = " . var_export($this->map->getMapArray(), true) . ";",
133 1
            file_get_contents($file)
134
        );
135 1
        file_put_contents($file, $content);
136 1
        return $this;
137
    }
138
}
139