ConfigTreeBuilder::addSettingsFromPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
namespace ConfigTree\Builder;
3
4
use ConfigTree\Tree\ConfigTree;
5
use ConfigTree\FileParsing\ArrayableFileFactory;
6
use ConfigTree\Exception\FileFormatNotParsable;
7
use ConfigTree\Exception\FileNotReadable;
8
9
class ConfigTreeBuilder
10
{
11
    /** @var ArrayableFileFactory */
12
    private $arrayableFileFactory;
13
14
    /** @var ConfigTree */
15
    private $configTreeToReturn;
16
17
    public function __construct(ArrayableFileFactory $arrayableFileFactory = null)
18
    {
19
        $this->arrayableFileFactory = $arrayableFileFactory ?: new ArrayableFileFactory();
20
        $this->configTreeToReturn = new ConfigTree([]);
21
    }
22
23
    /**
24
     * @throws FileFormatNotParsable
25
     * @throws FileNotReadable
26
     *
27
     * @param string[] $pathsToFiles
28
     */
29
    public function addSettingsFromPaths(array $pathsToFiles)
30
    {
31
        foreach ($pathsToFiles as $pathToFile) {
32
            $this->addSettingsFromPath($pathToFile);
33
        }
34
    }
35
36
    /**
37
     * @throws FileFormatNotParsable
38
     * @throws FileNotReadable
39
     */
40
    private function addSettingsFromPath(string $pathToFile)
41
    {
42
        $arrayableFile = $this->arrayableFileFactory->getArrayableFileFromPath($pathToFile);
43
        $fileContentsAsArray = $arrayableFile->toArray();
44
        $config = new ConfigTree($fileContentsAsArray);
45
        $this->configTreeToReturn = $this->configTreeToReturn->withAnotherConfigTreeMergedIn($config);
46
    }
47
48
    public function buildConfigTreeAndReset() : ConfigTree
49
    {
50
        $configToReturn = $this->configTreeToReturn;
51
        $this->configTreeToReturn = new ConfigTree([]);
52
        return $configToReturn;
53
    }
54
}
55