NEONConfig::parseFile()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
cc 5
eloc 12
c 4
b 3
f 1
nc 12
nop 1
dl 0
loc 19
rs 9.5555
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
use Nette\Neon\Neon;
7
8
/**
9
 * Class for config component getting data from NEON file.
10
 *
11
 * This class uses the Nette/Neon module
12
 *
13
 * @package Config
14
 * @author Stefanius <[email protected]>
15
 * @copyright MIT License - see the LICENSE file for details
16
 */
17
class NEONConfig extends AbstractConfig
18
{
19
    /**
20
     * The constructor expects an valid filename/path to the NEON 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
     * @return array<mixed>
32
     */
33
    protected function parseFile(string $strConfigFile) : array
34
    {
35
        if (!file_exists($strConfigFile)) {
36
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
37
        }
38
39
        $aNeon = null;
40
        $strNeon = file_get_contents($strConfigFile);
41
        if ($strNeon !== false) {
42
            try {
43
                $aNeon = Neon::decode($strNeon);
44
                if (!is_array($aNeon)) {
45
                    trigger_error('Config file (' . $strConfigFile . ') does not contain config informations!', E_USER_ERROR);
46
                }
47
            } catch (\Exception $e) {
48
                trigger_error('Invalid config file (' . $strConfigFile . '): ' . $e->getMessage(), E_USER_ERROR);
49
            }
50
        }
51
        return $aNeon ?? [];
52
    }
53
}
54