YAMLConfig::parseFile()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 1
b 0
f 1
nc 6
nop 1
dl 0
loc 17
rs 9.9666
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
     * @return array<mixed>
35
     */
36
    protected function parseFile(string $strConfigFile) : array
37
    {
38
        if (!file_exists($strConfigFile)) {
39
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
40
        }
41
42
        $strYaml = file_get_contents($strConfigFile);
43
        $aYAML = [];
44
        if ($strYaml !== false) {
45
            $aYAML = yaml_parse($strYaml);
46
            if ($aYAML === false) {
47
                // no codecoverage: haven't found example to test for returnvalue of false
48
                trigger_error('Invalid config file (' . $strConfigFile . ')', E_USER_ERROR); // @codeCoverageIgnore
49
            }
50
        }
51
52
        return $aYAML;
53
    }
54
}
55