Passed
Push — master ( d1b1aa...f4995a )
by Thierry
07:48
created

Reader::read()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Reader.php - Jaxon config reader
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2016 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
12
13
namespace Jaxon\Utils\Config;
14
15
class Reader
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Reader
Loading history...
16
{
17
    /**
18
     * Read options from a config file
19
     *
20
     * @param string        $sConfigFile        The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 8 found
Loading history...
21
     *
22
     * @return array
23
     */
24
    public function read($sConfigFile)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
25
    {
26
        if(!($sConfigFile = trim($sConfigFile)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
Coding Style introduced by
Consider using a different name than the parameter $sConfigFile. This often makes code more readable.
Loading history...
27
        {
28
            return [];
29
        }
30
31
        $sExt = pathinfo($sConfigFile, PATHINFO_EXTENSION);
32
        switch($sExt)
33
        {
34
        case 'php':
35
            $aConfigOptions = Php::read($sConfigFile);
36
            break;
37
        case 'yaml':
38
        case 'yml':
39
            $aConfigOptions = Yaml::read($sConfigFile);
40
            break;
41
        case 'json':
42
            $aConfigOptions = Json::read($sConfigFile);
43
            break;
44
        default:
45
            $sErrorMsg = jaxon_trans('errors.file.extension', ['path' => $sConfigFile]);
46
            throw new \Jaxon\Utils\Config\Exception\File($sErrorMsg);
47
        }
48
49
        return $aConfigOptions;
50
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
51
52
    /**
53
     * Read options from a config file and setup the library
54
     *
55
     * @param string        $sConfigFile        The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 8 found
Loading history...
56
     * @param string        $sConfigSection     The section of the config file to be loaded
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
57
     *
58
     * @return void
59
     */
60
    public function load($sConfigFile, $sConfigSection = '')
61
    {
62
        $aConfigOptions = $this->read($sConfigFile);
63
        // Setup the lib config options.
64
        jaxon()->di()->getConfig()->setOptions($aConfigOptions, $sConfigSection);
65
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
66
}
67