ConfigurationLoader::getConfigurationFilepath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Openl10n\Cli\ServiceContainer\Configuration;
4
5
use Openl10n\Cli\ServiceContainer\Exception\ConfigurationLoadingException;
6
use Symfony\Component\Yaml\Yaml;
7
8
class ConfigurationLoader
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $rootDirectory;
14
15
    /**
16
     * @var string
17
     */
18
    protected $configFilepath;
19
20
    /**
21
     * @var string
22
     */
23
    protected $filename;
24
25
    /**
26
     * @param string $rootDirectory Directory containing the config file
27
     * @param string $filename      Configuration file name
28
     */
29
    public function __construct($rootDirectory, $filename)
30
    {
31
        $this->rootDirectory = $rootDirectory;
32
        $this->filename = $filename;
33
    }
34
35
    /**
36
     * Read the config file and return the configuration array.
37
     *
38
     * @return array The configuration
39
     */
40
    public function loadConfiguration()
41
    {
42
        $filepath = $this->getConfigurationFilepath();
43
44
        if (!file_exists($filepath)) {
45
            throw new ConfigurationLoadingException(
46
                sprintf('Unable to find a configuration file in %s', $this->rootDirectory
47
            ));
48
        }
49
50
        return Yaml::parse(file_get_contents($filepath));
51
    }
52
53
    /**
54
     * @return string The configuration filepath
55
     */
56
    public function getConfigurationFilepath()
57
    {
58
        return $this->configFilepath ?: $this->rootDirectory.DIRECTORY_SEPARATOR.$this->filename;
59
    }
60
61
    /**
62
     * @param $configFilepath string The configuration filepath
63
     */
64
    public function setConfigurationFilepath($configFilepath)
65
    {
66
        $this->configFilepath = $configFilepath;
67
    }
68
69
    /**
70
     * @return string The root directory
71
     */
72
    public function getRootDirectory()
73
    {
74
        return $this->rootDirectory;
75
    }
76
77
    /**
78
     * @param string $rootDirectory The root directory
79
     */
80
    public function setRootDirectory($rootDirectory)
81
    {
82
        $this->rootDirectory = $rootDirectory;
83
    }
84
}
85