tfa_module::main()   F
last analyzed

Complexity

Conditions 21
Paths 896

Size

Total Lines 142

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 142
rs 0.1155
c 0
b 0
f 0
cc 21
nc 896
nop 2

How to fix   Long Method    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
 *
4
 * 2FA extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Paul Sohier
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace paul999\tfa\acp;
12
13
use paul999\tfa\helper\session_helper_interface;
14
15
class tfa_module
16
{
17
	/** @var  string */
18
	public $u_action;
19
20
	/** @var array  */
21
	public $new_config = array();
22
23
	/** @var   */
24
	public $page_title;
25
26
	/** @var   */
27
	public $tpl_name;
28
29
	public function main($id, $mode)
30
	{
31
		global $user, $template, $request;
32
		global $config, $phpbb_dispatcher, $phpbb_log;
33
34
		$user->add_lang_ext('paul999/tfa', 'acp_tfa');
35
		$user->add_lang('acp/board');
36
37
		$submit = $request->is_set('submit');
38
39
		$form_key = 'acp_tfa';
40
		add_form_key($form_key);
41
42
		$display_vars = array(
43
			'title'	=> 'ACP_TFA_SETTINGS',
44
			'vars'	=> array(
45
				'legend1'				=> 'ACP_TFA_SETTINGS',
46
				'tfa_mode'				=> array('lang' => 'TFA_MODE', 'validate' => 'int',	'type' => 'select', 'method' => 'select_tfa_method', 'explain' => true),
47
				'tfa_acp'               => array('lang' => 'TFA_ACP',  'validate' => 'int', 'type' => 'radio:no_yes', 'explain' => true),
48
49
				'legend4'				=> 'ACP_SUBMIT_CHANGES',
50
			)
51
		);
52
53
		/**
54
		 * Event to add and/or modify acp_board configurations
55
		 *
56
		 * @event paul999.tfa.tfa_config_edit_add
57
		 * @var	array	display_vars	Array of config values to display and process
58
		 * @var	string	mode			Mode of the config page we are displaying
59
		 * @var	boolean	submit			Do we display the form or process the submission
60
		 * @since 1.0.0-b2
61
		 */
62
		$vars = array('display_vars', 'mode', 'submit');
63
		extract($phpbb_dispatcher->trigger_event('paul999.tfa.tfa_config_edit_add', compact($vars)));
0 ignored issues
show
Bug introduced by
$phpbb_dispatcher->trigg...t_add', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
64
65
		$this->new_config = $config;
66
		// Copied from acp_board.php
67
		$cfg_array = ($request->is_set('config')) ? $request->variable('config', array('' => ''), true) : $this->new_config;
68
		$error = array();
69
70
		// We validate the complete config if wished
71
		validate_config_vars($display_vars['vars'], $cfg_array, $error);
72
73
		if ($submit && !check_form_key($form_key))
74
		{
75
			$error[] = $user->lang('FORM_INVALID');
76
		}
77
		// Do not write values if there is an error
78
		if (sizeof($error))
79
		{
80
			$submit = false;
81
		}
82
83
		// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
84
		foreach ($display_vars['vars'] as $config_name => $null)
85
		{
86
			if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
87
			{
88
				continue;
89
			}
90
91
			$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];
92
93
			if ($submit)
94
			{
95
				$config->set($config_name, $config_value);
96
			}
97
		}
98
99
		if ($submit)
100
		{
101
			$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_TFA_CONFIG_' . strtoupper($mode));
102
103
			$message = $user->lang('CONFIG_UPDATED');
104
			$message_type = E_USER_NOTICE;
105
106
			trigger_error($message . adm_back_link($this->u_action), $message_type);
107
		}
108
109
		if (!$request->is_secure())
110
		{
111
			$error[] = $user->lang('TFA_REQUIRES_SSL');
112
		}
113
114
		$this->tpl_name = 'acp_board';
115
		$this->page_title = $display_vars['title'];
116
117
		$template->assign_vars(array(
118
			'L_TITLE'			=> $user->lang($display_vars['title']),
119
			'L_TITLE_EXPLAIN'	=> $user->lang($display_vars['title'] . '_EXPLAIN'),
120
121
			'S_ERROR'			=> (sizeof($error)) ? true : false,
122
			'ERROR_MSG'			=> implode('<br />', $error),
123
124
			'U_ACTION'			=> $this->u_action,
125
		));
126
127
		// Output relevant page
128
		foreach ($display_vars['vars'] as $config_key => $vars)
129
		{
130
			if (!is_array($vars) && strpos($config_key, 'legend') === false)
131
			{
132
				continue;
133
			}
134
135
			if (strpos($config_key, 'legend') !== false)
136
			{
137
				$template->assign_block_vars('options', array(
138
					'S_LEGEND' => true,
139
					'LEGEND'   => array_key_exists($vars, $user->lang) ? $user->lang($vars) : $vars,
140
				));
141
142
				continue;
143
			}
144
145
			$type = explode(':', $vars['type']);
146
147
			$l_explain = '';
148
			if ($vars['explain'] && array_key_exists($vars['lang'] . '_EXPLAIN', $user->lang))
149
			{
150
				$l_explain =  $user->lang($vars['lang'] . '_EXPLAIN');
151
			}
152
153
			$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);
154
155
			if (empty($content))
156
			{
157
				continue;
158
			}
159
160
			$template->assign_block_vars('options', array(
161
				'KEY'			=> $config_key,
162
				'TITLE'			=> (array_key_exists($vars['lang'], $user->lang)) ? $user->lang($vars['lang']) : $vars['lang'],
163
				'S_EXPLAIN'		=> $vars['explain'],
164
				'TITLE_EXPLAIN'	=> $l_explain,
165
				'CONTENT'		=> $content,
166
			));
167
168
			unset($display_vars['vars'][$config_key]);
169
		}
170
	}
171
172
	/**
173
	 * Select tfa method
174
	 */
175
	public function select_tfa_method($selected_value, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

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

Loading history...
176
	{
177
		global $user;
178
		$act_ary = array(
179
			'TFA_DISABLED'					=> session_helper_interface::MODE_DISABLED,
180
			'TFA_NOT_REQUIRED'				=> session_helper_interface::MODE_NOT_REQUIRED,
181
			'TFA_REQUIRED_FOR_ACP_LOGIN'	=> session_helper_interface::MODE_REQUIRED_FOR_ACP_LOGIN,
182
			'TFA_REQUIRED_FOR_ADMIN'		=> session_helper_interface::MODE_REQUIRED_FOR_ADMIN,
183
			'TFA_REQUIRED_FOR_MODERATOR'	=> session_helper_interface::MODE_REQUIRED_FOR_MODERATOR,
184
			'TFA_REQUIRED'					=> session_helper_interface::MODE_REQUIRED,
185
		);
186
		$act_options = '';
187
		foreach ($act_ary as $key => $data)
188
		{
189
			$selected = ($data == $selected_value) ? ' selected="selected"' : '';
190
			$act_options .= '<option value="' . $data . '"' . $selected . '>' . $user->lang($key) . '</option>';
191
		}
192
		return $act_options;
193
	}
194
}
195