Completed
Push — master ( b2fe77...190add )
by Woody
01:54
created

ConfigCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A set() 0 4 1
A reducer() 0 16 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Config;
5
6
class ConfigCollection implements ConfigInterface
7
{
8
    /** @var ConfigInterface[] */
9
    private $configs;
10
11 2
    public function __construct(ConfigInterface ...$configs)
12
    {
13 2
        $this->configs = $configs;
14 2
    }
15
16 2
    public function get(string $dotPath, $default = null)
17
    {
18 2
        return array_reduce(array_reverse($this->configs), $this->reducer($dotPath), $default);
19
    }
20
21 2
    public function set(string $dotPath, $value)
22
    {
23 2
        $this->configs[0]->set($dotPath, $value);
24 2
    }
25
26
    private function reducer(string $dotPath): callable
27
    {
28 2
        return static function ($currentValue, ConfigInterface $config) use ($dotPath) {
29 2
            $found = $config->get($dotPath, null);
30
31 2
            if ($found === null) {
32 2
                return $currentValue;
33
            }
34
35 2
            if (is_array($currentValue) && is_array($found)) {
36 1
                return array_replace_recursive($currentValue, $found);
37
            }
38
39 2
            return $found;
40 2
        };
41
    }
42
}
43