Passed
Pull Request — master (#9)
by mon
02:42
created

MapUpdater::compareMaps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 15
cts 16
cp 0.9375
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3.0021

1 Method

Rating   Name   Duplication   Size   Complexity  
A MapUpdater::writeMapToPhpClassFile() 0 9 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
     * Default URL where to read the specification from.
14
     *
15
     * The apache httpd project contains the most complete list of file
16
     * extension to mime type mapping on the planet. We use it to update our
17
     * own list.
18
     */
19
    const DEFAULT_SOURCE_FILE = 'http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co';
20
21
    /**
22
     * Returns the default file with override commands to be executed.
23
     *
24
     * The YAML file provides an array of calls to MapHandler methods to be
25
     * executed sequentially. Each entry indicates the method to be invoked and
26
     * the arguments to be passed in.
27
     *
28
     * @return string
29
     */
30 1
    public static function getDefaultOverrideFile()
31
    {
32 1
        return __DIR__ . '/../resources/apache_overrides.yml';
33
    }
34
35
    /**
36
     * Creates a new type-to-extension map reading from a file.
37
     *
38
     * @param string $source_file
39
     *   (Optional) the source file. Defaults to the Apache source bind_textdomain_codeset
40
     *   repository file where MIME types and file extensions are associated.
41
     *
42
     * @throws \RuntimeException if file I/O error occurs.
43
     *
44
     * @return MapHandler
45
     *   The map handler with the new map.
46
     */
47 3
    public function createMapFromSourceFile($source_file = MapUpdater::DEFAULT_SOURCE_FILE)
48
    {
49 3
        $map = MapHandler::map('\FileEye\MimeMap\Map\EmptyMap');
50 3
        $lines = file($source_file);
51 3
        foreach ($lines as $line) {
52 2
            if ($line{0} == '#') {
53 2
                continue;
54
            }
55 2
            $line = preg_replace("#\\s+#", ' ', trim($line));
56 2
            $parts = explode(' ', $line);
57 2
            $type = array_shift($parts);
58 2
            foreach ($parts as $extension) {
59 2
                $map->addMapping($type, $extension);
0 ignored issues
show
Bug introduced by
The method addMapping cannot be called on $map (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
60
            }
61
        }
62 3
        if (empty($map->getMapArray())) {
0 ignored issues
show
Bug introduced by
The method getMapArray cannot be called on $map (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63 1
            throw new \RuntimeException('No data found in file ' . $source_file);
64
        }
65 2
        return $map;
66
    }
67
68
    /**
69
     * Applies to the map an array of overrides.
70
     *
71
     * @param AbstractMap $map
72
     *   The map.
73
     * @param array $overrides
74
     *   The overrides to be applied.
75
     *
76
     * @return void
77
     */
78 1
    public function applyOverrides(AbstractMap $map, array $overrides)
79
    {
80 1
        foreach ($overrides as $command) {
81 1
            call_user_func_array([$map, $command[0]], $command[1]);
82
        }
83 1
    }
84
85
    /**
86
     * Updates the map at a destination PHP file.
87
     *
88
     * @param AbstractMap $map
89
     *   The map.
90
     *
91
     * @return void
92
     */
93 1
    public function writeMapToPhpClassFile(AbstractMap $map, $file)
94
    {
95 1
        $content = preg_replace(
96 1
            '#protected static \$map = (.+?);#s',
97 1
            "protected static \$map = " . var_export($map->getMapArray(), true) . ";",
98 1
            file_get_contents($file)
99
        );
100 1
        file_put_contents($file, $content);
101 1
    }
102
}
103