Passed
Push — main ( cf7664...39577d )
by Stefan
06:19
created

NEONConfig::__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 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
     */
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
        $strNeon = file_get_contents($strConfigFile);
39
        $aNeon = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $aNeon is dead and can be removed.
Loading history...
40
        try {
41
            $aNeon = Neon::decode($strNeon);
42
            if (!is_array($aNeon)) {
43
                trigger_error('Config file (' . $strConfigFile . ') do not contain config informations!', E_USER_ERROR);
44
            }
45
        } catch (\Exception $e) {
46
            trigger_error('Invalid config file (' . $strConfigFile . '): ' . $e->getMessage(), E_USER_ERROR);
47
        }
48
49
        return $aNeon;
50
    }
51
}
52