Passed
Push — main ( 5396d8...bc775a )
by Stefan
01:33
created

JSONConfig::parseFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
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/GCalendar
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
     * Parse the given file an add all settings to the internal configuration.
21
     * @param string $strConfigFile
22
     */
23
    protected function parseFile(string $strConfigFile) : array
24
    {
25
        if (!file_exists($strConfigFile)) {
26
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
27
            return [];
28
        }
29
        
30
        $strJson = file_get_contents($strConfigFile);
31
        $aJSON = json_decode($strJson, true);
32
        if ($aJSON === null) {
33
            trigger_error('Invalid config file (' . $strConfigFile . '): ' . json_last_error_msg(), E_USER_ERROR);
34
        }
35
        return $aJSON;
36
    }
37
}
38