Json::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace TheIconic\Config\Parser;
4
5
use TheIconic\Config\Exception\ParserException;
6
7
/**
8
 * config file parser for ini files
9
 *
10
 * @package Shared\Helper\Config\Parser
11
 */
12
class Json extends AbstractParser
13
{
14
    /**
15
     * parses a json config file containing a config array
16
     *
17
     * @param string $file
18
     * @return array
19
     */
20
    public function parse($file)
21
    {
22
        $config = json_decode(file_get_contents($file), true);
23
24
        if (!is_array($config)) {
25
            $error = json_last_error();
26
27
            if ($error === JSON_ERROR_NONE) {
28
                $message = 'Invalid configuration format';
29
            } else {
30
                $message = json_last_error_msg();
31
            }
32
33
            throw new ParserException(sprintf('Couldn\'t parse config file %s: %s', $file, $message));
34
        }
35
36
        return $config;
37
    }
38
}
39