ConfigGroup   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 42
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
get() 0 1 ?
A __construct() 0 7 1
A describe() 0 4 1
A moreGeneralGroupName() 0 8 2
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
abstract class ConfigGroup
21
{
22
    protected $config;
23
    protected $group;
24
    protected $prefix;
25
    protected $postfix;
26
27
    public function __construct($config, $group, $prefix = '', $postfix = '.')
28
    {
29
        $this->config = $config;
30
        $this->group = $group;
31
        $this->prefix = $prefix;
32
        $this->postfix = $postfix;
33
    }
34
35
    /**
36
     * Return a description of the configuration group (with prefix and postfix).
37
     */
38
    public function describe($property)
39
    {
40
        return $this->prefix . $this->group . $this->postfix . $property;
41
    }
42
43
    /**
44
     * Get the requested configuration key from the most specific configuration
45
     * group that contains it.
46
     */
47
    abstract public function get($key);
48
49
    /**
50
     * Given a group name, such as "foo.bar.baz", return the next configuration
51
     * group in the fallback hierarchy, e.g. "foo.bar".
52
     */
53
    protected function moreGeneralGroupName($group)
54
    {
55
        $result = preg_replace('#\.[^.]*$#', '', $group);
56
        if ($result != $group) {
57
            return $result;
58
        }
59
        return false;
60
    }
61
}
62