Passed
Pull Request — master (#12)
by Marwan
02:32
created

Config   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 18
eloc 42
c 6
b 0
f 0
dl 0
loc 199
ccs 52
cts 52
cp 1
rs 10

13 Methods

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