Completed
Push — master ( 21ecde...d4facc )
by Nazar
04:59
created

Options::apply_formatting()   C

Complexity

Conditions 14
Paths 15

Size

Total Lines 48
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 14.0032

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 14
eloc 39
c 1
b 0
f 1
nc 15
nop 1
dl 0
loc 48
ccs 38
cts 39
cp 0.9744
crap 14.0032
rs 5.0498

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Config;
9
use
10
	cs\Config,
11
	cs\DB;
12
13
/**
14
 * Class for getting of db and storage configuration of module
15
 */
16
class Options {
17
	/**
18
	 * Get formats for all supported options
19
	 *
20
	 * Format includes option type, default value, supported values or ranges, whether option is password or is multilingual
21
	 *
22
	 * @return array[]
23
	 */
24 54
	public static function get_formatting () {
25 54
		$Config    = Config::instance();
26 54
		$languages = array_unique(
27
			array_merge(
28 54
				_mb_substr(get_files_list(LANGUAGES, '/^.*?\.php$/i', 'f'), 0, -4) ?: [],
29 54
				_mb_substr(get_files_list(LANGUAGES, '/^.*?\.json$/i', 'f'), 0, -5) ?: []
30
			)
31
		);
32 54
		asort($languages);
33
		/** @noinspection PhpParamsInspection */
34 54
		$modules_can_be_default = array_filter(
35 54
			array_keys($Config->components['modules']),
36
			function ($module) use ($Config) {
37 54
				return $Config->module($module) && file_exists_with_extension(MODULES."/$module/index", ['php', 'html', 'json']);
38 54
			}
39
		);
40 54
		$themes                 = get_files_list(THEMES, false, 'd');
41 54
		asort($themes);
42 54
		return static::get_formatting_normalize(
43
			[
44
				'array'        => [
45
					'url'           => [],
46
					'cookie_domain' => []
47 54
				],
48
				'int_bool'     => [
49 42
					'site_mode'                         => 1,
50 42
					'title_reverse'                     => 0,
51 42
					'cache_compress_js_css'             => 1,
52 42
					'frontend_load_optimization'        => 1,
53 42
					'vulcanization'                     => 1,
54 42
					'put_js_after_body'                 => 1,
55 42
					'disable_webcomponents'             => 0,
56 42
					'multilingual'                      => 0,
57 42
					'db_balance'                        => 0,
58 42
					'db_mirror_mode'                    => DB::MIRROR_MODE_MASTER_MASTER,
59 42
					'gravatar_support'                  => 0,
60 42
					'smtp'                              => 0,
61 42
					'smtp_auth'                         => 0,
62 42
					'allow_user_registration'           => 1,
63 42
					'require_registration_confirmation' => 1,
64 42
					'auto_sign_in_after_registration'   => 1,
65 42
					'registration_confirmation_time'    => 1,
66 42
					'remember_user_ip'                  => 0,
67 42
					'simple_admin_mode'                 => 1
68
				],
69
				'int_range'    => [
70
					'inserts_limit'                => [
71
						'min'   => 1,
72
						'value' => 1000
73
					],
74
					'key_expire'                   => [
75
						'min'   => 1,
76
						'value' => 120
77
					],
78
					'session_expire'               => [
79
						'min'   => 1,
80
						'value' => 2592000
81
					],
82
					'update_ratio'                 => [
83
						'min'   => 0,
84
						'max'   => 100,
85
						'value' => 75
86
					],
87
					'sign_in_attempts_block_count' => [
88
						'min'   => 0,
89
						'value' => 0
90
					],
91
					'sign_in_attempts_block_time'  => [
92
						'min'   => 1,
93
						'value' => 5
94
					],
95
					'password_min_length'          => [
96
						'min'   => 1,
97
						'value' => 4
98
					],
99
					'password_min_strength'        => [
100
						'min'   => 0,
101
						'max'   => 7,
102
						'value' => 3
103
					]
104
				],
105
				'set_single'   => [
106
					'smtp_secure'    => [
107
						'value'  => '',
108
						'values' => ['', 'ssl', 'tls']
109
					],
110
					'language'       => [
111 54
						'value'  => 'English',
112 54
						'values' => $Config->core['active_languages'],
113 54
						'source' => 'active_languages'
114
					],
115
					'timezone'       => [
116 54
						'value'  => 'UTC',
117 54
						'values' => get_timezones_list()
118
					],
119
					'default_module' => [
120 54
						'value'  => 'System',
121 54
						'values' => $modules_can_be_default
122
					],
123
					'theme'          => [
124 54
						'value'  => 'CleverStyle',
125 54
						'values' => $themes
126
					]
127
				],
128
				'set_multiple' => [
129
					'active_languages' => [
130
						'value'  => [
131
							'English'
132
						],
133 54
						'values' => $languages
134
					]
135
				],
136
				'string'       => [
137
					'admin_email'   => '',
138
					'cookie_prefix' => '',
139
					'smtp_host'     => '',
140
					'smtp_port'     => '',
141
					'smtp_user'     => '',
142
					'smtp_password' => [
143
						'value'    => '',
144
						'password' => true
145
					],
146
					'mail_from'     => ''
147
				],
148
				'text'         => [
149
					'title_delimiter' => ' | ',
150
					'site_name'       => [
151
						'multilingual' => true,
152
						'value'        => ''
153
					],
154
					'closed_title'    => [
155
						'multilingual' => true,
156
						'value'        => 'Site closed'
157
					],
158
					'mail_from_name'  => [
159
						'multilingual' => true,
160
						'value'        => 'Administrator'
161
					]
162
				],
163
				'html'         => [
164
					'closed_text'    => [
165
						'multilingual' => true,
166
						'value'        => '<p>Site closed for maintenance</p>'
167
					],
168
					'mail_signature' => [
169
						'multilingual' => true,
170
						'value'        => ''
171
					]
172
				]
173
			]
174
		);
175
	}
176
	/**
177
	 * @param array[] $format
178
	 *
179
	 * @return array[]
180
	 */
181 54
	protected static function get_formatting_normalize ($format) {
182 54
		foreach ($format as $type => &$items) {
183 54
			foreach ($items as $item => &$data) {
184 54
				if (!is_array($data) || !isset($data['value'])) {
185
					$data = [
186 54
						'value' => $data
187
					];
188
				}
189 54
				$data['type'] = $type;
190 54
				if (($type == 'set_single' || $type == 'set_multiple') && !is_array_assoc($data['values'])) {
191 54
					$data['values'] = array_combine($data['values'], $data['values']);
192
				}
193
			}
194 54
			unset($data);
195
		}
196 54
		return array_merge(...array_values($format));
197
	}
198
	/**
199
	 * Get default values for all supported options
200
	 *
201
	 * @return array
202
	 */
203 54
	public static function get_defaults () {
204 54
		return array_map(
205
			function ($option) {
206 54
				return $option['value'];
207 54
			},
208 54
			static::get_formatting()
209
		);
210
	}
211
	/**
212
	 * Get list of multilingual options
213
	 *
214
	 * @return string[]
1 ignored issue
show
Documentation introduced by
Should the return type not be array<integer|string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
215
	 */
216 8
	public static function get_multilingual () {
217 8
		return array_values(
218
			array_keys(
219
				array_filter(
220 8
					static::get_formatting(),
221
					function ($option) {
222 8
						return @$option['multilingual'];
223 8
					}
224
				)
225
			)
226
		);
227
	}
228
	/**
229
	 * Take options and check each value according to needed format, correct value or use default if needed
230
	 *
231
	 * @param array $target_options
232
	 *
233
	 * @return array
234
	 */
235 2
	public static function apply_formatting ($target_options) {
236 2
		$options = static::get_formatting();
237 2
		foreach ($target_options as $option => &$value) {
238 2
			if (!isset($options[$option])) {
239
				unset($target_options[$option]);
240
			} else {
241 2
				$format = $options[$option];
242 2
				switch ($format['type']) {
243 2
					case 'array':
244 2
						$value = (array)$value;
245 2
						break;
246 2
					case 'int_bool':
247 2
						$value = (int)(bool)$value;
248 2
						break;
249 2
					case 'int_range':
250 2
						if (isset($format['min'])) {
251 2
							$value = max($format['min'], (int)$value);
252
						}
253 2
						if (isset($format['max'])) {
254 2
							$value = min($format['max'], (int)$value);
255
						}
256 2
						break;
257 2
					case 'set_single':
258 2
						$value = (string)$value;
259 2
						if (!in_array($value, $format['values'], true)) {
260 2
							$value = $format['value'];
261
						}
262 2
						break;
263 2
					case 'set_multiple':
264 2
						$value = array_filter(
265 2
							(array)$value,
266 2
							function ($value) use ($format) {
267 2
								return in_array((string)$value, $format['values'], true);
268 2
							}
269
						);
270 2
						$value = $value ?: [$format['value']];
271 2
						break;
272 2
					case 'text':
273 2
						$value = xap($value);
274 2
						break;
275 2
					case 'html':
276 2
						$value = xap($value, true);
277 2
						break;
278
				}
279
			}
280
		}
281 2
		return $target_options;
282
	}
283
}
284