Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

_elgg_config_test()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg configuration procedural code.
4
 *
5
 * Includes functions for manipulating the configuration values stored in the database
6
 * Plugin authors should use the {@link elgg_get_config()}, {@link elgg_set_config()},
7
 * {@link elgg_save_config()}, and {@elgg_remove_config()} functions to access or update
8
 * config values.
9
 *
10
 * Elgg's configuration is split among 1 table and 1 file:
11
 * - dbprefix_config
12
 * - engine/settings.php (See {@link settings.example.php})
13
 *
14
 * Upon system boot, all values in dbprefix_config are read into $CONFIG.
15
 */
16
17
use Elgg\Project\Paths;
18
19
/**
20
 * Get the URL for the current (or specified) site, ending with "/".
21
 *
22
 * @return string
23
 * @since 1.8.0
24
 */
25
function elgg_get_site_url() {
26 308
	return _elgg_config()->wwwroot;
27
}
28
29
/**
30
 * Get the plugin path for this installation, ending with slash.
31
 *
32
 * @return string
33
 * @since 1.8.0
34
 */
35
function elgg_get_plugins_path() {
36 2301
	$path = _elgg_config()->plugins_path;
37 2301
	if (!$path) {
38 5
		$path = Paths::project() . 'mod/';
39
	}
40 2301
	return $path;
41
}
42
43
/**
44
 * Get the data directory path for this installation, ending with slash.
45
 *
46
 * @return string
47
 * @since 1.8.0
48
 */
49
function elgg_get_data_path() {
50 1
	return _elgg_config()->dataroot;
51
}
52
53
/**
54
 * Get the cache directory path for this installation, ending with slash.
55
 *
56
 * If not set in settings, the data path will be returned.
57
 *
58
 * @return string
59
 */
60
function elgg_get_cache_path() {
61 2
	return _elgg_config()->cacheroot;
62
}
63
64
/**
65
 * Get the project path (where composer is installed), ending with slash.
66
 *
67
 * Note: This is not the same as the Elgg root! In the Elgg 1.x series, Elgg
68
 * was always at the install root, but as of 2.0, Elgg can be installed as a
69
 * composer dependency, so you cannot assume that it the install root anymore.
70
 *
71
 * @return string
72
 * @since 1.8.0
73
 */
74
function elgg_get_root_path() {
75 3
	return Paths::project();
76
}
77
78
/**
79
 * /path/to/elgg/engine with no trailing slash.
80
 *
81
 * @return string
82
 */
83
function elgg_get_engine_path() {
84 1
	return Paths::elgg() . 'engine';
85
}
86
87
/**
88
 * Get an Elgg configuration value
89
 *
90
 * @param string $name    Name of the configuration value
91
 * @param mixed  $default (optional) default value if configuration value is not set
92
 *
93
 * @return mixed Configuration value or the default value if it does not exist
94
 * @since 1.8.0
95
 */
96
function elgg_get_config($name, $default = null) {
97 1919
	if ($name == 'icon_sizes') {
98
		$msg = 'The config value "icon_sizes" is deprecated. Use elgg_get_icon_sizes()';
99
		elgg_deprecated_notice($msg, '2.2');
100
	}
101
102 1919
	if (!_elgg_config()->hasValue($name)) {
103 705
		elgg_log("Config value for '$name' is not set'", 'INFO');
104 705
		return $default;
105
	}
106
107 1308
	return _elgg_config()->$name;
108
}
109
110
/**
111
 * Set an Elgg configuration value
112
 *
113
 * @warning This does not persist the configuration setting. Use elgg_save_config()
114
 *
115
 * @param string $name  Name of the configuration value
116
 * @param mixed  $value Value
117
 *
118
 * @return void
119
 * @since 1.8.0
120
 */
121
function elgg_set_config($name, $value) {
122 37
	_elgg_config()->$name = $value;
123 37
}
124
125
/**
126
 * Save a configuration setting
127
 *
128
 * @param string $name  Configuration name (cannot be greater than 255 characters)
129
 * @param mixed  $value Configuration value. Should be string for installation setting
130
 *
131
 * @return bool
132
 * @since 1.8.0
133
 */
134
function elgg_save_config($name, $value) {
135 8
	return _elgg_config()->save($name, $value);
136
}
137
138
/**
139
 * Removes a config setting.
140
 *
141
 * @param string $name The name of the field.
142
 *
143
 * @return bool Success or failure
144
 */
145
function elgg_remove_config($name) {
146 7
	return _elgg_config()->remove($name);
147
}
148
149
/**
150
 * Get the Elgg config service
151
 *
152
 * @return \Elgg\Config
153
 * @access private
154
 */
155
function _elgg_config() {
156 5390
	$config = elgg()->_services->config;
157 5390
	if (!$config) {
158
		throw new \RuntimeException(__FUNCTION__ . ' can not be called before an instance of ' . \Elgg\Application::class . ' is bootstrapped');
159
	}
160
161 5390
	return $config;
162
}
163
164
/**
165
 * Register unit tests
166
 *
167
 * @param string $hook  'unit_test'
168
 * @param string $type  'system'
169
 * @param array  $tests current return value
170
 *
171
 * @return array
172
 *
173
 * @access private
174
 * @codeCoverageIgnore
175
 */
176
function _elgg_config_test($hook, $type, $tests) {
2 ignored issues
show
Unused Code introduced by Cash Costello
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

176
function _elgg_config_test($hook, /** @scrutinizer ignore-unused */ $type, $tests) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

176
function _elgg_config_test(/** @scrutinizer ignore-unused */ $hook, $type, $tests) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
	$tests[] = ElggCoreConfigTest::class;
178
	return $tests;
179
}
180
181
/**
182
 * Returns a configuration array of icon sizes
183
 *
184
 * @param string $entity_type    Entity type
185
 * @param string $entity_subtype Entity subtype
186
 * @param string $type           The name of the icon. e.g., 'icon', 'cover_photo'
187
 * @return array
188
 */
189
function elgg_get_icon_sizes($entity_type = null, $entity_subtype = null, $type = 'icon') {
190 15
	return _elgg_services()->iconService->getSizes($entity_type, $entity_subtype, $type);
191
}
192
193
/**
194
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
195
 */
196
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
197 18
	$hooks->registerHandler('unit_test', 'system', '_elgg_config_test');
198
};
199