Passed
Push — master ( 20ea37...2b6380 )
by Thorsten
01:41
created

YamlConfigLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 30
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 9 2
A loadSources() 0 10 2
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/config project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\Config;
12
13
use Symfony\Component\Yaml\Yaml;
14
15
final class YamlConfigLoader implements ConfigLoaderInterface
16
{
17
    private $yamlParser;
18
19 2
    public function __construct(Yaml $yamlParser = null)
20
    {
21 2
        $this->yamlParser = $yamlParser ?? new Yaml;
22 2
    }
23
24 2
    public function load(array $locations, array $sources): array
25
    {
26
        return array_reduce($locations, function (array $config, string $location) use ($sources): array {
27 2
            if (substr($location, -1) !== '/') {
28 2
                $location .= '/';
29
            }
30 2
            return array_replace_recursive($config, $this->loadSources($location, $sources));
31 2
        }, []);
32
    }
33
34
    private function loadSources($location, array $sources)
35
    {
36 2
        return array_reduce($sources, function (array $config, string $source) use ($location): array {
37 2
            $filepath = $location.$source;
38 2
            if (is_readable($filepath)) {
39 2
                return array_replace_recursive($config, $this->yamlParser->parse(file_get_contents($filepath)));
40
            }
41
            return $config;
42 2
        }, []);
43
    }
44
}
45