JSONConfig::parseFile()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
/**
7
 * Class for config component getting data from JSON file.
8
 *
9
 * @package Config
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class JSONConfig extends AbstractConfig
14
{
15
    /**
16
     * The constructor expects an valid filename/path to the JSON file.
17
     * @param string $strConfigFile
18
     */
19
    public function __construct(string $strConfigFile)
20
    {
21
        $this->aConfig = $this->parseFile($strConfigFile);
22
    }
23
24
    /**
25
     * Parse the given file an add all settings to the internal configuration.
26
     * @param string $strConfigFile
27
     * @return array<mixed>
28
     */
29
    protected function parseFile(string $strConfigFile) : array
30
    {
31
        if (!file_exists($strConfigFile)) {
32
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
33
        }
34
35
        $aJSON = null;
36
        $strJson = file_get_contents($strConfigFile);
37
        if ($strJson !== false) {
38
            $aJSON = json_decode($strJson, true);
39
            if ($aJSON === null) {
40
                trigger_error('Invalid config file (' . $strConfigFile . '): ' . json_last_error_msg(), E_USER_ERROR);
41
            }
42
        }
43
        return $aJSON ?? [];
44
    }
45
}
46