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

YAMLConfig::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
/**
7
 * Class for config component getting data from YAML file.
8
 *
9
 * This class needs the php module yaml to be installed!
10
 *
11
 * @package Config
12
 * @author Stefanius <[email protected]>
13
 * @copyright MIT License - see the LICENSE file for details
14
 */
15
class YAMLConfig extends AbstractConfig
16
{
17
    /**
18
     * The constructor expects an valid filename/path to the YAML file.
19
     * @param string $strConfigFile
20
     */
21
    public function __construct(string $strConfigFile)
22
    {
23
        if (!extension_loaded('yaml')) {
24
            // no codecoverage: can only be tested on clean system...
25
            trigger_error('The php extension [yaml] must be installed!', E_USER_ERROR); // @codeCoverageIgnore
26
        } else {
27
            $this->aConfig = $this->parseFile($strConfigFile);
28
        }
29
    }
30
31
    /**
32
     * Parse the given file an add all settings to the internal configuration.
33
     * @param string $strConfigFile
34
     */
35
    protected function parseFile(string $strConfigFile) : array
36
    {
37
        if (!file_exists($strConfigFile)) {
38
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
39
        }
40
41
        $strYaml = file_get_contents($strConfigFile);
42
        $aYAML = yaml_parse($strYaml);
43
        if ($aYAML === false) {
44
            // no codecoverage: haven't found example to test for returnvalue of false
45
            trigger_error('Invalid config file (' . $strConfigFile . ')', E_USER_ERROR); // @codeCoverageIgnore
46
        }
47
48
        return $aYAML;
49
    }
50
}
51