Completed
Push — master ( e8d3de...59dd4d )
by Marwan
12s queued 11s
created

Config::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @author Marwan Al-Soltany <[email protected]>
4
 * @copyright Marwan Al-Soltany 2020
5
 * For the full copyright and license information, please view
6
 * the LICENSE file that was distributed with this source code.
7
 */
8
9
namespace MAKS\AmqpAgent;
10
11
use Exception;
12
use MAKS\AmqpAgent\Helper\Utility;
13
use MAKS\AmqpAgent\Exception\ConfigFileNotFoundException;
14
15
/**
16
 * A class that turns the configuration file into an object.
17
 *
18
 * Example:
19
 * ```
20
 * $config = new Config('path/to/some/config-file.php'); // specific config
21
 * $config = new Config(); // default config
22
 * ```
23
 *
24
 * @since 1.0.0
25
 * @property array $connectionOptions
26
 * @property array $channelOptions
27
 * @property array $queueOptions
28
 * @property array $exchangeOptions
29
 * @property array $bindOptions
30
 * @property array $qosOptions
31
 * @property array $waitOptions
32
 * @property array $messageOptions
33
 * @property array $publishOptions
34
 * @property array $consumeOptions
35
 */
36
final class Config
37
{
38
    /**
39
     * The default name of the configuration file.
40
     * @var string
41
     */
42
    public const DEFAULT_CONFIG_FILE_NAME = 'maks-amqp-agent-config';
43
44
    /**
45
     * The default name of the configuration file.
46
     * @var string
47
     */
48
    public const DEFAULT_CONFIG_FILE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . self::DEFAULT_CONFIG_FILE_NAME . '.php';
49
50
    /**
51
     * The multidimensional configuration array.
52
     * @var array
53
     */
54
    private $config;
55
56
    /**
57
     * Configuration file path.
58
     * @var string
59
     */
60
    private $configPath;
61
62
63
    /**
64
     * Config object constructor.
65
     * @param string|null $configPath [optional] The path to AMQP Agent configuration file.
66
     */
67 21
    public function __construct(?string $configPath = null)
68
    {
69 21
        $configFile = realpath($configPath ?? self::DEFAULT_CONFIG_FILE_PATH);
70
71 21
        if (!file_exists($configFile)) {
72 1
            throw new ConfigFileNotFoundException(
73 1
                "AMQP Agent configuration file cloud not be found, check if the given path \"{$configPath}\" exists."
74
            );
75
        }
76
77 21
        $this->config = include($configFile);
78 21
        $this->configPath = $configFile;
79
80 21
        $this->repair();
81 21
    }
82
83
    /**
84
     * Gets the the given key from the configuration array via public property access notation.
85
     * @param string $key
86
     * @return mixed
87
     */
88 3
    public function __get(string $key)
89
    {
90 3
        return $this->config[$key];
91
    }
92
93
    /**
94
     * Sets the the given key in the configuration array via public property assignment notation.
95
     * @param string $key
96
     * @param mixed $value
97
     * @return void
98
     */
99 1
    public function __set(string $key, $value)
100
    {
101 1
        $this->config[$key] = $value;
102 1
    }
103
104
    /**
105
     * Returns config file path if the object was casted to a string.
106
     * @return string
107
     */
108 1
    public function __toString()
109
    {
110 1
        return $this->configPath;
111
    }
112
113
114
    /**
115
     * Repairs the config array if first-level of the passed array does not have all keys.
116
     * @return void
117
     */
118 21
    private function repair(): void
119
    {
120 21
        $config = require(self::DEFAULT_CONFIG_FILE_PATH);
121
122 21
        foreach ($config as $key => $value) {
123 21
            if (!array_key_exists($key, $this->config)) {
124 4
                $this->config[$key] = [];
125
            }
126
        }
127
128 21
        unset($config);
129 21
    }
130
131
    /**
132
     * Checks wether a value exists in the configuration array via dot-notation representation.
133
     * @since 1.2.2
134
     * @param string $key The dotted key representation.
135
     * @return bool True if key is set otherwise false.
136
     */
137 1
    public function has(string $key): bool
138
    {
139 1
        $value = Utility::getArrayValueByKey($this->config, $key, null);
140
141 1
        return isset($value);
142
    }
143
144
    /**
145
     * Gets a value of a key from the configuration array via dot-notation representation.
146
     * @since 1.2.2
147
     * @param string $key The dotted key representation.
148
     * @return mixed The requested value or null.
149
     */
150 2
    public function get(string $key)
151
    {
152 2
        $value = Utility::getArrayValueByKey($this->config, $key);
153
154 2
        return $value;
155
    }
156
157
    /**
158
     * Sets a value of a key from the configuration array via dot-notation representation.
159
     * @since 1.2.2
160
     * @param string $key The dotted key representation.
161
     * @param string $value The value to set.
162
     * @return self
163
     */
164 1
    public function set(string $key, $value)
165
    {
166 1
        Utility::setArrayValueByKey($this->config, $key, $value);
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * Returns the default configuration array.
173
     * @return array
174
     */
175 1
    public function getDefaultConfig(): array
176
    {
177 1
        return include(self::DEFAULT_CONFIG_FILE_PATH);
178
    }
179
180
    /**
181
     * Returns the current configuration array.
182
     * @return array
183
     */
184 3
    public function getConfig(): array
185
    {
186 3
        return $this->config;
187
    }
188
189
    /**
190
     * Sets a new configuration array to be used instead of the current and generates a new flat version of it.
191
     * @param array $config
192
     * @return self
193
     */
194 4
    public function setConfig(array $config): self
195
    {
196 4
        $this->config = $config;
197
198 4
        $this->repair();
199
200 4
        return $this;
201
    }
202
203
    /**
204
     * Returns the path of the configuration file.
205
     * @return string
206
     */
207 2
    public function getConfigPath(): string
208
    {
209 2
        return $this->configPath;
210
    }
211
212
    /**
213
     * Sets the path of the configuration file and rebuilds the internal state of the object.
214
     * @param string $configPath
215
     * @return self
216
     */
217 2
    public function setConfigPath(string $configPath): self
218
    {
219
        try {
220 2
            $this->config = include($configPath);
221 1
            $this->configPath = $configPath;
222
223 1
            $this->repair();
224 1
        } catch (Exception $error) {
225 1
            throw new ConfigFileNotFoundException(
226 1
                "Something went wrong when trying to include the file and rebuild the configuration, check if the given path \"{$configPath}\" exists.",
227 1
                (int)$error->getCode(),
228
                $error
229
            );
230
        }
231
232 1
        return $this;
233
    }
234
}
235