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

ConfigCollection::reducer()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 1
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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