Config   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.46%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 204
ccs 47
cts 57
cp 0.8246
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getArray() 0 4 1
A init() 0 15 3
A get() 0 8 3
A set() 0 4 1
A setArray() 0 4 1
A reset() 0 10 1
A parse() 0 10 3
A write() 0 30 4
A clear() 0 4 1
A _writeContent() 0 6 1
1
<?php
2
namespace Redaxscript;
3
4
use function array_key_exists;
5
use function array_keys;
6
use function basename;
7
use function dirname;
8
use function end;
9
use function is_array;
10
use function is_file;
11
use function parse_url;
12
use function str_replace;
13
use function trim;
14
15
/**
16
 * children class to store database config
17
 *
18
 * @since 2.4.0
19
 *
20
 * @package Redaxscript
21
 * @category Config
22
 * @author Henry Ruhs
23
 */
24
25
class Config extends Singleton
26
{
27
	/**
28
	 * path to the config
29
	 *
30
	 * @var string
31
	 */
32
33
	protected static $_configPath = 'config.php';
34
35
	/**
36
	 * array of the config
37
	 *
38
	 * @var array
39
	 */
40
41
	protected static $_configArray = [];
42
43
	/**
44
	 * init the class
45
	 *
46
	 * @since 2.4.0
47
	 *
48
	 * @param string $configPath path to the config
49
	 */
50
51 6
	public function init(string $configPath = null) : void
52
	{
53 6
		if (is_file($configPath))
54
		{
55 6
			self::$_configPath = $configPath;
56
		}
57
58
		/* load config */
59
60 6
		$configArray = include(self::$_configPath);
61 6
		if (is_array($configArray))
62
		{
63 1
			$this->setArray($configArray);
64
		}
65 6
	}
66
67
	/**
68
	 * get the value from config
69
	 *
70
	 * @since 2.2.0
71
	 *
72
	 * @param string $key key of the item
73
	 *
74
	 * @return string|null
75
	 */
76
77 1
	public function get(string $key = null) : ?string
78
	{
79 1
		if (is_array(self::$_configArray) && array_key_exists($key, self::$_configArray))
80
		{
81 1
			return self::$_configArray[$key];
82
		}
83
		return null;
84
	}
85
86
	/**
87
	 * get the array from config
88
	 *
89
	 * @since 4.0.0
90
	 *
91
	 * @return array
92
	 */
93
94 8
	public function getArray() : array
95
	{
96 8
		return self::$_configArray;
97
	}
98
99
	/**
100
	 * set the value to config
101
	 *
102
	 * @since 2.2.0
103
	 *
104
	 * @param string $key key of the item
105
	 * @param string $value value of the item
106
	 */
107
108 5
	public function set(string $key = null, string $value = null) : void
109
	{
110 5
		self::$_configArray[$key] = $value;
111 5
	}
112
113
	/**
114
	 * set the array to config
115
	 *
116
	 * @since 5.0.0
117
	 *
118
	 * @param array $configArray array of the config
119
	 */
120
121 8
	public function setArray(array $configArray = []) : void
122
	{
123 8
		self::$_configArray = $configArray;
124 8
	}
125
126
	/**
127
	 * reset the config
128
	 *
129
	 * @since 4.5.0
130
	 */
131
132
	public function reset() : void
133
	{
134
		$this->clear();
135
		$this->set('dbType', null);
136
		$this->set('dbHost', null);
137
		$this->set('dbName', null);
138
		$this->set('dbUser', null);
139
		$this->set('dbPassword', null);
140
		$this->set('dbPrefix', null);
141
	}
142
143
	/**
144
	 * parse from database url
145
	 *
146
	 * @param string $dbUrl database url to be parsed
147
	 *
148
	 * @since 3.0.0
149
	 */
150
151 4
	public function parse(string $dbUrl = null) : void
152
	{
153 4
		$dbArray = parse_url($dbUrl);
154 4
		$this->clear();
155 4
		$this->set('dbType', str_replace('postgres', 'pgsql', $dbArray['scheme']));
156 4
		$this->set('dbHost', array_key_exists('port', $dbArray) ? $dbArray['host'] . ':' . $dbArray['port'] : $dbArray['host']);
157 4
		$this->set('dbName', array_key_exists('path', $dbArray) ? trim($dbArray['path'], '/') : null);
158 4
		$this->set('dbUser', $dbArray['user'] ?? null);
159 4
		$this->set('dbPassword', $dbArray['pass'] ?? null);
160 4
	}
161
162
	/**
163
	 * write the config
164
	 *
165
	 * @since 2.4.0
166
	 *
167
	 * @return bool
168
	 */
169
170 1
	public function write() : bool
171
	{
172 1
		$configKeys = array_keys(self::$_configArray);
173 1
		$lastKey = end($configKeys);
174
175
		/* collect content */
176
177 1
		$content = '<?php' . PHP_EOL . 'return' . PHP_EOL . '[' . PHP_EOL;
178 1
		foreach (self::$_configArray as $key => $value)
179
		{
180 1
			if ($value)
181
			{
182 1
				$content .= '	\'' . $key . '\' => \'' . $value . '\'';
183
			}
184
			else
185
			{
186 1
				$content .= '	\'' . $key . '\' => null';
187
			}
188 1
			if ($key !== $lastKey)
189
			{
190 1
				$content .= ',';
191
			}
192 1
			$content .= PHP_EOL;
193
		}
194 1
		$content .= '];' . PHP_EOL;
195
196
		/* write content */
197
198 1
		return $this->_writeContent($content);
199
	}
200
201
	/**
202
	 * clear the config
203
	 *
204
	 * @since 3.0.0
205
	 */
206
207 4
	public function clear() : void
208
	{
209 4
		$this->setArray([]);
210 4
	}
211
212
	/**
213
	 * write content to file
214
	 *
215
	 * @since 2.4.0
216
	 *
217
	 * @param string|null $content
218
	 *
219
	 * @return bool
220
	 */
221
222 1
	protected function _writeContent(string $content = null) : bool
223
	{
224 1
		$filesystem = new Filesystem\File();
225 1
		$filesystem->init(dirname(self::$_configPath));
226 1
		return $filesystem->writeFile(basename (self::$_configPath), $content);
227
	}
228
}
229