admin_controller::__construct()   A
last analyzed

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
 * Topic Prefixes extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\topicprefixes\controller;
12
13
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...
14
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...
15
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...
16
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...
17
use phpbb\topicprefixes\prefixes\manager;
18
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...
19
20
/**
21
 * Class admin_controller
22
 */
23
class admin_controller
24
{
25
	/** @var manager Topic prefixes manager object */
26
	protected $manager;
27
28
	/** @var language phpBB language object */
29
	protected $language;
30
31
	/** @var log phpBB log object */
32
	protected $log;
33
34
	/** @var request phpBB request object */
35
	protected $request;
36
37
	/** @var template phpBB template object */
38
	protected $template;
39
40
	/** @var user phpBB user object */
41
	protected $user;
42
43
	/** @var string phpBB root path */
44
	protected $root_path;
45
46
	/** @var string PHP extension */
47
	protected $php_ext;
48
49
	/** @var string Form key used for form validation */
50
	protected $form_key;
51
52
	/** @var int Forum identifier */
53
	protected $forum_id;
54
55
	/** @var string Custom form action */
56
	protected $u_action;
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param manager  $manager         Topic prefixes manager object
62
	 * @param language $language        phpBB language object
63
	 * @param log      $log             phpBB log object
64
	 * @param request  $request         phpBB request object
65
	 * @param template $template        phpBB template object
66
	 * @param user     $user            phpBB user object
67
	 * @param string   $phpbb_root_path phpBB root path
68
	 * @param string   $phpEx           PHP extension
69
	 */
70
	public function __construct(manager $manager, language $language, log $log, request $request, template $template, user $user, $phpbb_root_path, $phpEx)
71
	{
72
		$this->manager = $manager;
73
		$this->language = $language;
74
		$this->log = $log;
75
		$this->request = $request;
76
		$this->template = $template;
77
		$this->user = $user;
78
		$this->root_path = $phpbb_root_path;
79
		$this->php_ext = $phpEx;
80
	}
81
82
	/**
83
	 * Main handler, called by the ACP module
84
	 *
85
	 * @return void
86
	 */
87
	public function main()
88
	{
89
		$this->form_key = 'acp_topic_prefixes';
90
		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

90
		/** @scrutinizer ignore-call */ 
91
  add_form_key($this->form_key);
Loading history...
91
92
		$action = $this->request->variable('action', '');
93
		$prefix_id = $this->request->variable('prefix_id', 0);
94
		$this->set_forum_id($this->request->variable('forum_id', 0));
95
96
		switch ($action)
97
		{
98
			case 'add':
99
				$this->add_prefix();
100
			break;
101
102
			case 'edit':
103
			case 'delete':
104
				$this->{$action . '_prefix'}($prefix_id);
105
			break;
106
107
			case 'move_up':
108
			case 'move_down':
109
				$this->move_prefix($prefix_id, str_replace('move_', '', $action));
110
			break;
111
		}
112
113
		$this->display_settings();
114
	}
115
116
	/**
117
	 * Display topic prefix settings
118
	 *
119
	 * @return void
120
	 */
121
	public function display_settings()
122
	{
123
		foreach ($this->manager->get_prefixes($this->forum_id) as $prefix)
124
		{
125
			$this->template->assign_block_vars('prefixes', [
126
				'PREFIX_TAG'		=> $prefix['prefix_tag'],
127
				'PREFIX_ENABLED'	=> (int) $prefix['prefix_enabled'],
128
				'U_EDIT'			=> "{$this->u_action}&amp;action=edit&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . generate_link_hash('edit' . $prefix['prefix_id']),
0 ignored issues
show
Bug introduced by
The function generate_link_hash 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

128
				'U_EDIT'			=> "{$this->u_action}&amp;action=edit&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . /** @scrutinizer ignore-call */ generate_link_hash('edit' . $prefix['prefix_id']),
Loading history...
129
				'U_DELETE'			=> "{$this->u_action}&amp;action=delete&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id,
130
				'U_MOVE_UP'			=> "{$this->u_action}&amp;action=move_up&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . generate_link_hash('up' . $prefix['prefix_id']),
131
				'U_MOVE_DOWN'		=> "{$this->u_action}&amp;action=move_down&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . generate_link_hash('down' . $prefix['prefix_id']),
132
			]);
133
		}
134
135
		$this->template->assign_vars([
136
			'S_FORUM_OPTIONS'	=> make_forum_select($this->forum_id, 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

136
			'S_FORUM_OPTIONS'	=> /** @scrutinizer ignore-call */ make_forum_select($this->forum_id, false, false, true),
Loading history...
137
			'FORUM_ID'			=> $this->forum_id,
138
			'U_ACTION'			=> $this->u_action,
139
		]);
140
	}
141
142
	/**
143
	 * Add a prefix
144
	 *
145
	 * @return void
146
	 */
147
	public function add_prefix()
148
	{
149
		if ($this->request->is_set_post('submit'))
150
		{
151
			if (!check_form_key($this->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

151
			if (!/** @scrutinizer ignore-call */ check_form_key($this->form_key))
Loading history...
152
			{
153
				$this->trigger_message('FORM_INVALID', E_USER_WARNING);
154
			}
155
156
			$tag = $this->request->variable('prefix_tag', '', true);
157
			$prefix = $this->manager->add_prefix($tag, $this->forum_id);
158
159
			if ($prefix)
160
			{
161
				$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_ADDED');
162
			}
163
		}
164
	}
165
166
	/**
167
	 * Edit a prefix
168
	 *
169
	 * @param int $prefix_id The prefix identifier to edit
170
	 * @return void
171
	 */
172
	public function edit_prefix($prefix_id)
173
	{
174
		if (!$this->check_hash('edit' . $prefix_id))
175
		{
176
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
177
		}
178
179
		try
180
		{
181
			$prefix = $this->manager->get_prefix($prefix_id);
182
			$this->manager->update_prefix(!$prefix ?: $prefix['prefix_id'], !$prefix ? [] : ['prefix_enabled' => !$prefix['prefix_enabled']]);
183
		}
184
		catch (\OutOfBoundsException $e)
185
		{
186
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
187
		}
188
189
		if ($this->request->is_ajax())
190
		{
191
			$json_response = new \phpbb\json_response;
0 ignored issues
show
Bug introduced by
The type phpbb\json_response 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...
192
			$json_response->send(['success' => true]);
193
		}
194
	}
195
196
	/**
197
	 * Delete a prefix
198
	 *
199
	 * @param int $prefix_id The prefix identifier to delete
200
	 * @return void
201
	 */
202
	public function delete_prefix($prefix_id)
203
	{
204
		if (confirm_box(true))
0 ignored issues
show
Bug introduced by
The function confirm_box 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

204
		if (/** @scrutinizer ignore-call */ confirm_box(true))
Loading history...
205
		{
206
			try
207
			{
208
				$prefix = $this->manager->get_prefix($prefix_id);
209
				$this->manager->delete_prefix(!$prefix ?: $prefix['prefix_id']);
210
				$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_DELETED');
211
			}
212
			catch (\OutOfBoundsException $e)
213
			{
214
				$this->trigger_message($e->getMessage(), E_USER_WARNING);
215
			}
216
217
			$this->trigger_message('TOPIC_PREFIX_DELETED');
218
		}
219
220
		confirm_box(false, $this->language->lang('DELETE_TOPIC_PREFIX_CONFIRM'), build_hidden_fields([
0 ignored issues
show
Bug introduced by
The function build_hidden_fields 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

220
		confirm_box(false, $this->language->lang('DELETE_TOPIC_PREFIX_CONFIRM'), /** @scrutinizer ignore-call */ build_hidden_fields([
Loading history...
221
			'mode'		=> 'manage',
222
			'action'	=> 'delete',
223
			'prefix_id'	=> $prefix_id,
224
			'forum_id'	=> $this->forum_id,
225
		]));
226
	}
227
228
	/**
229
	 * Move a prefix up/down
230
	 *
231
	 * @param int    $prefix_id The prefix identifier to move
232
	 * @param string $direction The direction (up|down)
233
	 * @param int    $amount    The amount of places to move (default: 1)
234
	 * @return void
235
	 */
236
	public function move_prefix($prefix_id, $direction, $amount = 1)
237
	{
238
		if (!$this->check_hash($direction . $prefix_id))
239
		{
240
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
241
		}
242
243
		try
244
		{
245
			$this->manager->move_prefix($prefix_id, $direction, $amount);
246
		}
247
		catch (\OutOfBoundsException $e)
248
		{
249
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
250
		}
251
252
		if ($this->request->is_ajax())
253
		{
254
			$json_response = new \phpbb\json_response;
255
			$json_response->send(['success' => true]);
256
		}
257
	}
258
259
	/**
260
	 * Set u_action
261
	 *
262
	 * @param string $u_action Custom form action
263
	 * @return admin_controller
264
	 */
265
	public function set_u_action($u_action)
266
	{
267
		$this->u_action = $u_action;
268
		return $this;
269
	}
270
271
	/**
272
	 * Set forum ID
273
	 *
274
	 * @param int $forum_id Forum identifier
275
	 * @return admin_controller
276
	 */
277
	public function set_forum_id($forum_id)
278
	{
279
		$this->forum_id = $forum_id;
280
		return $this;
281
	}
282
283
	/**
284
	 * Check link hash helper
285
	 *
286
	 * @param string $hash A hashed string
287
	 * @return bool True if hash matches, false if not
288
	 */
289
	protected function check_hash($hash)
290
	{
291
		return check_link_hash($this->request->variable('hash', ''), $hash);
0 ignored issues
show
Bug introduced by
The function check_link_hash 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

291
		return /** @scrutinizer ignore-call */ check_link_hash($this->request->variable('hash', ''), $hash);
Loading history...
292
	}
293
294
	/**
295
	 * Trigger a message and back link for error/success dialogs
296
	 *
297
	 * @param string $message A language key
298
	 * @param int    $error   Error type constant, optional
299
	 * @return void
300
	 */
301
	protected function trigger_message($message = '', $error = E_USER_NOTICE)
302
	{
303
		trigger_error($this->language->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}"), $error);
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

303
		trigger_error($this->language->lang($message) . /** @scrutinizer ignore-call */ adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}"), $error);
Loading history...
304
	}
305
306
	/**
307
	 * Helper for logging topic prefix admin actions
308
	 *
309
	 * @param string $tag     The topic prefix tag
310
	 * @param string $message The log action language key
311
	 * @return void
312
	 */
313
	protected function log($tag, $message)
314
	{
315
		$forum_data = $this->get_forum_info($this->forum_id);
316
317
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $message, time(), [$tag, $forum_data['forum_name']]);
318
	}
319
320
	/**
321
	 * Get a forum's information
322
	 *
323
	 * @param int $forum_id
324
	 * @return mixed Array with the current row, false, if the row does not exist
325
	 */
326
	protected function get_forum_info($forum_id)
327
	{
328
		if (!class_exists('acp_forums'))
329
		{
330
			include $this->root_path . 'includes/acp/acp_forums.' . $this->php_ext;
331
		}
332
333
		$acp_forums = new \acp_forums();
0 ignored issues
show
Bug introduced by
The type acp_forums 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...
334
335
		return $acp_forums->get_forum_info($forum_id);
336
	}
337
}
338