Test Failed
Push — main ( bc775a...919c7e )
by Stefan
07:40
created

JSONConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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