Passed
Pull Request — master (#1)
by Dark❶
03:32
created

acp_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 10
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * Reduce Search Index [RSI]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\reducesearchindex\controller;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\config\config;
17
use phpbb\db\driver\driver_interface;
18
use phpbb\template\template;
19
use phpbb\user;
20
use phpbb\language\language;
21
use phpbb\log\log;
22
use phpbb\request\request;
23
use phpbb\cron\manager as cron_manager;
24
25
/**
26
 * Reduce Search Index [RSI] ACP controller.
27
 */
28
class acp_controller
29
{
30
	/** @var \phpbb\config\config */
31
	protected $config;
32
33
	/** @var \phpbb\language\language */
34
	protected $language;
35
36
	/** @var \phpbb\log\log */
37
	protected $log;
38
39
	/** @var \phpbb\request\request */
40
	protected $request;
41
42
	/** @var \phpbb\template\template */
43
	protected $template;
44
45
	/** @var \phpbb\user */
46
	protected $user;
47
48
	/** @var \phpbb\db\driver\driver_interface */
49
	protected $db;
50
51
	/** @var \phpbb\cron\manager */
52
	protected $cron_manager;
53
54
	/** @var string The module ID */
55
	protected $id;
56
57
	/** @var string The module mode */
58
	protected $mode;
59
60
	/** @var string Custom form action */
61
	protected $u_action;
62
63
	/**
64
	 * Constructor.
65
	 *
66
	 * @param \phpbb\config\config					$config			Config object
67
	 * @param \phpbb\language\language				$language		Language object
68
	 * @param \phpbb\log\log						$log			Log object
69
	 * @param \phpbb\request\request				$request		Request object
70
	 * @param \phpbb\template\template				$template		Template object
71
	 * @param \phpbb\user							$user			User object
72
	 * @param \phpbb\db\driver\driver_interface		$db				Database object
73
	 * @param \phpbb\cron\manager					$cron_manager	Cron manager
74
	 */
75
	public function __construct(config $config, language $language, log $log, request $request, template $template, user $user, driver_interface $db, cron_manager $cron_manager)
76
	{
77
		$this->config		= $config;
78
		$this->language		= $language;
79
		$this->log			= $log;
80
		$this->request		= $request;
81
		$this->template		= $template;
82
		$this->user			= $user;
83
		$this->db			= $db;
84
		$this->cron_manager	= $cron_manager;
85
	}
86
87
	/**
88
	 * Set Data form.
89
	 *
90
	 * @param int		$id			The module ID
91
	 * @param string	$mode		The module mode
92
	 * @param string	$u_action	Custom form action
93
	 *
94
	 * @return void
95
	 */
96
	public function set_data($id, $mode, $u_action)
97
	{
98
		$this->id = $id;
99
		$this->mode = strtolower($mode);
100
		$this->u_action = $u_action;
101
	}
102
103
	/**
104
	 * Get Data form.
105
	 *
106
	 * @return void
107
	 */
108
	public function get_data()
109
	{
110
		return [
111
			'tpl_name' => 'dark1_rsi_acp_' . $this->mode,
112
			'page_title' => $this->language->lang('ACP_RSI_TITLE') . ' - ' . $this->language->lang('ACP_RSI_' . strtoupper($this->mode)),
113
		];
114
	}
115
116
	/**
117
	 * Set Display form.
118
	 *
119
	 * @return void
120
	 */
121
	public function display()
122
	{
123
		$ext_name_rsi = 'Reduce Search Index [RSI]';
124
		$ext_by_dark1 = 'Dark❶ [dark1]';
125
126
		// Add our common language file
127
		$this->language->add_lang('lang_rsi', 'dark1/reducesearchindex');
128
129
		// Create a form key for preventing CSRF attacks
130
		add_form_key('dark1_rsi_acp_' . $this->mode);
131
132
		// Set u_action in the template
133
		$this->template->assign_vars([
134
			'U_ACTION'		=> $this->u_action,
135
			'RSI_MODE'		=> $this->mode,
136
			'RSI_EXT_MODE'	=> $this->language->lang('ACP_RSI_' . strtoupper($this->mode)),
137
			'RSI_EXT_NAME'	=> $ext_name_rsi,
138
			'RSI_EXT_DEV'	=> $ext_by_dark1,
139
		]);
140
141
		$mode_display = 'mode_' . $this->mode;
142
143
		if (!method_exists($this, $mode_display))
144
		{
145
			trigger_error('FORM_INVALID', E_USER_WARNING);
146
		}
147
148
		// Trigger the Mode
149
		$this->$mode_display();
150
	}
151
152
	/**
153
	 * Check Form On Submit .
154
	 *
155
	 * @return void
156
	 */
157
	private function check_form_on_submit()
158
	{
159
		// Test if the submitted form is valid
160
		if (!check_form_key('dark1_rsi_acp_' . $this->mode))
161
		{
162
			trigger_error('FORM_INVALID', E_USER_WARNING);
163
		}
164
	}
165
166
	/**
167
	 * Success Form On Submit.
168
	 * Used to Log & Trigger Success Err0r.
169
	 *
170
	 * @return void
171
	 */
172
	private function success_form_on_submit()
173
	{
174
		// Add option settings change action to the admin log
175
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_RSI_LOG_SET_SAV', time(), array($this->language->lang('ACP_RSI_' . strtoupper($this->mode))));
176
177
		// Option settings have been updated and logged
178
		// Confirm this to the user and provide link back to previous page
179
		trigger_error($this->language->lang('ACP_RSI_LOG_SET_SAV', $this->language->lang('ACP_RSI_' . strtoupper($this->mode))) . adm_back_link($this->u_action), E_USER_NOTICE);
180
	}
181
182
	/**
183
	 * Display the options a user can configure for Main Mode.
184
	 *
185
	 * @return void
186
	 */
187
	private function mode_main()
188
	{
189
		// Is the form being submitted to us?
190
		if ($this->request->is_set_post('submit'))
191
		{
192
			$this->check_form_on_submit();
193
194
			// Set the options the user configured
195
			$this->config->set('dark1_rsi_enable', $this->request->variable('dark1_rsi_enable', 0));
196
			$this->config->set('dark1_rsi_time', strtotime($this->request->variable('dark1_rsi_time', '0', true)));
197
			$this->config->set('dark1_rsi_interval', ($this->request->variable('dark1_rsi_interval', 0)) * 86400);
198
199
			$this->success_form_on_submit();
200
		}
201
202
		// Set output variables for display in the template
203
		$this->template->assign_vars([
204
			'RSI_ENABLE'		=> $this->config['dark1_rsi_enable'],
205
			'RSI_INTERVAL'		=> ($this->config['dark1_rsi_interval'] / 86400),
206
			'RSI_TIME'			=> $this->user->format_date($this->config['dark1_rsi_time'], 'Y-m-d h:i:s A P', true),
207
			'RSI_CURR_TIME'		=> $this->user->format_date(time(), 'Y-m-d h:i:s A P', true),
208
		]);
209
	}
210
211
	/**
212
	 * Display the options a user can configure for Forum Mode.
213
	 *
214
	 * @return void
215
	 */
216
	private function mode_forum()
217
	{
218
		// Is the form being submitted to us?
219
		if ($this->request->is_set_post('submit'))
220
		{
221
			$this->check_form_on_submit();
222
223
			// Set the options the user configured
224
			$sql = 'SELECT forum_id, forum_type FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC';
225
			$result = $this->db->sql_query($sql);
226
			$forum_id_set = array();
227
			while ($row = $this->db->sql_fetchrow($result))
228
			{
229
				if ($row['forum_type'] == FORUM_POST)
230
				{
231
					$forum_id_set[$row['forum_id']] =  $this->request->variable('forum_' . $row['forum_id'] . '_enable', 0);
232
				}
233
			}
234
			$this->db->sql_freeresult($result);
235
			foreach ($forum_id_set as $id => $input)
236
			{
237
				$sql = 'UPDATE ' . FORUMS_TABLE . ' SET dark1_rsi_f_enable = ' . (int) $input . ' WHERE forum_id = ' . (int) $id;
238
				$this->db->sql_query($sql);
239
			}
240
241
			$this->success_form_on_submit();
242
		}
243
244
		// Set output variables for display in the template
245
		$sql = 'SELECT forum_id, forum_type, forum_name, parent_id, left_id, right_id, dark1_rsi_f_enable FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC';
246
		$result = $this->db->sql_query($sql);
247
248
		$right = 0;
249
		$padding_store = array('0' => '');
250
		$padding = '';
251
252
		while ($row = $this->db->sql_fetchrow($result))
253
		{
254
			if ($row['left_id'] < $right)
255
			{
256
				$padding .= '&nbsp; &nbsp; &nbsp;';
257
				$padding_store[$row['parent_id']] = $padding;
258
			}
259
			else if ($row['left_id'] > $right + 1)
260
			{
261
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
262
			}
263
			$right = $row['right_id'];
264
265
			// Category forums are displayed for organizational purposes, but have no configuration
266
			if ($row['forum_type'] == FORUM_CAT)
267
			{
268
				$tpl_row = array(
269
					'S_IS_CAT'		=> true,
270
					'FORUM_NAME'	=> $padding . '&nbsp; &#8627; &nbsp;' . $row['forum_name'],
271
				);
272
				$this->template->assign_block_vars('forumrow', $tpl_row);
273
			}
274
			// Normal forums have a radio input with the value selected based on the value of the discord_notifications_enabled setting
275
			else if ($row['forum_type'] == FORUM_POST)
276
			{
277
				// The labels for all the inputs are constructed based on the forum IDs to make it easy to know which
278
				$tpl_row = array(
279
					'S_IS_CAT'		=> false,
280
					'FORUM_NAME'	=> $padding . '&nbsp; &#8627; &nbsp;' . $row['forum_name'],
281
					'FORUM_ID'		=> $row['forum_id'],
282
					'ENABLE'		=> $row['dark1_rsi_f_enable'],
283
				);
284
				$this->template->assign_block_vars('forumrow', $tpl_row);
285
			}
286
			// Other forum types (links) are ignored
287
		}
288
		$this->db->sql_freeresult($result);
289
	}
290
291
	/**
292
	 * Display the options a user can configure for Cron Mode.
293
	 *
294
	 * @return void
295
	 */
296
	private function mode_cron()
297
	{
298
		// Is the form being submitted to us?
299
		if ($this->request->is_set_post('submit'))
300
		{
301
			$this->check_form_on_submit();
302
303
			// Set the options the user configured
304
			$this->config->set('dark1_rsi_auto_reduce_sync_enable', $this->request->variable('dark1_rsi_auto_reduce_sync_enable', 0));
305
			$this->config->set('dark1_rsi_auto_reduce_sync_gc', ($this->request->variable('dark1_rsi_auto_reduce_sync_gc', 0)) * 86400);
306
			$this->config->set('dark1_rsi_auto_reduce_sync_last_gc', strtotime($this->request->variable('dark1_rsi_auto_reduce_sync_last_gc', '0', true)), false);
307
308
			$this->success_form_on_submit();
309
		}
310
311
		// Run Cron Task
312
		if ($this->request->is_set_post('runcrontask'))
313
		{
314
			$this->check_form_on_submit();
315
316
			$cron_task = $this->cron_manager->find_task('dark1.reducesearchindex.cron.auto_reduce_sync');
317
			$cron_task->run();
318
			$this->template->assign_var('DONE_RUN_LS_CRON', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $varval of phpbb\template\template::assign_var(). ( Ignorable by Annotation )

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

318
			$this->template->assign_var('DONE_RUN_LS_CRON', /** @scrutinizer ignore-type */ true);
Loading history...
319
		}
320
321
		// Set output variables for display in the template
322
		$this->template->assign_vars([
323
			'ENABLE_CRON'		=> $this->config['dark1_rsi_auto_reduce_sync_enable'],
324
			'CRON_INTERVAL'		=> ($this->config['dark1_rsi_auto_reduce_sync_gc'] / 86400),
325
			'CRON_LAST_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'], 'Y-m-d h:i:s A P', true),
326
			'CRON_NEXT_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'] + $this->config['dark1_rsi_auto_reduce_sync_gc'], 'Y-m-d h:i:s A P', true),
327
			'CRON_PREV_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'] - $this->config['dark1_rsi_auto_reduce_sync_gc'], 'Y-m-d h:i:s A P', true),
328
		]);
329
	}
330
}
331