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

YamlConfigLoader::loadSources()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 2
crap 2.0185
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