Completed
Pull Request — master (#2)
by Jakub
09:06
created

admin_controller   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 390
Duplicated Lines 6.92 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 10
Bugs 0 Features 1
Metric Value
wmc 36
c 10
b 0
f 1
lcom 1
cbo 0
dl 27
loc 390
ccs 0
cts 219
cp 0
rs 8.8

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A set_page_url() 0 4 1
A load_lang() 0 4 1
A get_page_title() 0 4 1
A get_action() 0 4 1
B action_add() 13 34 3
B action_edit() 14 52 4
B ad_enable() 0 28 6
B action_delete() 0 28 4
B list_ads() 0 26 3
A check_form_key() 0 7 2
A get_form_data() 0 9 1
A validate() 0 11 3
A assign_errors() 0 7 2
A assign_form_data() 0 9 1
A success() 0 4 1
A error() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\admanagement\controller;
12
13
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	const MAX_NAME_LENGTH = 255;
19
20
	/** @var \phpbb\db\driver\driver_interface */
21
	protected $db;
22
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/** @var \phpbb\request\request */
30
	protected $request;
31
32
	/** @var string ads_table */
33
	protected $ads_table;
34
35
	/** @var string php_ext */
36
	protected $php_ext;
37
38
	/** @var string phpbb_admin_path */
39
	protected $phpbb_admin_path;
40
41
	/** @var string Custom form action */
42
	protected $u_action;
43
44
	/** @var array Form validation errors */
45
	protected $errors = array();
46
47
	/**
48
	* Constructor
49
	*
50
	* @param \phpbb\db\driver\driver_interface	$db					DB driver interface
51
	* @param \phpbb\template\template			$template			Template object
52
	* @param \phpbb\user						$user				User object
53
	* @param \phpbb\request\request				$request			Request object
54
	* @param string								$ads_table			Ads table
55
	* @param string								$php_ext			PHP extension
56
	* @param string								$phpbb_admin_path	Path to admin
57
	*/
58
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table, $php_ext, $phpbb_admin_path)
59
	{
60
		$this->db = $db;
61
		$this->template = $template;
62
		$this->user = $user;
63
		$this->request = $request;
64
		$this->ads_table = $ads_table;
65
		$this->php_ext = $php_ext;
66
		$this->phpbb_admin_path = $phpbb_admin_path;
67
	}
68
69
	/**
70
	* Set page url
71
	*
72
	* @param string $u_action Custom form action
73
	* @return void
74
	*/
75
	public function set_page_url($u_action)
76
	{
77
		$this->u_action = $u_action;
78
	}
79
80
	/**
81
	* Load module-specific language
82
	*
83
	* @return void
84
	*/
85
	public function load_lang()
86
	{
87
		$this->user->add_lang_ext('phpbb/admanagement', 'acp');
88
	}
89
90
	/**
91
	* Get ACP page title for Ads module
92
	*
93
	* @return string	Language string for Ads ACP module
94
	*/
95
	public function get_page_title()
96
	{
97
		return $this->user->lang('ACP_ADMANAGEMENT_TITLE');
98
	}
99
100
	/**
101
	* Get action
102
	*
103
	* @return string	Ads module action
104
	*/
105
	public function get_action()
106
	{
107
		return $this->request->variable('action', '');
108
	}
109
110
	/**
111
	* Add an advertisement
112
	*
113
	* @return void
114
	*/
115
	public function action_add()
116
	{
117
		$errors = array();
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
119
		add_form_key('phpbb/admanagement/add');
120
		if ($this->request->is_set_post('submit'))
121
		{
122
			$this->check_form_key('phpbb/admanagement/add');
123
124
			$data = $this->get_form_data();
125
126
			$this->validate($data);
127
128 View Code Duplication
			if (empty($this->errors))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
			{
130
				// Insert the ad data to the database
131
				$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
132
				$this->db->sql_query($sql);
133
134
				$this->success('ACP_AD_ADD_SUCCESS');
135
			}
136
			else
137
			{
138
				$this->assign_errors();
139
				$this->assign_form_data($data);
140
			}
141
		}
142
143
		// Set output vars for display in the template
144
		$this->template->assign_vars(array(
145
			'S_ADD_AD'	=> true,
146
			'U_BACK'	=> $this->u_action,
147
		));
148
	}
149
150
	/**
151
	* Edit an advertisement
152
	*
153
	* @return void
154
	*/
155
	public function action_edit()
156
	{
157
		$ad_id = $this->request->variable('id', 0);
158
		$errors = array();
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
160
		add_form_key('phpbb/admanagement/edit');
161
		if ($this->request->is_set_post('submit'))
162
		{
163
			$this->check_form_key('phpbb/admanagement/edit');
164
165
			$data = $this->get_form_data();
166
167
			$this->validate($data);
168
169 View Code Duplication
			if (empty($this->errors))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
			{
171
				// Insert the ad data to the database
172
				$sql = 'UPDATE ' . $this->ads_table . '
173
					SET ' . $this->db->sql_build_array('UPDATE', $data) . '
174
					WHERE ad_id = ' . (int) $ad_id;
175
				$this->db->sql_query($sql);
176
177
				$this->success('ACP_AD_EDIT_SUCCESS');
178
			}
179
			else
180
			{
181
				$this->assign_errors();
182
			}
183
		}
184
		else
185
		{
186
			$sql = 'SELECT *
187
				FROM ' . $this->ads_table . '
188
				WHERE ad_id = ' . (int) $ad_id;
189
			$result = $this->db->sql_query($sql);
190
			$data = $this->db->sql_fetchrow($result);
191
			$this->db->sql_freeresult($result);
192
193
			if (empty($data))
194
			{
195
				$this->error('ACP_AD_DOES_NOT_EXIST');
196
			}
197
		}
198
199
		// Set output vars for display in the template
200
		$this->template->assign_vars(array(
201
			'S_EDIT_AD'	=> true,
202
			'EDIT_ID'	=> $ad_id,
203
			'U_BACK'	=> $this->u_action,
204
		));
205
		$this->assign_form_data($data);
206
	}
207
208
	/**
209
	* Enable/disable an advertisement
210
	*
211
	* @param	bool	$enable	Enable or disable the advertisement?
212
	* @return void
213
	*/
214
	public function ad_enable($enable)
215
	{
216
		$sql = 'UPDATE ' . $this->ads_table . '
217
			SET ad_enabled = ' . (int) $enable . '
218
			WHERE ad_id = ' . (int) $this->request->variable('id', 0);
219
		$this->db->sql_query($sql);
220
		$success = (bool) $this->db->sql_affectedrows();
221
222
		// If AJAX was used, show user a result message
223
		if ($this->request->is_ajax())
224
		{
225
			$json_response = new \phpbb\json_response;
226
			$json_response->send(array(
227
				'text'	=> $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
228
				'title'	=> $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
229
			));
230
		}
231
232
		// Otherwise, show traditional infobox
233
		if ($success)
234
		{
235
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
236
		}
237
		else
238
		{
239
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
240
		}
241
	}
242
243
	/**
244
	* Delete an advertisement
245
	*
246
	* @return void
247
	*/
248
	public function action_delete()
249
	{
250
		$ad_id = $this->request->variable('id', 0);
251
		if ($ad_id)
252
		{
253
			if (confirm_box(true))
254
			{
255
				$sql = 'DELETE FROM ' . $this->ads_table . '
256
					WHERE ad_id = ' . (int) $ad_id;
257
				$this->db->sql_query($sql);
258
259
				// Only notify user on error
260
				if (!$this->db->sql_affectedrows())
261
				{
262
					$this->error('ACP_AD_DELETE_ERRORED');
263
				}
264
			}
265
			else
266
			{
267
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
268
					'id'		=> $ad_id,
269
					'i'			=> $this->request->variable('i', ''),
270
					'mode'		=> $this->request->variable('mode', ''),
271
					'action'	=> 'delete'
272
				)));
273
			}
274
		}
275
	}
276
277
278
	/**
279
	* Display the ads
280
	*
281
	* @return void
282
	*/
283
	public function list_ads()
284
	{
285
		$sql = 'SELECT ad_id, ad_name, ad_enabled
286
			FROM ' . $this->ads_table;
287
		$result = $this->db->sql_query($sql);
288
		while ($row = $this->db->sql_fetchrow($result))
289
		{
290
			$ad_enabled = (bool) $row['ad_enabled'];
291
292
			$this->template->assign_block_vars('ads', array(
293
				'NAME'		=> $row['ad_name'],
294
				'S_ENABLED'	=> (int) $ad_enabled,
295
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
296
				'U_PREVIEW'	=> append_sid(generate_board_url() . '/index.' . $this->php_ext, 'ad_preview=' . $row['ad_id']),
297
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
298
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
299
			));
300
		}
301
		$this->db->sql_freeresult($result);
302
303
		// Set output vars for display in the template
304
		$this->template->assign_vars(array(
305
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
306
			'ICON_PREVIEW'	=> '<img src="' . htmlspecialchars($this->phpbb_admin_path) . 'images/file_up_to_date.gif" alt="' . $this->user->lang('AD_PREVIEW') . '" title="' . $this->user->lang('AD_PREVIEW') . '" />',
307
		));
308
	}
309
310
	/**
311
	* Check the form key.
312
	*
313
	* @param	string	$form_name	The name of the form.
314
	* @return void
315
	*/
316
	protected function check_form_key($form_name)
317
	{
318
		if (!check_form_key($form_name))
319
		{
320
			$this->errors[] = $this->user->lang('FORM_INVALID');
321
		}
322
	}
323
324
	/**
325
	* Get admin form data.
326
	*
327
	* @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
328
	*/
329
	protected function get_form_data()
330
	{
331
		return array(
332
			'ad_name'		=> $this->request->variable('ad_name', '', true),
333
			'ad_note'		=> $this->request->variable('ad_note', '', true),
334
			'ad_code'		=> $this->request->variable('ad_code', '', true),
335
			'ad_enabled'	=> $this->request->variable('ad_enabled', false),
336
		);
337
	}
338
339
	/**
340
	* Validate form data.
341
	*
342
	* @param	array	$data	The form data.
343
	* @return void
344
	*/
345
	protected function validate($data)
346
	{
347
		if ($data['ad_name'] === '')
348
		{
349
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
350
		}
351
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
352
		{
353
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
354
		}
355
	}
356
357
	/**
358
	* Assign errors to the template.
359
	*
360
	* @return void
361
	*/
362
	protected function assign_errors()
363
	{
364
		$this->template->assign_vars(array(
365
			'S_ERROR'			=> (bool) count($this->errors),
366
			'ERROR_MSG'			=> count($this->errors) ? implode('<br />', $this->errors) : '',
367
		));
368
	}
369
370
	/**
371
	* Assign form data to the template.
372
	*
373
	* @param	array	$data	The form data.
374
	* @return void
375
	*/
376
	protected function assign_form_data($data)
377
	{
378
		$this->template->assign_vars(array(
379
			'AD_NAME'		=> $data['ad_name'],
380
			'AD_NOTE'		=> $data['ad_note'],
381
			'AD_CODE'		=> $data['ad_code'],
382
			'AD_ENABLED'	=> $data['ad_enabled'],
383
		));
384
	}
385
386
	/**
387
	* Print success message.
388
	*
389
	* It takes arguments in the form of a language key, followed by language substitution values.
390
	*/
391
	protected function success()
392
	{
393
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
394
	}
395
396
	/**
397
	* Print error message.
398
	*
399
	* It takes arguments in the form of a language key, followed by language substitution values.
400
	*/
401
	protected function error()
402
	{
403
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
404
	}
405
}
406