Passed
Branch 1.5.0 (c959a1)
by Sylver
04:41
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 13
dl 0
loc 15
rs 9.8333

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
* @package		Breizh Smilies Categories Extension
5
* @copyright	(c) 2020-2023 Sylver35  https://breizhcode.com
6
* @license		https://opensource.org/licenses/gpl-license.php GNU Public License
7
*
8
*/
9
10
namespace sylver35\smiliescat\controller;
11
12
use sylver35\smiliescat\core\category;
13
use sylver35\smiliescat\core\smiley;
14
use sylver35\smiliescat\core\work;
15
use phpbb\config\config;
16
use phpbb\db\driver\driver_interface as db;
17
use phpbb\pagination;
18
use phpbb\request\request;
19
use phpbb\template\template;
20
use phpbb\user;
21
use phpbb\language\language;
22
use phpbb\log\log;
23
24
class admin_controller
25
{
26
	/* @var \sylver35\smiliescat\core\category */
27
	protected $category;
28
29
	/* @var \sylver35\smiliescat\core\smiley */
30
	protected $smiley;
31
32
	/* @var \sylver35\smiliescat\core\work */
33
	protected $work;
34
35
	/** @var \phpbb\config\config */
36
	protected $config;
37
38
	/** @var \phpbb\db\driver\driver_interface */
39
	protected $db;
40
41
	/** @var \phpbb\pagination */
42
	protected $pagination;
43
44
	/** @var \phpbb\request\request */
45
	protected $request;
46
47
	/** @var \phpbb\template\template */
48
	protected $template;
49
50
	/** @var \phpbb\user */
51
	protected $user;
52
53
	/** @var \phpbb\language\language */
54
	protected $language;
55
56
	/** @var \phpbb\log\log */
57
	protected $log;
58
59
	/** @var string phpBB root path */
60
	protected $root_path;
61
62
	/** @var string Custom form action */
63
	protected $u_action;
64
65
	/**
66
	 * The database tables
67
	 *
68
	 * @var string */
69
	protected $smilies_category_table;
70
71
	/**
72
	 * Constructor
73
	 */
74
	public function __construct(category $category, smiley $smiley, work $work, config $config, db $db, pagination $pagination, request $request, template $template, user $user, language $language, log $log, $root_path, $smilies_category_table)
75
	{
76
		$this->category = $category;
77
		$this->smiley = $smiley;
78
		$this->work = $work;
79
		$this->config = $config;
80
		$this->db = $db;
81
		$this->pagination = $pagination;
82
		$this->request = $request;
83
		$this->template = $template;
84
		$this->user = $user;
85
		$this->language = $language;
86
		$this->log = $log;
87
		$this->root_path = $root_path;
88
		$this->smilies_category_table = $smilies_category_table;
89
	}
90
91
	public function acp_smilies_category($id, $action)
92
	{
93
		$this->language->add_lang('acp/posting');
94
		$start = (int) $this->request->variable('start', 0);
95
		$select = (int) $this->request->variable('select', -1);
96
		$cat_id = (int) $this->request->variable('cat_id', 0);
97
		$ex_cat = (int) $this->request->variable('ex_cat', 0);
98
		$list = $this->request->variable('list', [0]);
99
		$form_key = 'sylver35/smiliescat';
100
		add_form_key($form_key);
101
102
		if ($action)
103
		{
104
			switch ($action)
105
			{
106
				case 'edit':
107
					$this->smiley->edit_smiley($id, $start, $this->u_action);
108
				break;
109
110
				case 'edit_multi':
111
					$list = $this->request->variable('mark', [0]);
112
					$this->smiley->edit_multi_smiley($list, $start, $this->u_action);
113
				break;
114
115
				case 'modify':
116
					if (!check_form_key($form_key))
117
					{
118
						trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
119
					}
120
121
					$this->smiley->modify_smiley($id, $cat_id, $ex_cat);
122
					trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
123
				break;
124
125
				case 'modify_list':
126
					foreach ($list as $smiley)
127
					{
128
						$this->smiley->modify_smiley($smiley, $cat_id);
129
					}
130
					trigger_error($this->language->lang('SMILIES_EDITED', count($list)) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
131
				break;
132
			}
133
134
			$this->template->assign_vars([
135
				'IN_ACTION' => true,
136
			]);
137
		}
138
		else
139
		{
140
			$this->smiley->extract_list_smilies($select, $start, $this->u_action);
141
142
			$this->template->assign_vars([
143
				'LIST_CATEGORY'		=> $this->smiley->select_categories($select, true, true),
144
				'U_SELECT_CAT'		=> $this->u_action . '&amp;select=' . $select,
145
				'U_MODIFY_LIST'		=> $this->u_action . '&amp;action=edit_multi&amp;start=' . $start,
146
				'U_BACK'			=> $this->u_action,
147
			]);
148
		}
149
150
		$this->template->assign_vars([
151
			'CATEGORIE_SMILIES'	=> true,
152
		]);
153
	}
154
155
	public function acp_categories_config($id, $action, $mode)
156
	{
157
		$this->language->add_lang('acp/language');
158
		$form_key = 'sylver35/smiliescat';
159
		add_form_key($form_key);
160
161
		if ($action)
162
		{
163
			if (in_array($action, ['config_cat', 'add_cat', 'edit_cat']) && !check_form_key($form_key))
164
			{
165
				trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
166
			}
167
168
			switch ($action)
169
			{
170
				case 'config_cat':
171
					$this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15));
172
173
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time());
174
					trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
175
				break;
176
177
				case 'add':
178
					$this->work->add_cat($this->u_action);
179
				break;
180
181
				case 'add_cat':
182
					$this->work->add_category($this->u_action);
183
				break;
184
185
				case 'edit':
186
					$this->work->edit_cat((int) $id, $this->u_action);
187
				break;
188
189
				case 'edit_cat':
190
					$this->work->edit_category((int) $id, $this->u_action);
191
				break;
192
193
				case 'delete':
194
					if (confirm_box(true))
195
					{
196
						$this->work->delete_cat((int) $id, $this->u_action);
197
					}
198
					else
199
					{
200
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([
201
							'mode'		=> $mode,
202
							'id'		=> $id,
203
							'action'	=> $action,
204
						]));
205
					}
206
				break;
207
			}
208
209
			$this->template->assign_vars([
210
				'IN_ACTION'	=> true,
211
			]);
212
		}
213
		else
214
		{
215
			$this->work->adm_list_cat($this->u_action);
216
		}
217
218
		$this->template->assign_vars([
219
			'CATEGORIE_CONFIG'		=> true,
220
			'SMILIES_PER_PAGE_CAT'	=> $this->config['smilies_per_page_cat'],
221
			'U_ACTION_CONFIG'		=> $this->u_action . '&amp;action=config_cat',
222
			'U_ADD'					=> $this->u_action . '&amp;action=add',
223
		]);
224
	}
225
226
	/**
227
	 * Set page url
228
	 *
229
	 * @param string $u_action Custom form action
230
	 * @return null
231
	 * @access public
232
	 */
233
	public function set_page_url($u_action)
234
	{
235
		$this->u_action = $u_action;
236
	}
237
}
238