Passed
Push — 1.1.0 ( d0bccc...c7508f )
by Sylver
03:00
created

main::extract_categories()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 20
nop 1
dl 0
loc 42
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
*
4
* @package		Breizh Smilies Categories Extension
5
* @copyright	(c) 2020 Sylver35  https://breizhcode.com
6
* @license		http://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 phpbb\request\request;
14
use phpbb\config\config;
15
use phpbb\controller\helper;
16
use phpbb\path_helper;
17
use phpbb\db\driver\driver_interface as db;
18
use phpbb\template\template;
19
use phpbb\user;
20
use phpbb\language\language;
21
use phpbb\pagination;
22
23
class main
24
{
25
	/* @var \sylver35\smiliescat\core\category */
26
	protected $category;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\config\config */
32
	protected $config;
33
34
	/* @var \phpbb\controller\helper */
35
	protected $helper;
36
37
	/* @var \phpbb\path_helper */
38
	protected $path_helper;
39
40
	/** @var \phpbb\db\driver\driver_interface */
41
	protected $db;
42
43
	/** @var \phpbb\template\template */
44
	protected $template;
45
46
	/** @var \phpbb\user */
47
	protected $user;
48
49
	/** @var \phpbb\language\language */
50
	protected $language;
51
52
	/** @var \phpbb\pagination */
53
	protected $pagination;
54
55
	/** @var string phpBB root path */
56
	protected $root_path;
57
58
	/**
59
	 * The database tables
60
	 *
61
	 * @var string */
62
	protected $smilies_category_table;
63
64
	/**
65
	 * Constructor
66
	 */
67
	public function __construct(category $category, request $request, config $config, helper $helper, path_helper $path_helper, db $db, template $template, user $user, language $language, pagination $pagination, $root_path, $smilies_category_table)
68
	{
69
		$this->category = $category;
70
		$this->request = $request;
71
		$this->config = $config;
72
		$this->helper = $helper;
73
		$this->path_helper = $path_helper;
74
		$this->db = $db;
75
		$this->template = $template;
76
		$this->user = $user;
77
		$this->language = $language;
78
		$this->pagination = $pagination;
79
		$this->root_path = $root_path;
80
		$this->smilies_category_table = $smilies_category_table;
81
	}
82
83
	/**
84
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
85
	 */
86
	public function popup_smilies_category()
87
	{
88
		$start = $this->request->variable('start', 0);
89
		$cat = $this->request->variable('select', -1);
90
		$cat = ($cat == -1) ? $this->config['smilies_category_nb'] : $cat;
91
		$count = $this->category->smilies_count($cat);
92
		$url = $this->helper->route('sylver35_smiliescat_smilies_pop');
93
94
		$sql = $this->db->sql_build_query('SELECT', array(
95
			'SELECT'	=> 'smiley_url, MIN(smiley_id) AS smiley_id, MIN(code) AS code, MIN(smiley_order) AS min_smiley_order, MIN(smiley_width) AS smiley_width, MIN(smiley_height) AS smiley_height, MIN(emotion) AS emotion',
96
			'FROM'		=> array(SMILIES_TABLE => ''),
97
			'WHERE'		=> "category = $cat",
98
			'GROUP_BY'	=> 'smiley_url',
99
			'ORDER_BY'	=> 'min_smiley_order ASC',
100
		));
101
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
102
		while ($row = $this->db->sql_fetchrow($result))
103
		{
104
			$this->template->assign_block_vars('smilies', array(
105
				'SMILEY_CODE'		=> $row['code'],
106
				'SMILEY_EMOTION'	=> $row['emotion'],
107
				'SMILEY_WIDTH'		=> $row['smiley_width'],
108
				'SMILEY_HEIGHT'		=> $row['smiley_height'],
109
				'SMILEY_SRC'		=> generate_board_url() . '/' . $this->config['smilies_path'] . '/' . $row['smiley_url'],
110
			));
111
		}
112
		$this->db->sql_freeresult($result);
113
114
		$start = $this->pagination->validate_start($start, (int) $this->config['smilies_per_page_cat'], $count);
115
		$this->pagination->generate_template_pagination("{$url}?select={$cat}", 'pagination', 'start', $count, (int) $this->config['smilies_per_page_cat'], $start);
116
117
		$title = $this->extract_categories($cat);
118
		$data = $this->category->get_version();
119
		$this->template->assign_vars(array(
120
			'U_SELECT_CAT'		=> $url,
121
			'LIST_CATEGORY'		=> $this->category->select_categories($cat),
122
			'POPUP_TITLE'		=> $this->language->lang('SC_CATEGORY_IN', $title),
123
			'SC_VERSION'		=> $this->language->lang('SC_VERSION_COPY', $data['homepage'], $data['version']),
124
		));
125
126
		page_header($this->language->lang('SC_CATEGORY_IN', $title));
127
128
		$this->template->set_filenames(array(
129
			'body' => '@sylver35_smiliescat/smilies_category.html')
130
		);
131
132
		page_footer();
133
	}
134
	
135
	private function extract_categories($cat)
136
	{
137
		$title = '';
138
		$cat_order = $i = 0;
139
		$lang = $this->user->lang_name;
140
		$sql = $this->db->sql_build_query('SELECT', array(
141
			'SELECT'	=> '*',
142
			'FROM'		=> array($this->smilies_category_table => ''),
143
			'WHERE'		=> "cat_lang = '$lang'",
144
			'ORDER_BY'	=> 'cat_order ASC',
145
		));
146
		$result = $this->db->sql_query($sql);
147
		while ($row = $this->db->sql_fetchrow($result))
148
		{
149
			$active = ($row['cat_id'] == $cat) ? true : false;
150
			$this->template->assign_block_vars('categories', array(
151
				'CLASS'			=> ($active) ? 'cat-active' : 'cat-inactive',
152
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
153
				'CAT_ID'		=> $row['cat_id'],
154
				'CAT_ORDER'		=> $row['cat_order'],
155
				'CAT_NAME'		=> $row['cat_name'],
156
				'CAT_NB'		=> $row['cat_nb'],
157
			));
158
			$i++;
159
			$title = ($active) ? $row['cat_name'] : $title;
160
			$cat_order = $row['cat_order'];
161
		}
162
		$this->db->sql_freeresult($result);
163
164
		// Add the Unclassified category
165
		$unclassified = $this->language->lang('SC_CATEGORY_DEFAUT');
166
		$un_active = ($cat == 0) ? true : false;
167
		$this->template->assign_block_vars('categories', array(
168
			'CLASS'			=> ($un_active) ? 'cat-active' : 'cat-inactive',
169
			'SEPARATE'		=> ($i > 0) ? ' - ' : '',
170
			'CAT_ID'		=> 0,
171
			'CAT_ORDER'		=> $cat_order + 1,
172
			'CAT_NAME'		=> $unclassified,
173
			'CAT_NB'		=> $this->category->smilies_count(0),
174
		));
175
176
		return ($un_active) ? $unclassified : $title;
177
	}
178
}
179