Passed
Push — 1.6.0 ( d55b30...970b60 )
by Sylver
04:56
created

category   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Importance

Changes 26
Bugs 0 Features 0
Metric Value
eloc 189
dl 0
loc 375
rs 6.96
c 26
b 0
f 0
wmc 53

19 Methods

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

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-2023 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, $compact = false)
97
	{
98
		$sql = $this->db->sql_build_query('SELECT', [
99
			'SELECT'	=> (!$compact) ? 'COUNT(DISTINCT smiley_id) AS smilies_count' : 'COUNT(DISTINCT smiley_url) 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
		$return = [];
118
		$sql = 'SELECT *
119
			FROM ' . LANG_TABLE;
120
		$result = $this->db->sql_query($sql);
121
		while ($row = $this->db->sql_fetchrow($result))
122
		{
123
			$return[$row['lang_id']] = $row['lang_iso'];
124
		}
125
		$this->db->sql_freeresult($result);
126
127
		return $return;
128
	}
129
130
	public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax)
131
	{
132
		$return = [];
133
		foreach ($langs as $id => $iso)
134
		{
135
			if (!isset($lang_cat[$cat][$id]))
136
			{
137
				$return[] = $iso;
138
			}
139
		}
140
141
		if ($ajax === false)
142
		{
143
			$this->cat_to_template($i, $return);
144
		}
145
		else
146
		{
147
			return $this->cat_to_ajax($i, $return);
148
		}
149
	}
150
151
	private function cat_to_template($i, $return)
152
	{
153
		if (($i !== 0) && !empty($return))
154
		{
155
			$this->template->assign_block_vars('categories', [
156
				'ERROR'			=> true,
157
				'LANG_EMPTY'	=> $this->language->lang('SC_LANGUAGE_EMPTY', (count($return) > 1) ? 2 : 1) . implode(', ', $return),
158
			]);
159
		}
160
	}
161
162
	private function cat_to_ajax($i, $return)
163
	{
164
		$values = [
165
			'error'			=> false,
166
			'lang_empty'	=> '',
167
		];
168
169
		if (($i !== 0) && !empty($return))
170
		{
171
			$values = [
172
				'error'			=> true,
173
				'lang_empty'	=> $this->language->lang('SC_LANGUAGE_EMPTY', (count($return) > 1) ? 2 : 1) . implode(', ', $return),
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
			ORDER BY cat_order ASC';
185
		$result = $this->db->sql_query_limit($sql, 1);
186
		$total = (int) $this->db->sql_fetchfield('total');
187
		$this->db->sql_freeresult($result);
188
189
		return $total;
190
	}
191
192
	public function cat_name($cat)
193
	{
194
		$cat_name = $this->language->lang('SC_CATEGORY_DEFAUT');
195
		if ($cat > 0)
196
		{
197
			$lang = (string) $this->user->lang_name;
198
			$sql = 'SELECT cat_name
199
				FROM ' . $this->smilies_category_table . "
200
					WHERE cat_lang = '$lang'
201
					AND cat_id = $cat";
202
			$result = $this->db->sql_query_limit($sql, 1);
203
			$row = $this->db->sql_fetchrow($result);
204
			if (isset($row['cat_name']))
205
			{
206
				$cat_name = $row['cat_name'];
207
				$this->db->sql_freeresult($result);
208
			}
209
			else
210
			{
211
				$sql = 'SELECT cat_name
212
					FROM ' . $this->smilies_category_table . "
213
						WHERE cat_lang = 'en'
214
						AND cat_id = $cat";
215
				$result = $this->db->sql_query_limit($sql, 1);
216
				$row = $this->db->sql_fetchrow($result);
217
				$cat_name = $row['cat_name'];
218
				$this->db->sql_freeresult($result);
219
			}
220
		}
221
222
		return $cat_name;
223
	}
224
225
	public function get_max_order()
226
	{
227
		// Get max order id...
228
		$sql = 'SELECT MAX(cat_order) AS maxi
229
			FROM ' . $this->smilies_category_table;
230
		$result = $this->db->sql_query($sql);
231
		$max = (int) $this->db->sql_fetchfield('maxi');
232
		$this->db->sql_freeresult($result);
233
234
		return $max;
235
	}
236
237
	public function get_max_id()
238
	{
239
		// Get max id...
240
		$sql = 'SELECT MAX(cat_id) AS id_max
241
			FROM ' . $this->smilies_category_table;
242
		$result = $this->db->sql_query($sql);
243
		$id_max = (int) $this->db->sql_fetchfield('id_max');
244
		$this->db->sql_freeresult($result);
245
246
		return $id_max;
247
	}
248
249
	public function get_first_order()
250
	{
251
		// Get first order id...
252
		$sql = 'SELECT cat_order, cat_id
253
			FROM ' . $this->smilies_category_table . '
254
			ORDER BY cat_order ASC';
255
		$result = $this->db->sql_query_limit($sql, 1);
256
		$first = (int) $this->db->sql_fetchfield('cat_id');
257
		$this->db->sql_freeresult($result);
258
259
		return $first;
260
	}
261
262
	public function get_cat_id($id)
263
	{
264
		$sql = 'SELECT category
265
			FROM ' . SMILIES_TABLE . '
266
				WHERE smiley_id = ' . $id;
267
		$result = $this->db->sql_query($sql);
268
		$cat = (int) $this->db->sql_fetchfield('category');
269
		$this->db->sql_freeresult($result);
270
271
		return $cat;
272
	}
273
274
	public function get_cat_nb($id)
275
	{
276
		$sql = 'SELECT cat_nb
277
			FROM ' . $this->smilies_category_table . '
278
				WHERE cat_id = ' . (int) $id;
279
		$result = $this->db->sql_query($sql);
280
		$cat_nb = (int) $this->db->sql_fetchfield('cat_nb');
281
		$this->db->sql_freeresult($result);
282
283
		return $cat_nb;
284
	}
285
286
	public function extract_list_categories($cat)
287
	{
288
		$title = '';
289
		$cat_order = $i = 0;
290
		$lang = (string) $this->user->lang_name;
291
		$sql = $this->db->sql_build_query('SELECT', [
292
			'SELECT'	=> '*',
293
			'FROM'		=> [$this->smilies_category_table => ''],
294
			'WHERE'		=> "cat_nb <> 0 AND cat_lang = '$lang'",
295
			'ORDER_BY'	=> 'cat_order ASC',
296
		]);
297
		$result = $this->db->sql_query($sql);
298
		while ($row = $this->db->sql_fetchrow($result))
299
		{
300
			$actual_cat = (int) $row['cat_id'] === $cat;
301
			$this->template->assign_block_vars('categories', [
302
				'CLASS'			=> $actual_cat ? 'cat-active' : 'cat-inactive',
303
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
304
				'CAT_NAME'		=> $row['cat_name'] ?: $row['cat_title'],
305
				'CAT_ORDER'		=> $row['cat_order'],
306
				'CAT_ID'		=> $row['cat_id'],
307
				'CAT_NB'		=> $row['cat_nb'],
308
				'U_CAT'			=> $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]),
309
			]);
310
			$i++;
311
312
			// Keep these values in memory
313
			$title = $actual_cat ? $row['cat_name'] : $title;
314
			$cat_order = $row['cat_order'];
315
		}
316
		$this->db->sql_freeresult($result);
317
318
		// Add the Unclassified category if not empty
319
		if ($nb = $this->smilies_count(0))
320
		{
321
			$this->template->assign_block_vars('categories', [
322
				'CLASS'			=> ($cat === 0) ? 'cat-active' : 'cat-inactive',
323
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
324
				'CAT_NAME'		=> $this->language->lang('SC_CATEGORY_DEFAUT'),
325
				'CAT_ORDER'		=> $cat_order + 1,
326
				'CAT_ID'		=> 0,
327
				'CAT_NB'		=> $nb,
328
				'U_CAT'			=> $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => 0]),
329
			]);
330
		}
331
332
		return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title;
333
	}
334
335
	public function set_order($action, $current_order)
336
	{
337
		$switch_order_id = 0;
338
		$max_order = $this->get_max_order();
339
		if ($current_order === 1 && $action === 'move_up')
340
		{
341
			return $switch_order_id;
342
		}
343
344
		if (($current_order === $max_order) && ($action === 'move_down'))
345
		{
346
			return $switch_order_id;
347
		}
348
349
		// on move_down, switch position with next order_id...
350
		// on move_up, switch position with previous order_id...
351
		$switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1;
352
353
		return $switch_order_id;
354
	}
355
356
	public function reset_first_cat($current_order, $switch_order_id)
357
	{
358
		$first = $this->get_first_order();
359
		if ($current_order === 1 || $switch_order_id === 1)
360
		{
361
			if ($this->get_cat_nb($first) > 0)
362
			{
363
				$this->config->set('smilies_category_nb', $first);
364
			}
365
		}
366
	}
367
368
	public function move_cat($id, $action)
369
	{
370
		// Get current order id and title...
371
		$sql = 'SELECT cat_order, cat_title
372
			FROM ' . $this->smilies_category_table . '
373
				WHERE cat_id = ' . $id;
374
		$result = $this->db->sql_query($sql);
375
		$row = $this->db->sql_fetchrow($result);
376
		$current_order = (int) $row['cat_order'];
377
		$title = $row['cat_title'];
378
		$this->db->sql_freeresult($result);
379
380
		$switch_order_id = $this->set_order($action, $current_order);
381
		if ($switch_order_id === 0)
382
		{
383
			return;
384
		}
385
386
		$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $current_order WHERE cat_order = $switch_order_id AND cat_id <> $id");
387
		$move_executed = (bool) $this->db->sql_affectedrows();
388
389
		// Only update the other entry too if the previous entry got updated
390
		if ($move_executed)
391
		{
392
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $switch_order_id WHERE cat_order = $current_order AND cat_id = $id");
393
		}
394
395
		$this->reset_first_cat($current_order, $switch_order_id);
396
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), [$title]);
397
	}
398
}
399