Passed
Pull Request — master (#34)
by Matt
07:28
created

similar_topics_admin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 12
dl 0
loc 20
rs 9.6666

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
 * Precise Similar Topics
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\similartopics\acp\controller;
12
13
use phpbb\cache\driver\driver_interface as cache;
0 ignored issues
show
Bug introduced by
The type phpbb\cache\driver\driver_interface 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...
14
use phpbb\config\config;
0 ignored issues
show
Bug introduced by
The type phpbb\config\config 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...
15
use phpbb\config\db_text;
0 ignored issues
show
Bug introduced by
The type phpbb\config\db_text 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...
16
use phpbb\db\driver\driver_interface as dbal;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface 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...
17
use phpbb\language\language;
0 ignored issues
show
Bug introduced by
The type phpbb\language\language 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...
18
use phpbb\log\log;
0 ignored issues
show
Bug introduced by
The type phpbb\log\log 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...
19
use phpbb\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request 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...
20
use phpbb\template\template;
0 ignored issues
show
Bug introduced by
The type phpbb\template\template 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...
21
use phpbb\user;
0 ignored issues
show
Bug introduced by
The type phpbb\user 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...
22
use vse\similartopics\driver\manager;
23
use vse\similartopics\driver\driver_interface as similartopics;
24
25
class similar_topics_admin
26
{
27
	/** @var cache */
28
	protected $cache;
29
30
	/** @var config */
31
	protected $config;
32
33
	/** @var db_text */
34
	protected $config_text;
35
36
	/** @var dbal */
37
	protected $db;
38
39
	/** @var language */
40
	protected $language;
41
42
	/** @var log */
43
	protected $log;
44
45
	/** @var request */
46
	protected $request;
47
48
	/** @var similartopics */
49
	protected $similartopics;
50
51
	/** @var template */
52
	protected $template;
53
54
	/** @var user */
55
	protected $user;
56
57
	/** @var string */
58
	protected $root_path;
59
60
	/** @var string */
61
	protected $php_ext;
62
63
	/** @var array */
64
	protected $times;
65
66
	/** @var string */
67
	protected $form_key;
68
69
	/** @var string */
70
	public $u_action;
71
72
	/**
73
	 * Admin controller constructor
74
	 *
75
	 * @access public
76
	 * @param cache    $cache
77
	 * @param config   $config
78
	 * @param db_text  $config_text
79
	 * @param dbal     $db
80
	 * @param manager  $similartopics
81
	 * @param language $language
82
	 * @param log      $log
83
	 * @param request  $request
84
	 * @param template $template
85
	 * @param user     $user
86
	 * @param string   $root_path
87
	 * @param string   $php_ext
88
	 */
89
	public function __construct(cache $cache, config $config, db_text $config_text, dbal $db, manager $similartopics, language $language, log $log, request $request, template $template, user $user, $root_path, $php_ext)
90
	{
91
		$this->cache         = $cache;
92
		$this->config        = $config;
93
		$this->config_text   = $config_text;
94
		$this->db            = $db;
95
		$this->similartopics = $similartopics->get_driver($this->db->get_sql_layer());
96
		$this->language      = $language;
97
		$this->log           = $log;
98
		$this->request       = $request;
99
		$this->template      = $template;
100
		$this->user          = $user;
101
		$this->root_path     = $root_path;
102
		$this->php_ext       = $php_ext;
103
		$this->form_key      = 'acp_similar_topics';
104
		$this->times         = [
105
			'd' => 86400, // one day
106
			'w' => 604800, // one week
107
			'm' => 2626560, // one month
108
			'y' => 31536000, // one year
109
		];
110
	}
111
112
	/**
113
	 * Set the u_action variable from the form/module
114
	 *
115
	 * @access public
116
	 * @param string $u_action
117
	 *
118
	 * @return similar_topics_admin $this
119
	 */
120
	public function set_u_action($u_action)
121
	{
122
		$this->u_action = $u_action;
123
		return $this;
124
	}
125
126
	/**
127
	 * Controller handler. Call this method from the ACP module.
128
	 *
129
	 * @access public
130
	 */
131
	public function handle()
132
	{
133
		$this->language->add_lang('acp_similar_topics', 'vse/similartopics');
134
135
		add_form_key($this->form_key);
0 ignored issues
show
Bug introduced by
The function add_form_key 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

135
		/** @scrutinizer ignore-call */ 
136
  add_form_key($this->form_key);
Loading history...
136
137
		if ($this->request->variable('action', '') === 'advanced')
138
		{
139
			$this->advanced_settings();
140
		}
141
		else
142
		{
143
			$this->default_settings();
144
		}
145
	}
146
147
	/**
148
	 * Display/Save default settings
149
	 *
150
	 * @access protected
151
	 */
152
	protected function default_settings()
153
	{
154
		if ($this->request->is_set_post('submit'))
155
		{
156
			$this->check_form_key($this->form_key);
157
158
			// Set basic config settings
159
			$this->config->set('similar_topics', $this->request->variable('pst_enable', 0));
160
			$this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only
161
			$this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only
162
			$this->config_text_set('similar_topics_words', $this->request->variable('pst_words', '', true));
163
164
			// Set sensitivity
165
			$pst_sense = min(abs($this->request->variable('pst_sense', 5)), 10); // use abs for positive values only
166
			$this->config->set('similar_topics_sense', $pst_sense);
167
168
			// Set date/time config settings
169
			$pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only
170
			$pst_time_type = $this->request->variable('pst_time_type', '');
171
			$this->config->set('similar_topics_type', $pst_time_type);
172
			$this->config->set('similar_topics_time', $this->set_pst_time($pst_time, $pst_time_type));
0 ignored issues
show
Bug introduced by
It seems like $pst_time can also be of type double; however, parameter $length of vse\similartopics\acp\co...s_admin::set_pst_time() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

172
			$this->config->set('similar_topics_time', $this->set_pst_time(/** @scrutinizer ignore-type */ $pst_time, $pst_time_type));
Loading history...
173
174
			// Set checkbox array form data
175
			$this->update_forum('similar_topics_hide', $this->request->variable('mark_noshow_forum', array(0), true));
176
			$this->update_forum('similar_topics_ignore', $this->request->variable('mark_ignore_forum', array(0), true));
177
178
			// Set PostgreSQL TS Name
179
			if ($this->similartopics && $this->similartopics->get_type() === 'postgres')
180
			{
181
				$ts_name = $this->request->variable('pst_postgres_ts_name', ($this->config['pst_postgres_ts_name'] ?: 'simple'));
182
				$this->config->set('pst_postgres_ts_name', $ts_name);
183
				$this->similartopics->create_fulltext_index('topic_title');
184
			}
185
186
			$this->cache->destroy('sql', TOPICS_TABLE);
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\controller\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
187
188
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG');
189
190
			$this->end('PST_SAVED');
191
		}
192
193
		// Build the time options select menu
194
		$time_options = array(
195
			'd' => 'PST_DAYS',
196
			'w' => 'PST_WEEKS',
197
			'm' => 'PST_MONTHS',
198
			'y' => 'PST_YEARS',
199
		);
200
		foreach ($time_options as $value => $label)
201
		{
202
			$this->template->assign_block_vars('similar_time_options', array(
203
				'VALUE'      => $value,
204
				'LABEL'      => $label,
205
				'S_SELECTED' => $value === $this->config['similar_topics_type'],
206
			));
207
		}
208
209
		$this->template->assign_vars(array(
210
			'S_PST_ENABLE'    => $this->isset_or_default($this->config['similar_topics'], false),
211
			'PST_LIMIT'       => $this->isset_or_default($this->config['similar_topics_limit'], ''),
212
			'PST_CACHE'       => $this->isset_or_default($this->config['similar_topics_cache'], ''),
213
			'PST_SENSE'       => $this->isset_or_default($this->config['similar_topics_sense'], ''),
214
			'PST_WORDS'       => $this->isset_or_default($this->config_text_get('similar_topics_words'), ''),
215
			'PST_TIME'        => $this->get_pst_time($this->config['similar_topics_time'], $this->config['similar_topics_type']),
216
			'S_PST_NO_COMPAT' => $this->similartopics === null || !$this->similartopics->is_fulltext('topic_title'),
217
			'U_ACTION'        => $this->u_action,
218
		));
219
220
		// If postgresql, we need to make an options list of text search names
221
		if ($this->similartopics && $this->similartopics->get_type() === 'postgres')
222
		{
223
			$this->language->add_lang('acp/search');
224
			foreach ($this->get_cfgname_list() as $row)
225
			{
226
				$this->template->assign_block_vars('postgres_ts_names', array(
227
					'NAME'       => $row['ts_name'],
228
					'S_SELECTED' => $row['ts_name'] === $this->config['pst_postgres_ts_name'],
229
				));
230
			}
231
		}
232
233
		$forum_list = $this->get_forum_list();
234
		foreach ($forum_list as $row)
235
		{
236
			$this->template->assign_block_vars('forums', array(
237
				'FORUM_NAME'           => $row['forum_name'],
238
				'FORUM_ID'             => $row['forum_id'],
239
				'CHECKED_IGNORE_FORUM' => $row['similar_topics_ignore'] ? 'checked="checked"' : '',
240
				'CHECKED_NOSHOW_FORUM' => $row['similar_topics_hide'] ? 'checked="checked"' : '',
241
				'S_IS_ADVANCED'        => (bool) $row['similar_topic_forums'],
242
				'U_ADVANCED'           => "{$this->u_action}&amp;action=advanced&amp;f=" . $row['forum_id'],
243
				'U_FORUM'              => append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']),
0 ignored issues
show
Bug introduced by
The function append_sid 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

243
				'U_FORUM'              => /** @scrutinizer ignore-call */ append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']),
Loading history...
244
			));
245
		}
246
	}
247
248
	/**
249
	 * Display/Save advanced settings
250
	 *
251
	 * @access protected
252
	 */
253
	protected function advanced_settings()
254
	{
255
		$forum_id = $this->request->variable('f', 0);
256
257
		if ($this->request->is_set_post('submit'))
258
		{
259
			$this->check_form_key($this->form_key);
260
261
			$similar_topic_forums = $this->request->variable('similar_forums_id', array(0));
262
			$similar_topic_forums = !empty($similar_topic_forums) ? json_encode($similar_topic_forums) : '';
263
264
			$sql = 'UPDATE ' . FORUMS_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\controller\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
265
				SET similar_topic_forums = '" . $this->db->sql_escape($similar_topic_forums) . "'
266
				WHERE forum_id = $forum_id";
267
			$this->db->sql_query($sql);
268
269
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG');
270
271
			$this->end('PST_SAVED');
272
		}
273
274
		$forum_name = '';
275
		$selected = array();
276
		if ($forum_id > 0)
277
		{
278
			$sql = 'SELECT forum_name, similar_topic_forums
279
				FROM ' . FORUMS_TABLE . "
280
				WHERE forum_id = $forum_id";
281
			$result = $this->db->sql_query($sql);
282
			while ($fid = $this->db->sql_fetchrow($result))
283
			{
284
				$selected = json_decode($fid['similar_topic_forums'], true);
285
				$forum_name = $fid['forum_name'];
286
			}
287
			$this->db->sql_freeresult($result);
288
		}
289
290
		$this->template->assign_vars(array(
291
			'S_ADVANCED_SETTINGS'    => true,
292
			'SIMILAR_FORUMS_OPTIONS' => make_forum_select($selected, false, false, true),
0 ignored issues
show
Bug introduced by
The function make_forum_select 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

292
			'SIMILAR_FORUMS_OPTIONS' => /** @scrutinizer ignore-call */ make_forum_select($selected, false, false, true),
Loading history...
293
			'PST_FORUM_NAME'         => $forum_name,
294
			'U_ACTION'               => $this->u_action . '&amp;action=advanced&amp;f=' . $forum_id,
295
			'U_BACK'                 => $this->u_action,
296
		));
297
	}
298
299
	/**
300
	 * Check form key, trigger error if invalid
301
	 *
302
	 * @access protected
303
	 * @param string $form_key The form key value
304
	 */
305
	protected function check_form_key($form_key)
306
	{
307
		if (!check_form_key($form_key))
0 ignored issues
show
Bug introduced by
The function check_form_key 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

307
		if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
308
		{
309
			$this->end('FORM_INVALID', E_USER_WARNING);
310
		}
311
	}
312
313
	/**
314
	 * Get list of PostgreSQL text search names
315
	 *
316
	 * @access protected
317
	 * @return array array of text search names
318
	 */
319
	protected function get_cfgname_list()
320
	{
321
		$sql = 'SELECT cfgname AS ts_name FROM pg_ts_config';
322
		$result = $this->db->sql_query($sql);
323
		$ts_options = $this->db->sql_fetchrowset($result);
324
		$this->db->sql_freeresult($result);
325
326
		return $ts_options;
327
	}
328
329
	/**
330
	 * Get forums list
331
	 *
332
	 * @access protected
333
	 * @return array forum data rows
334
	 */
335
	protected function get_forum_list()
336
	{
337
		$sql = 'SELECT forum_id, forum_name, similar_topic_forums, similar_topics_hide, similar_topics_ignore
338
			FROM ' . FORUMS_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\controller\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
339
			WHERE forum_type = ' . FORUM_POST . '
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\controller\FORUM_POST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
340
			ORDER BY left_id ASC';
341
		$result = $this->db->sql_query($sql);
342
		$forum_list = $this->db->sql_fetchrowset($result);
343
		$this->db->sql_freeresult($result);
344
345
		return $forum_list;
346
	}
347
348
	/**
349
	 * Update the similar topics columns in the forums table
350
	 *
351
	 * @access protected
352
	 * @param string $column    The name of the column to update
353
	 * @param array  $forum_ids An array of forum_ids
354
	 */
355
	protected function update_forum($column, $forum_ids)
356
	{
357
		$this->db->sql_transaction('begin');
358
359
		// Set marked forums (in set) to 1
360
		$sql = 'UPDATE ' . FORUMS_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\controller\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
361
			SET $column = 1
362
			WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, false, true);
363
		$this->db->sql_query($sql);
364
365
		// Set unmarked forums (not in set) to 0
366
		$sql = 'UPDATE ' . FORUMS_TABLE . "
367
			SET $column = 0
368
			WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, true, true);
369
		$this->db->sql_query($sql);
370
371
		$this->db->sql_transaction('commit');
372
	}
373
374
	/**
375
	 * Store a config_text item in the database.
376
	 *
377
	 * @access protected
378
	 * @param string $name  Name of a config_text item
379
	 * @param string $value Value of a config_text item
380
	 */
381
	protected function config_text_set($name, $value)
382
	{
383
		$this->config_text->set($name, $value);
384
		$this->cache->put("_$name", $value);
385
	}
386
387
	/**
388
	 * Get a config_text value from the cache if it is cached, otherwise
389
	 * get it directly from the database.
390
	 *
391
	 * @access protected
392
	 * @param string $name Name of a config_text item
393
	 * @return string|null Value of a config_text item, either cached or from db
394
	 */
395
	protected function config_text_get($name)
396
	{
397
		if (($value = $this->cache->get("_$name")) === false)
398
		{
399
			$value = $this->config_text->get($name);
400
401
			$this->cache->put("_$name", $value);
402
		}
403
404
		return !empty($value) ? $value : null;
405
	}
406
407
	/**
408
	 * Calculate the time in seconds based on requested time period length
409
	 *
410
	 * @access protected
411
	 * @param int    $length user entered value
412
	 * @param string $type   years, months, weeks, days (y|m|w|d)
413
	 * @return int time in seconds
414
	 */
415
	protected function set_pst_time($length, $type = 'y')
416
	{
417
		$type = isset($this->times[$type]) ? $type : 'y';
418
419
		return (int) ($length * $this->times[$type]);
420
	}
421
422
	/**
423
	 * Get the correct time period length value for the form
424
	 *
425
	 * @access protected
426
	 * @param int    $time as a timestamp
427
	 * @param string $type years, months, weeks, days (y|m|w|d)
428
	 * @return int time converted to the given $type
429
	 */
430
	protected function get_pst_time($time, $type = '')
431
	{
432
		return isset($this->times[$type]) ? (int) round($time / $this->times[$type]) : 0;
433
	}
434
435
	/**
436
	 * Return a variable if it is set, otherwise default
437
	 *
438
	 * @access protected
439
	 * @param mixed $var     The variable to test
440
	 * @param mixed $default The default value to use
441
	 * @return mixed The value of the variable if set, otherwise default value
442
	 */
443
	protected function isset_or_default($var, $default)
444
	{
445
		return null !== $var ? $var : $default;
446
	}
447
448
	/**
449
	 * End script execution with a trigger_error message
450
	 *
451
	 * @access protected
452
	 * @param string $message Language key string
453
	 * @param int    $code    E_USER_NOTICE|E_USER_WARNING
454
	 * @return void
455
	 */
456
	protected function end($message, $code = E_USER_NOTICE)
457
	{
458
		trigger_error($this->language->lang($message) . adm_back_link($this->u_action), $code);
0 ignored issues
show
Bug introduced by
The function adm_back_link 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

458
		trigger_error($this->language->lang($message) . /** @scrutinizer ignore-call */ adm_back_link($this->u_action), $code);
Loading history...
459
	}
460
}
461