Completed
Push — master ( 4f2fad...dd1cd3 )
by Guillaume
07:10
created

ConfigurationLoader::loadConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Hogosha\Monitor\Configuration;
4
5
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException;
6
use Symfony\Component\Yaml\Yaml;
7
8
/**
9
 * @author Guillaume Cavana <[email protected]>
10
 */
11
class ConfigurationLoader
12
{
13
    /**
14
     * $rootDirectory.
15
     *
16
     * @var string
17
     */
18
    protected $rootDirectory;
19
20
    /**
21
     * $configFilepath.
22
     *
23
     * @var string
24
     */
25
    protected $configFilepath;
26
27
    /**
28
     * $filename.
29
     *
30
     * @var string
31
     */
32
    protected $filename;
33
34
    /**
35
     * @param string $rootDirectory Directory containing the config file
36
     * @param string $filename      Configuration file name
37
     */
38
    public function __construct($rootDirectory, $filename)
39
    {
40
        $this->rootDirectory = $rootDirectory;
41
        $this->filename = $filename;
42
    }
43
44
    /**
45
     * loadConfiguration.
46
     *
47
     * @return mixed
48
     */
49
    public function loadConfiguration()
50
    {
51
        $filePath = $this->getConfigurationFilepath();
52
53
        if (!file_exists($filePath)) {
54
            throw new ConfigurationLoadingException(
55
                sprintf('Unable to find a configuration file in %s', $this->rootDirectory)
56
            );
57
        }
58
59
        return Yaml::parse(file_get_contents($filePath));
60
    }
61
62
    /**
63
     * getConfigurationFilepath.
64
     *
65
     * @return string The configuration filepath
66
     */
67
    public function getConfigurationFilepath()
68
    {
69
        return $this->configFilepath ?: $this->rootDirectory.DIRECTORY_SEPARATOR.$this->filename;
70
    }
71
72
    /**
73
     * setConfigurationFilepath.
74
     *
75
     * @param string $configFilepath The configuration filepath
76
     */
77
    public function setConfigurationFilepath($configFilepath)
78
    {
79
        $this->configFilepath = $configFilepath;
80
    }
81
82
    /**
83
     * getRootDirectory.
84
     *
85
     * @return string The root directory
86
     */
87
    public function getRootDirectory()
88
    {
89
        return $this->rootDirectory;
90
    }
91
92
    /**
93
     * setRootDirectory.
94
     *
95
     * @param string $rootDirectory The root directory
96
     */
97
    public function setRootDirectory($rootDirectory)
98
    {
99
        $this->rootDirectory = $rootDirectory;
100
    }
101
}
102