Passed
Pull Request — master (#31)
by Daniel
02:18
created

Config::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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