Test Failed
Branch master (d625c8)
by Joe
04:55
created

has_module_db()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Pre-Config Functions.   This stuff is called almost instantly
4
 * so it needs to be defined before any of the rest of the program can go.
5
 * @author Joe Huss <[email protected]>
6
 * @copyright 2017
7
 * @package MyAdmin
8
 * @category Config
9
 */
10
11
/**
12
 * register_module()
13
 * @param string     $module
14
 * @param array|bool $settings
15
 * @return void
16
 */
17
function register_module($module, $settings = false) {
18
	if ($settings === false)
19
		$settings = [];
20
	if (!isset($GLOBALS['modules']))
21
		$GLOBALS['modules'] = [];
22
	$modules = $GLOBALS['modules'];
23
	$modules[$module] = $settings;
24
	$GLOBALS['modules'] = $modules;
25
}
26
27
/**
28
 * get_module_stuff()
29
 * @param string $module
30
 * @return array
31
 */
32
function get_module_stuff($module = 'default') {
33
	$module = get_module_name($module);
34
	return [
35
		get_module_db($module),
36
		$module,
37
		get_module_settings($module)
38
	];
39
}
40
41
/**
42
 * get_module_name()
43
 * gets the name of a module, or makes sure that the given module exists, if not returns default
44
 *
45
 * @param string $module the module name your attempting to validate / get the name of
46
 * @return string the name of the module
47
 */
48
function get_module_name($module = 'default') {
49
	if ($module != 'default') {
50
		if (isset($GLOBALS[$module.'_dbh']))
51
			return $module;
52
		if (isset($GLOBALS['modules'][$module]))
53
			return $module;
54
		elseif (isset($_REQUEST['module']) && isset($GLOBALS['modules'][$_REQUEST['module']]))
55
			return $_REQUEST['module'];
56
	}
57
	$tkeys = array_keys($GLOBALS['modules']);
58
	if (count($tkeys) > 0)
59
		foreach ($tkeys as $idx => $key)
60
			if ($key != 'default')
61
				return $key;
62
	return 'default';
63
}
64
65
/**
66
 * get_module_settings()
67
 * gets the array of settings for a given module, or a specific setting for that module
68
 *
69
 * @param string $module
70
 * @param bool|string $setting optional parameter, false to return all settings, or a specific setting name to return that setting
71
 * @return array|false array of settings or false if no setting
72
 */
73
function get_module_settings($module = 'default', $setting = false) {
74
	if (!isset($GLOBALS['modules'][$module])) {
75
		$keys = array_keys($GLOBALS['modules']);
76
		$module = $keys[0];
77
	}
78
	if ($setting !== false) {
79
		if (isset($GLOBALS['modules'][$module][$setting]))
80
			return $GLOBALS['modules'][$module][$setting];
81
		else
82
			return false;
83
	} else
84
		return $GLOBALS['modules'][$module];
85
}
86
87
/**
88
 * @param $service
89
 * @return mixed
90
 */
91
function get_service_define($service) {
92
	return $GLOBALS['tf']->get_service_define($service);
93
}
94
95
/**
96
 * @param $module
97
 * @return bool
98
 */
99
function has_module_db($module) {
100
	return isset($GLOBALS[$module.'_dbh']);
101
}
102
103
/**
104
 * gets the database handler for a given module
105
 *
106
 * @param string $module the name of the module to get the dbh for
107
 * @return Db the database handler resource
0 ignored issues
show
Bug introduced by
The type Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
108
 */
109
function get_module_db($module) {
110
	if ($module == 'powerdns') {
111
		if (!isset($GLOBALS['powerdns_dbh'])) {
112
			$GLOBALS['powerdns_dbh'] = new \MyDb\Mdb2\Db(POWERDNS_DB, POWERDNS_USER, POWERDNS_PASSWORD, POWERDNS_HOST);
0 ignored issues
show
Bug introduced by
The constant POWERDNS_USER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant POWERDNS_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant POWERDNS_DB was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant POWERDNS_PASSWORD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The type MyDb\Mdb2\Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
113
			$GLOBALS['powerdns_dbh']->Type = 'mysqli';
114
		}
115
		return clone $GLOBALS['powerdns_dbh'];
116
	} else {
117
		if (isset($GLOBALS[$module.'_dbh']))
118
			return clone $GLOBALS[$module.'_dbh'];
119
		else {
120
			if ($module != '' && $module != 'default')
121
				myadmin_log('myadmin', 'info', "Tried to get_module_db(${module}) and GLOBALS[${module}_dbh] does not exist, falling back to GLOBALS['tf']->db", __LINE__, __FILE__);
0 ignored issues
show
Bug introduced by
The function myadmin_log was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

121
				/** @scrutinizer ignore-call */ 
122
    myadmin_log('myadmin', 'info', "Tried to get_module_db(${module}) and GLOBALS[${module}_dbh] does not exist, falling back to GLOBALS['tf']->db", __LINE__, __FILE__);
Loading history...
122
			return clone $GLOBALS['tf']->db;
123
		}
124
	}
125
}
126
127
/**
128
 * get_valid_module()
129
 * returns the module name if a valid module or default if not
130
 *
131
 * @param string $module
132
 * @return string the/a validated module name
133
 */
134
function get_valid_module($module = 'default') {
135
	if (isset($GLOBALS['modules'][$module]))
136
		return $module;
137
	else
138
		return 'default';
139
}
140