Completed
Pull Request — master (#6)
by Daniel
02:39
created

Config::getValue()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
nop 1
1
<?php
2
3
namespace Jellyfish\Config;
4
5
use ArrayObject;
6
use Exception;
7
use Jellyfish\Config\Exception\ConfigKeyNotFoundException;
8
use Jellyfish\Config\Exception\NotSupportedConfigValueTypeException;
9
10
class Config implements ConfigInterface
11
{
12
    protected const CONFIG_FILE_PREFIX = 'config-';
13
    protected const CONFIG_FILE = 'default';
14
    protected const CONFIG_FILE_SUFFIX = '.php';
15
16
    /**
17
     * @var \ArrayObject|null
18
     */
19
    protected $config;
20
21
    /**
22
     * @var string
23
     */
24
    protected $appDir;
25
26
    /**
27
     * @var string
28
     */
29
    protected $environment;
30
31
    /**
32
     * @param string $appDir
33
     * @param string $environment
34
     *
35
     * @throws Exception
36
     */
37
    public function __construct(
38
        string $appDir,
39
        string $environment
40
    ) {
41
        $this->appDir = $appDir;
42
        $this->environment = $environment;
43
44
        $this->initialize();
45
    }
46
47
    /**
48
     * @param string $key
49
     * @param string|null $default
50
     *
51
     * @return string
52
     *
53
     * @throws \Jellyfish\Config\Exception\ConfigKeyNotFoundException
54
     * @throws \Jellyfish\Config\Exception\NotSupportedConfigValueTypeException
55
     */
56
    public function get(string $key, ?string $default = null): string
57
    {
58
        if ($default !== null && !$this->hasValue($key)) {
59
            return $default;
60
        }
61
62
        if (!$this->hasValue($key)) {
63
            throw new ConfigKeyNotFoundException(sprintf('Could not find key "%s" in "%s"', $key, __CLASS__));
64
        }
65
66
        return $this->getValue($key);
67
    }
68
69
    /**
70
     * @param string $key
71
     *
72
     * @return string
73
     *
74
     * @throws \Jellyfish\Config\Exception\NotSupportedConfigValueTypeException
75
     */
76
    protected function getValue(string $key): string
77
    {
78
        $value = $this->config[$key];
79
80
        if (\is_string($value)) {
81
            return $value;
82
        }
83
84
        if (\is_int($value) || \is_float($value) || \is_bool($value)) {
85
            return (string) $value;
86
        }
87
88
        throw new NotSupportedConfigValueTypeException(sprintf('Value type for key "%s" is not supported.', $key));
89
    }
90
91
    /**
92
     * @param string $key
93
     *
94
     * @return bool
95
     */
96
    protected function hasValue(string $key): bool
97
    {
98
        return isset($this->config[$key]);
99
    }
100
101
    /**
102
     * @return void
103
     *
104
     * @throws Exception
105
     */
106
    protected function initialize(): void
107
    {
108
        $config = new ArrayObject();
109
110
        $this->buildConfig($config);
111
        $this->buildConfig($config, $this->environment);
112
113
        $this->config = $config;
114
    }
115
116
    /**
117
     * @param \ArrayObject $config
118
     * @param string $environment
119
     *
120
     * @return \ArrayObject
121
     */
122
    protected function buildConfig(ArrayObject $config, string $environment = null): ArrayObject
123
    {
124
        $configFile = $environment ?? self::CONFIG_FILE;
125
        $fileName = self::CONFIG_FILE_PREFIX . $configFile . self::CONFIG_FILE_SUFFIX;
126
        $pathToConfigFile = $this->appDir . $fileName;
127
128
        if (file_exists($pathToConfigFile)) {
129
            include $pathToConfigFile;
130
        }
131
132
        return $config;
133
    }
134
}
135