Passed
Push — master ( 25cbd8...3d7e23 )
by Mauro
02:02
created

Parser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SimpleDIC\Parser;
4
5
class Parser
6
{
7
    /**
8
     * @var array
9
     */
10
    private static $allowedExtensions = ['json', 'ini', 'xml', 'yaml', 'yml'];
11
12
    /**
13
     * @param string $filename
14
     *
15
     * @return array
16
     * @throws \Exception
17
     */
18
    public static function parse($filename)
19
    {
20
        $ext = self::getExt($filename);
21
22
        if (false === in_array($ext, self::$allowedExtensions)) {
23
            throw new \InvalidArgumentException($ext . ' is not a valid configuration file.');
24
        }
25
26
        $parser = self::getParser($ext);
27
28
        return $parser->parse($filename);
29
    }
30
31
    /**
32
     * @param string $filename
33
     *
34
     * @return mixed
35
     */
36
    private static function getExt($filename)
37
    {
38
        return pathinfo($filename, PATHINFO_EXTENSION);
39
    }
40
41
    /**
42
     * @param string $ext
43
     *
44
     * @return ParserInterface
45
     */
46
    private static function getParser($ext)
47
    {
48
        switch ($ext) {
49
            case 'ini':
50
                return new IniParser();
51
52
            case 'json':
53
                return new JsonParser();
54
55
            case 'xml':
56
                return new XmlParser();
57
58
            case 'yaml':
59
            case 'yml':
60
                return new YamlParser();
61
        }
62
    }
63
}
64