ConfigLoader::getConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\YamlConfig\Business\Loader;
6
7
8
use DataProvider\YamlConfigDataProvider;
9
use DataProvider\YamlConfigFileListDataProvider;
10
use Xervice\YamlConfig\Business\Hydrator\HydratorCollection;
11
use Xervice\YamlConfig\Business\Reader\ReaderInterface;
12
13
class ConfigLoader implements ConfigLoaderInterface
14
{
15
    /**
16
     * @var YamlConfigFileListDataProvider
17
     */
18
    private $fileList;
19
20
    /**
21
     * @var \Xervice\YamlConfig\Business\Reader\ReaderInterface
22
     */
23
    private $fileReader;
24
25
    /**
26
     * @var \Xervice\YamlConfig\Business\Hydrator\HydratorCollection
27
     */
28
    private $hydratorCollection;
29
30
    /**
31
     * ConfigLoader constructor.
32
     *
33
     * @param YamlConfigFileListDataProvider $fileList
34
     * @param \Xervice\YamlConfig\Business\Reader\ReaderInterface $fileReader
35
     * @param \Xervice\YamlConfig\Business\Hydrator\HydratorCollection $hydratorCollection
36
     */
37 2
    public function __construct(
38
        YamlConfigFileListDataProvider $fileList,
39
        ReaderInterface $fileReader,
40
        HydratorCollection $hydratorCollection
41
    ) {
42 2
        $this->fileList = $fileList;
43 2
        $this->fileReader = $fileReader;
44 2
        $this->hydratorCollection = $hydratorCollection;
45 2
    }
46
47
48
    /**
49
     * @return \DataProvider\YamlConfigDataProvider
50
     * @throws \Xervice\YamlConfig\Business\Exception\ConfigException
51
     */
52 2
    public function getConfig(): YamlConfigDataProvider
53
    {
54 2
        $config = new YamlConfigDataProvider();
55
56 2
        foreach ($this->fileList->getFiles() as $file) {
57 2
            $configData = $this->fileReader->getArrayFromFile($file);
58 2
            $config = $this->hydrateConfigs($configData, $config);
59
        }
60
61 2
        return $config;
62
    }
63
64
    /**
65
     * @param $configData
66
     * @param $config
67
     *
68
     * @return \DataProvider\YamlConfigDataProvider
69
     * @throws \Xervice\YamlConfig\Business\Exception\ConfigException
70
     */
71 2
    private function hydrateConfigs($configData, $config): YamlConfigDataProvider
72
    {
73 2
        foreach ($this->hydratorCollection as $hydrator) {
74 1
            $config = $hydrator->hydrateConfig($configData, $config);
75
        }
76 2
        return $config;
77
    }
78
79
}