ConfigFallback::getWithFallback()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 4
1
<?php
2
namespace Consolidation\Config\Util;
3
4
/**
5
 * Fetch a configuration value from a configuration group. If the
6
 * desired configuration value is not found in the most specific
7
 * group named, keep stepping up to the next parent group until a
8
 * value is located.
9
 *
10
 * Given the following constructor inputs:
11
 *   - $prefix  = "command."
12
 *   - $group   = "foo.bar.baz"
13
 *   - $postfix = ".options."
14
 * Then the `get` method will then consider, in order:
15
 *   - command.foo.bar.baz.options
16
 *   - command.foo.bar.options
17
 *   - command.foo.options
18
 * If any of these contain an option for "$key", then return its value.
19
 */
20
class ConfigFallback extends ConfigGroup
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function get($key)
26
    {
27
        return $this->getWithFallback($key, $this->group, $this->prefix, $this->postfix);
28
    }
29
30
    /**
31
     * Fetch an option value from a given key, or, if that specific key does
32
     * not contain a value, then consult various fallback options until a
33
     * value is found.
34
     *
35
     */
36
    protected function getWithFallback($key, $group, $prefix = '', $postfix = '.')
37
    {
38
        $configKey = "{$prefix}{$group}${postfix}{$key}";
39
        if ($this->config->has($configKey)) {
40
            return $this->config->get($configKey);
41
        }
42
        if ($this->config->hasDefault($configKey)) {
43
            return $this->config->getDefault($configKey);
44
        }
45
        $moreGeneralGroupname = $this->moreGeneralGroupName($group);
46
        if ($moreGeneralGroupname) {
47
            return $this->getWithFallback($key, $moreGeneralGroupname, $prefix, $postfix);
48
        }
49
        return null;
50
    }
51
}
52