settings::process()   C
last analyzed

Complexity

Conditions 13
Paths 192

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 5.85
c 0
b 0
f 0
cc 13
nc 192
nop 0

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
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\controller\acp;
12
13
class settings
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\language\language */
19
	protected $language;
20
21
	/** @var \phpbb\log\log */
22
	protected $log;
23
24
	/** @var \phpbb\request\request */
25
	protected $request;
26
27
	/** @var \phpbb\template\template */
28
	protected $template;
29
30
	/** @var \phpbb\user */
31
	protected $user;
32
33
	/** @var string Custom form action */
34
	protected $u_action;
35
36
	/** @var array */
37
	private $new_config;
38
39
	/** @var array */
40
	private $display_vars;
41
42
	/**
43
	* Constructor
44
	*
45
	* @param \phpbb\config\config		$config		Config object
46
	* @param \phpbb\language\language	$language	Language object
47
	* @param \phpbb\log\log				$log		Log object
48
	* @param \phpbb\request\request		$request	Request object
49
	* @param \phpbb\template\template	$template	Template object
50
	* @param \phpbb\user				$user		User object
51
	*/
52
	public function __construct(\phpbb\config\config $config, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
53
	{
54
		$this->config	= $config;
55
		$this->language	= $language;
56
		$this->log		= $log;
57
		$this->template	= $template;
58
		$this->user		= $user;
59
		$this->request	= $request;
60
61
		$this->_generate_config();
62
	}
63
64
	/**
65
	* Display config page
66
	*
67
	* @return null
68
	*/
69
	public function display_config()
70
	{
71
		// Output relevant page
72
		foreach ($this->display_vars['vars'] as $config_key => $vars)
73
		{
74
			if (!is_array($vars) && strpos($config_key, 'legend') === false)
75
			{
76
				continue;
77
			}
78
79
			if (strpos($config_key, 'legend') !== false)
80
			{
81
				$this->template->assign_block_vars('options', array(
82
					'S_LEGEND'	=> true,
83
					'LEGEND'	=> $this->language->lang($vars))
84
				);
85
86
				continue;
87
			}
88
89
			$type = explode(':', $vars['type']);
90
91
			$l_explain = '';
92
			if ($vars['explain'] && isset($vars['lang_explain']))
93
			{
94
				$l_explain = $this->language->lang($vars['lang_explain']);
95
			}
96
			else if ($vars['explain'])
97
			{
98
				$l_explain = $this->language->lang($vars['lang'] . '_EXPLAIN');
99
			}
100
101
			$this->template->assign_block_vars('options', array(
102
				'KEY'			=> $config_key,
103
				'TITLE'			=> $this->language->lang($vars['lang']),
104
				'S_EXPLAIN'		=> $vars['explain'],
105
				'TITLE_EXPLAIN'	=> $l_explain,
106
				'CONTENT'		=> build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars),
107
			));
108
109
			unset($this->display_vars['vars'][$config_key]);
110
		}
111
	}
112
113
	/**
114
	* Validate config vars and update config table if needed
115
	*
116
	* @return null
117
	*/
118
	public function process()
119
	{
120
		$submit	= ($this->request->is_set_post('submit')) ? true : false;
121
122
		$this->new_config = $this->config;
123
		$cfg_array = ($this->request->is_set('config')) ? $this->request->variable('config', array('' => ''), true) : $this->new_config;
124
		$error = array();
125
126
		// We validate the complete config if whished
127
		validate_config_vars($this->display_vars['vars'], $cfg_array, $error);
128
129
		// Do not write values if there is an error
130
		if (count($error))
131
		{
132
			$submit = false;
133
		}
134
135
		// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
136
		foreach ($this->display_vars['vars'] as $config_name => $null)
137
		{
138
			if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
139
			{
140
				continue;
141
			}
142
143
			$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];
144
145
			if ($config_name == 'dir_banner_filesize')
146
			{
147
				$size_var = $this->request->variable($config_name, '');
148
				$this->new_config[$config_name] = $config_value = ($size_var == 'kb') ? round($config_value * 1024) : (($size_var == 'mb') ? round($config_value * 1048576) : $config_value);
149
			}
150
151
			if ($submit)
152
			{
153
				$this->config->set($config_name, $config_value);
154
			}
155
		}
156
157
		if ($submit)
158
		{
159
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'DIR_CONFIG_SETTINGS');
160
161
			trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
162
		}
163
164
		$this->template->assign_vars(array(
165
			'L_TITLE'			=> $this->language->lang($this->display_vars['title']),
166
			'L_TITLE_EXPLAIN'	=> $this->language->lang($this->display_vars['title'] . '_EXPLAIN'),
167
168
			'S_ERROR'			=> (count($error)) ? true : false,
169
			'ERROR_MSG'			=> implode('<br />', $error),
170
171
			'U_ACTION'			=> $this->u_action)
172
		);
173
	}
174
175
	/**
176
	* Set page url
177
	*
178
	* @param	string $u_action Custom form action
179
	* @return	null
180
	* @access	public
181
	*/
182
	public function set_page_url($u_action)
183
	{
184
		$this->u_action = $u_action;
185
	}
186
187
	/**
188
	* Build config matrice
189
	*
190
	* @return null
191
	*/
192
	private function _generate_config()
193
	{
194
		$this->display_vars = array(
195
			'title'	=> 'ACP_DIRECTORY_SETTINGS',
196
			'vars'	=> array(
197
				'legend1' => 'DIR_PARAM',
198
199
				'dir_banner_width'					=> '',
200
				'dir_banner_height'					=> '',
201
202
				'dir_mail'							=> array('lang' => 'DIR_MAIL_VALIDATION',	'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => false),
203
				'dir_activ_checkurl'				=> array('lang' => 'DIR_ACTIVE_CHECK',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true),
204
				'dir_activ_flag'					=> array('lang' => 'DIR_ACTIV_FLAG',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => false),
205
				'dir_activ_rss'						=> array('lang' => 'DIR_ACTIV_RSS',			'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true),
206
				'dir_show'							=> array('lang' => 'DIR_SHOW',				'validate' => 'int:1:9999', 'type' => 'number:1:9999',	'explain' => false),
207
				'dir_length_describe'				=> array('lang' => 'DIR_MAX_DESC',			'validate' => 'int:1:999',	'type' => 'number:1:999',	'explain' => false),
208
				'dir_new_time'						=> array('lang' => 'DIR_NEW_TIME',			'validate' => 'int:1:999', 	'type' => 'number:1:999',	'explain' => true),
209
				'dir_default_order'					=> array('lang' => 'DIR_DEFAULT_ORDER',		'validate' => 'string', 	'type' => 'select',			'explain' => false, 'method' => 'get_order_list', 'params' => array('{CONFIG_VALUE}')),
210
211
				'legend2'							=> 'DIR_RECENT_GUEST',
212
				'dir_recent_block'					=> array('lang' => 'DIR_RECENT_ENABLE',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true),
213
				'dir_recent_rows'					=> array('lang' => 'DIR_RECENT_ROWS',		'validate' => 'int:1:999',	'type' => 'number:1:999',	'explain' => false),
214
				'dir_recent_columns'				=> array('lang' => 'DIR_RECENT_COLUMNS',	'validate' => 'int:1:999',	'type' => 'number:1:999',	'explain' => false),
215
				'dir_recent_exclude'				=> array('lang' => 'DIR_RECENT_EXCLUDE',	'validate' => 'string',		'type' => 'text:6:99',		'explain' => true),
216
217
				'legend3'							=> 'DIR_ADD_GUEST',
218
				'dir_visual_confirm'				=> array('lang' => 'DIR_VISUAL_CONFIRM',	'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true),
219
				'dir_visual_confirm_max_attempts'	=> array('lang' => 'DIR_MAX_ADD_ATTEMPTS',	'validate' => 'int:0:9999',	'type' => 'number:0:9999',	'explain' => true),
220
221
				'legend4'							=> 'DIR_THUMB_PARAM',
222
				'dir_activ_thumb'					=> array('lang' => 'DIR_ACTIVE_THUMB',			'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => false),
223
				'dir_activ_thumb_remote'			=> array('lang' => 'DIR_ACTIVE_THUMB_REMOTE',	'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
224
				'dir_thumb_service'					=> array('lang' => 'DIR_THUMB_SERVICE',			'validate' => 'string', 'type' => 'select',			'explain' => true, 'method' => 'get_thumb_service_list', 'params' => array('{CONFIG_VALUE}')),
225
				'dir_thumb_service_reverse'			=> array('lang' => 'DIR_THUMB_SERVICE_REVERSE',	'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
226
227
				'legend5'							=> 'DIR_COMM_PARAM',
228
				'dir_allow_bbcode'					=> array('lang' => 'DIR_ALLOW_BBCODE',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => false),
229
				'dir_allow_flash'					=> array('lang' => 'DIR_ALLOW_FLASH',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => false),
230
				'dir_allow_links'					=> array('lang' => 'DIR_ALLOW_LINKS',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => false),
231
				'dir_allow_smilies'					=> array('lang' => 'DIR_ALLOW_SMILIES',		'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => false),
232
				'dir_length_comments'				=> array('lang' => 'DIR_LENGTH_COMMENTS',	'validate' => 'int:1:999',	'type' => 'number:1:999',	'explain' => true),
233
				'dir_comments_per_page'				=> array('lang' => 'DIR_COMM_PER_PAGE',		'validate' => 'int:1:9999',	'type' => 'number:1:9999',	'explain' => false),
234
235
				'legend6'							=> 'DIR_BANN_PARAM',
236
				'dir_activ_banner'					=> array('lang' => 'DIR_ACTIV_BANNER',		'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => false),
237
				'dir_banner'						=> array('lang' => 'DIR_MAX_BANN',			'validate' => 'int:0',	'type' => 'dimension:0',	'explain' => true, 'append' => ' ' . $this->user->lang['PIXEL']),
238
				'dir_banner_filesize'				=> array('lang' => 'DIR_MAX_SIZE',			'validate' => 'string',	'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
239
				'dir_storage_banner'				=> array('lang' => 'DIR_STORAGE_BANNER',	'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
240
			)
241
		);
242
	}
243
}
244