ConfigManager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 29
dl 0
loc 120
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addFile() 0 4 1
A initialize() 0 5 2
A getConfig() 0 16 4
A get() 0 12 3
A reload() 0 3 1
A clear() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebThumbnailer\Application;
6
7
use WebThumbnailer\Exception\BadRulesException;
8
use WebThumbnailer\Exception\IOException;
9
use WebThumbnailer\Utils\DataUtils;
10
use WebThumbnailer\Utils\FileUtils;
11
12
/**
13
 * Load configuration from JSON files.
14
 */
15
class ConfigManager
16
{
17
    /** @var string Flag telling a setting is not found. */
18
    public const NOT_FOUND = 'NOT_FOUND';
19
20
    /** @var string[] List of JSON configuration file path. */
21
    public static $configFiles = [
22
        FileUtils::RESOURCES_PATH . 'settings.json',
23
    ];
24
25
    /** @var mixed[] Loaded config array. */
26
    protected static $loadedConfig;
27
28
    /**
29
     * Rebuild the loaded config array from config files.
30
     *
31
     * @throws IOException
32
     * @throws BadRulesException
33
     */
34
    public static function reload(): void
35
    {
36
        static::initialize();
37
    }
38
39
    /**
40
     * Initialize loaded conf in ConfigManager.
41
     *
42
     * @throws IOException
43
     * @throws BadRulesException
44
     */
45
    protected static function initialize(): void
46
    {
47
        static::$loadedConfig = [];
48
        foreach (static::$configFiles as $configFile) {
49
            static::$loadedConfig = array_replace_recursive(static::$loadedConfig, DataUtils::loadJson($configFile));
50
        }
51
    }
52
53
    /**
54
     * Add a configuration file.
55
     *
56
     * @param string $file path.
57
     *
58
     * @throws BadRulesException
59
     * @throws IOException
60
     */
61
    public static function addFile(string $file): void
62
    {
63
        static::$configFiles[] = $file;
64
        static::initialize();
65
    }
66
67
    /**
68
     * Clear the current config
69
     *
70
     * @throws BadRulesException
71
     * @throws IOException
72
     */
73
    public static function clear(): void
74
    {
75
        static::$configFiles = [
76
            FileUtils::RESOURCES_PATH . 'settings.json',
77
        ];
78
        static::reload();
79
    }
80
81
    /**
82
     * Get a setting.
83
     *
84
     * Supports nested settings with dot separated keys.
85
     * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
86
     * or in JSON:
87
     *   { "config": { "stuff": {"option": "mysetting" } } } }
88
     *
89
     * @param string $setting Asked setting, keys separated with dots.
90
     * @param mixed  $default Default value if not found.
91
     *
92
     * @return mixed Found setting, or the default value.
93
     *
94
     * @throws BadRulesException
95
     * @throws IOException
96
     */
97
    public static function get(string $setting, $default = '')
98
    {
99
        if (empty(static::$loadedConfig)) {
100
            static::initialize();
101
        }
102
103
        $settings = explode('.', $setting);
104
        $value = static::getConfig($settings, static::$loadedConfig);
105
        if ($value == static::NOT_FOUND) {
106
            return $default;
107
        }
108
        return $value;
109
    }
110
111
    /**
112
     * Recursive function which find asked setting in the loaded config.
113
     *
114
     * @param string[] $settings Ordered array which contains keys to find.
115
     * @param mixed[]  $config   Loaded settings, then sub-array.
116
     *
117
     * @return mixed Found setting or NOT_FOUND flag.
118
     */
119
    protected static function getConfig(array $settings, array $config)
120
    {
121
        if (count($settings) === 0) {
122
            return static::NOT_FOUND;
123
        }
124
125
        $setting = array_shift($settings);
126
        if (!isset($config[$setting])) {
127
            return static::NOT_FOUND;
128
        }
129
130
        if (count($settings) > 0) {
131
            return static::getConfig($settings, $config[$setting]);
132
        }
133
134
        return $config[$setting];
135
    }
136
}
137