Passed
Push — master ( 7a05a5...807ba8 )
by Sylver
03:18
created

category   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 368
Duplicated Lines 0 %

Importance

Changes 27
Bugs 0 Features 0
Metric Value
eloc 186
dl 0
loc 368
rs 8.5599
c 27
b 0
f 0
wmc 48

18 Methods

Rating   Name   Duplication   Size   Complexity  
A get_version() 0 16 2
A __construct() 0 13 1
A capitalize() 0 3 1
A verify_cat_langs() 0 18 4
A cat_to_template() 0 7 4
A get_langs() 0 13 2
A cat_to_ajax() 0 16 4
A get_max_id() 0 10 1
A cat_name() 0 31 3
A category_exist() 0 9 1
A get_max_order() 0 10 1
A set_order() 0 21 6
A move_cat() 0 28 3
A smilies_count() 0 12 2
B extract_list_categories() 0 47 10
A get_cat_id() 0 10 1
A get_first_order() 0 16 1
A get_cat_nb() 0 10 1

How to fix   Complexity   

Complex Class

Complex classes like category often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use category, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * @package		Breizh Smilies Categories Extension
5
 * @copyright	(c) 2020-2024 Sylver35  https://breizhcode.com
6
 * @license		https://opensource.org/licenses/gpl-license.php GNU Public License
7
 *
8
 */
9
10
namespace sylver35\smiliescat\core;
11
12
use phpbb\cache\driver\driver_interface as cache;
13
use phpbb\db\driver\driver_interface as db;
14
use phpbb\config\config;
15
use phpbb\controller\helper;
16
use phpbb\user;
17
use phpbb\language\language;
18
use phpbb\template\template;
19
use phpbb\extension\manager;
20
use phpbb\log\log;
21
22
class category
23
{
24
	/** @var \phpbb\cache\driver\driver_interface */
25
	protected $cache;
26
27
	/** @var \phpbb\db\driver\driver_interface */
28
	protected $db;
29
30
	/** @var \phpbb\config\config */
31
	protected $config;
32
33
	/* @var \phpbb\controller\helper */
34
	protected $helper;
35
36
	/** @var \phpbb\user */
37
	protected $user;
38
39
	/** @var \phpbb\language\language */
40
	protected $language;
41
42
	/** @var \phpbb\template\template */
43
	protected $template;
44
45
	/** @var \phpbb\extension\manager "Extension Manager" */
46
	protected $ext_manager;
47
48
	/** @var \phpbb\log\log */
49
	protected $log;
50
51
	/**
52
	 * The database tables
53
	 *
54
	 * @var string */
55
	protected $smilies_category_table;
56
57
	/** @var string phpBB root path */
58
	protected $root_path;
59
60
	/**
61
	 * Constructor
62
	 */
63
	public function __construct(cache $cache, db $db, config $config, helper $helper, user $user, language $language, template $template, manager $ext_manager, log $log, $smilies_category_table, $root_path)
64
	{
65
		$this->cache = $cache;
66
		$this->db = $db;
67
		$this->config = $config;
68
		$this->helper = $helper;
69
		$this->user = $user;
70
		$this->language = $language;
71
		$this->template = $template;
72
		$this->ext_manager = $ext_manager;
73
		$this->log = $log;
74
		$this->smilies_category_table = $smilies_category_table;
75
		$this->root_path = $root_path;
76
	}
77
78
	public function get_version()
79
	{
80
		if (($data = $this->cache->get('_smiliescat_version')) === false)
81
		{
82
			$md_manager = $this->ext_manager->create_extension_metadata_manager('sylver35/smiliescat');
83
			$meta = $md_manager->get_metadata();
84
85
			$data = [
86
				'version'	=> $meta['version'],
87
				'homepage'	=> $meta['homepage'],
88
			];
89
			// cache for 7 days
90
			$this->cache->put('_smiliescat_version', $data, 604800);
91
		}
92
93
		return $data;
94
	}
95
96
	public function smilies_count($cat)
97
	{
98
		$sql = $this->db->sql_build_query('SELECT', [
99
			'SELECT'	=> 'COUNT(DISTINCT smiley_id) AS smilies_count',
100
			'FROM'		=> [SMILIES_TABLE => ''],
101
			'WHERE'		=> ($cat > -1) ? 'category = ' . (int) $cat : "code <> ''",
102
		]);
103
		$result = $this->db->sql_query($sql);
104
		$nb = (int) $this->db->sql_fetchfield('smilies_count');
105
		$this->db->sql_freeresult($result);
106
107
		return $nb;
108
	}
109
110
	public function capitalize($var)
111
	{
112
		return ucfirst(strtolower(trim($var)));
113
	}
114
115
	public function get_langs()
116
	{
117
		$data = [];
118
		$sql = 'SELECT *
119
			FROM ' . LANG_TABLE;
120
		$result = $this->db->sql_query($sql);
121
		while ($row = $this->db->sql_fetchrow($result))
122
		{
123
			$data[$row['lang_id']] = $row['lang_iso'];
124
		}
125
		$this->db->sql_freeresult($result);
126
127
		return $data;
128
	}
129
130
	public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax)
131
	{
132
		$data = [];
133
		foreach ($langs as $id => $iso)
134
		{
135
			if (!isset($lang_cat[$cat][$id]))
136
			{
137
				$data[] = $iso;
138
			}
139
		}
140
141
		if ($ajax === false)
142
		{
143
			$this->cat_to_template($i, $data);
144
		}
145
		else
146
		{
147
			return $this->cat_to_ajax($i, $data);
148
		}
149
	}
150
151
	private function cat_to_template($i, $data)
152
	{
153
		if (($i !== 0) && !empty($data))
154
		{
155
			$this->template->assign_block_vars('categories', [
156
				'ERROR'			=> true,
157
				'LANG_EMPTY'	=> $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data),
158
			]);
159
		}
160
	}
161
162
	private function cat_to_ajax($i, $data)
163
	{
164
		$values = [
165
			'error'			=> false,
166
			'lang_empty'	=> '',
167
		];
168
169
		if (($i !== 0) && !empty($data))
170
		{
171
			$values = [
172
				'error'			=> true,
173
				'lang_empty'	=> $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data),
174
			];
175
		}
176
177
		return $values;
178
	}
179
180
	public function category_exist()
181
	{
182
		$sql = 'SELECT COUNT(cat_id) AS total
183
			FROM ' . $this->smilies_category_table;
184
		$result = $this->db->sql_query($sql);
185
		$total = (int) $this->db->sql_fetchfield('total');
186
		$this->db->sql_freeresult($result);
187
188
		return $total;
189
	}
190
191
	public function cat_name($cat)
192
	{
193
		$cat_name = $this->language->lang('SC_CATEGORY_DEFAUT');
194
		if ($cat > 0)
195
		{
196
			$lang = (string) $this->user->lang_name;
197
			$sql = 'SELECT cat_name
198
				FROM ' . $this->smilies_category_table . "
199
					WHERE cat_id = $cat
200
					AND cat_lang = '$lang'";
201
			$result = $this->db->sql_query_limit($sql, 1);
202
			$row = $this->db->sql_fetchrow($result);
203
			$this->db->sql_freeresult($result);
204
			if (isset($row['cat_name']))
205
			{
206
				$cat_name = $row['cat_name'];
207
			}
208
			else
209
			{
210
				$sql = 'SELECT cat_name
211
					FROM ' . $this->smilies_category_table . "
212
						WHERE cat_lang = 'en'
213
						AND cat_id = $cat";
214
				$result = $this->db->sql_query_limit($sql, 1);
215
				$row = $this->db->sql_fetchrow($result);
216
				$cat_name = $row['cat_name'];
217
				$this->db->sql_freeresult($result);
218
			}
219
		}
220
221
		return $cat_name;
222
	}
223
224
	public function get_max_order()
225
	{
226
		// Get max order id...
227
		$sql = 'SELECT MAX(cat_order) AS maxi
228
			FROM ' . $this->smilies_category_table;
229
		$result = $this->db->sql_query($sql);
230
		$max = (int) $this->db->sql_fetchfield('maxi');
231
		$this->db->sql_freeresult($result);
232
233
		return $max;
234
	}
235
236
	public function get_max_id()
237
	{
238
		// Get max id...
239
		$sql = 'SELECT MAX(cat_id) AS id_max
240
			FROM ' . $this->smilies_category_table;
241
		$result = $this->db->sql_query($sql);
242
		$id_max = (int) $this->db->sql_fetchfield('id_max');
243
		$this->db->sql_freeresult($result);
244
245
		return $id_max;
246
	}
247
248
	public function get_first_order()
249
	{
250
		// Get first order id...
251
		$sql = 'SELECT cat_id, cat_order, cat_nb
252
			FROM ' . $this->smilies_category_table . '
253
				WHERE cat_nb > 0
254
				ORDER BY cat_order ASC';
255
		$result = $this->db->sql_query_limit($sql, 1);
256
		$row = $this->db->sql_fetchrow($result);
257
		$data = [
258
			'first'		=> $row['cat_id'],
259
			'cat_nb'	=> $row['cat_nb'],
260
		];
261
		$this->db->sql_freeresult($result);
262
263
		return $data;
264
	}
265
266
	public function get_cat_id($id)
267
	{
268
		$sql = 'SELECT category
269
			FROM ' . SMILIES_TABLE . '
270
				WHERE smiley_id = ' . $id;
271
		$result = $this->db->sql_query($sql);
272
		$category = (int) $this->db->sql_fetchfield('category');
273
		$this->db->sql_freeresult($result);
274
275
		return $category;
276
	}
277
278
	public function get_cat_nb($id)
279
	{
280
		$sql = 'SELECT cat_nb
281
			FROM ' . $this->smilies_category_table . '
282
				WHERE cat_id = ' . (int) $id;
283
		$result = $this->db->sql_query($sql);
284
		$cat_nb = (int) $this->db->sql_fetchfield('cat_nb');
285
		$this->db->sql_freeresult($result);
286
287
		return $cat_nb;
288
	}
289
290
	public function extract_list_categories($cat)
291
	{
292
		$title = '';
293
		$cat_order = $i = 0;
294
		$lang = (string) $this->user->lang_name;
295
		$sql = $this->db->sql_build_query('SELECT', [
296
			'SELECT'	=> '*',
297
			'FROM'		=> [$this->smilies_category_table => ''],
298
			'WHERE'		=> "cat_nb <> 0 AND cat_lang = '$lang'",
299
			'ORDER_BY'	=> 'cat_order ASC',
300
		]);
301
		$result = $this->db->sql_query($sql);
302
		while ($row = $this->db->sql_fetchrow($result))
303
		{
304
			$actual_cat = (int) $row['cat_id'] === $cat;
305
			$this->template->assign_block_vars('categories', [
306
				'CLASS'			=> $actual_cat ? 'cat-active' : 'cat-inactive',
307
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
308
				'CAT_NAME'		=> $row['cat_name'] ?: $row['cat_title'],
309
				'CAT_ORDER'		=> $row['cat_order'],
310
				'CAT_ID'		=> $row['cat_id'],
311
				'CAT_NB'		=> $row['cat_nb'],
312
				'U_CAT'			=> $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]),
313
			]);
314
			$i++;
315
316
			// Keep these values in memory
317
			$title = $actual_cat ? $row['cat_name'] : $title;
318
			$cat_order = $row['cat_order'];
319
		}
320
		$this->db->sql_freeresult($result);
321
322
		// Add the Unclassified category if not empty
323
		if ($nb = $this->smilies_count(0))
324
		{
325
			$this->template->assign_block_vars('categories', [
326
				'CLASS'			=> ($cat === 0) ? 'cat-active' : 'cat-inactive',
327
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
328
				'CAT_NAME'		=> $this->language->lang('SC_CATEGORY_DEFAUT'),
329
				'CAT_ORDER'		=> $cat_order + 1,
330
				'CAT_ID'		=> 0,
331
				'CAT_NB'		=> $nb,
332
				'U_CAT'			=> $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => 0]),
333
			]);
334
		}
335
336
		return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title;
337
	}
338
339
	public function set_order($action, $current_order)
340
	{
341
		// Never move up the first
342
		if ($current_order === 1 && $action === 'move_up')
343
		{
344
			return 0;
345
		}
346
347
		// Never move down the last
348
		$max_order = $this->get_max_order();
349
		if (($current_order === $max_order) && ($action === 'move_down'))
350
		{
351
			return 0;
352
		}
353
354
		// on move_down, switch position with next order_id...
355
		// on move_up, switch position with previous order_id...
356
		$switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1;
357
358
		// Return the new position
359
		return $switch_order_id;
360
	}
361
362
	public function move_cat($id, $action)
363
	{
364
		// Get current order id and title...
365
		$sql = 'SELECT cat_order, cat_title
366
			FROM ' . $this->smilies_category_table . '
367
				WHERE cat_id = ' . $id;
368
		$result = $this->db->sql_query($sql);
369
		$row = $this->db->sql_fetchrow($result);
370
		$current_order = (int) $row['cat_order'];
371
		$title = $row['cat_title'];
372
		$this->db->sql_freeresult($result);
373
374
		$switch_order_id = $this->set_order($action, $current_order);
375
		if ($switch_order_id === 0)
376
		{
377
			return;
378
		}
379
380
		$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $current_order WHERE cat_order = $switch_order_id AND cat_id <> $id");
381
		$move_executed = (bool) $this->db->sql_affectedrows();
382
383
		// Only update the other entry too if the previous entry got updated
384
		if ($move_executed)
385
		{
386
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $switch_order_id WHERE cat_order = $current_order AND cat_id = $id");
387
		}
388
389
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), [$title]);
390
	}
391
}
392