YamlReader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 2
b 0
f 0
dl 0
loc 32
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A read() 0 20 4
1
<?php
2
3
/**
4
 * YamlExtension.php - Jaxon config reader
5
 *
6
 * Read the config data from a YAML formatted config file.
7
 *
8
 * @package jaxon-config
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Config\Reader;
16
17
use Jaxon\Config\Exception\YamlExtension;
18
use Jaxon\Config\Exception\FileAccess;
19
use Jaxon\Config\Exception\FileContent;
20
21
use function is_array;
22
use function realpath;
23
use function is_readable;
24
use function extension_loaded;
25
use function yaml_parse_file;
26
27
class YamlReader
28
{
29
    /**
30
     * Read options from a YAML formatted config file
31
     *
32
     * @param string $sConfigFile The full path to the config file
33
     *
34
     * @return array
35
     * @throws YamlExtension
36
     * @throws FileAccess
37
     * @throws FileContent
38
     */
39
    public static function read(string $sConfigFile): array
40
    {
41
        if(!extension_loaded('yaml'))
42
        {
43
            throw new YamlExtension();
44
        }
45
46
        $sConfigFile = realpath($sConfigFile);
47
        if(!is_readable($sConfigFile))
48
        {
49
            throw new FileAccess($sConfigFile);
50
        }
51
52
        $aConfigOptions = yaml_parse_file($sConfigFile);
53
        if(!is_array($aConfigOptions))
54
        {
55
            throw new FileContent($sConfigFile);
56
        }
57
58
        return $aConfigOptions;
59
    }
60
}
61