Completed
Pull Request — master (#73)
by Aitor
02:56
created

ConfigurationFileReader::getDefaultConfigData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PhpGitHooks\Module\Configuration\Infrastructure\Persistence\Disk;
4
5
use PhpGitHooks\Module\Configuration\Domain\Config;
6
use PhpGitHooks\Module\Configuration\Model\ConfigurationFileReaderInterface;
7
use PhpGitHooks\Module\Configuration\Service\ConfigFactory;
8
use Symfony\Component\Yaml\Yaml;
9
10
class ConfigurationFileReader implements ConfigurationFileReaderInterface
11
{
12
    const CONFIG_FILE = 'php-git-hooks.yml';
13
    const DEFAULT_CONFIG_FILE = 'php-git-hooks.yml.default';
14
15
    /**
16
     * @return Config
17
     */
18
    public function getData()
19
    {
20
        $data = true === $this->configFileExists() ? $this->getConfigData() : [];
21
22
        $defaultData = $this->getDefaultConfigData();
23
24
        $data = array_replace_recursive($defaultData, $data);
25
26
        return ConfigFactory::fromArray($data);
27
    }
28
29
    /**
30
     * @return bool
31
     */
32
    private function configFileExists()
33
    {
34
        return file_exists(self::CONFIG_FILE);
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    private function getConfigData()
41
    {
42
        return Yaml::parse(file_get_contents(self::CONFIG_FILE));
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    private function getDefaultConfigData()
49
    {
50
        return Yaml::parse(
51
            file_get_contents(__DIR__ . '/' . self::DEFAULT_CONFIG_FILE)
52
        );
53
    }
54
}
55