main   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 308
Duplicated Lines 14.61 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 1
dl 45
loc 308
rs 9.36
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B display_confirm() 0 35 6
C display_stats() 0 84 9
C exec_action() 45 72 13
A set_page_url() 0 4 1
B _orphan_files() 0 45 8

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
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\controller\acp;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class main extends helper
16
{
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/** @var \phpbb\language\language */
21
	protected $language;
22
23
	/** @var \phpbb\request\request */
24
	protected $request;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var string Custom form action */
30
	protected $u_action;
31
32
	/**
33
	* Constructor
34
	*
35
	* @param \phpbb\db\driver\driver_interface 		$db			Database object
36
	* @param \phpbb\language\language				$language	Language object
37
	* @param \phpbb\request\request					$request	Request object
38
	* @param \phpbb\template\template				$template	Template object
39
	*/
40
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\template\template $template)
41
	{
42
		$this->db			= $db;
43
		$this->language		= $language;
44
		$this->template		= $template;
45
		$this->request		= $request;
46
	}
47
48
	/**
49
	* Display confirm box
50
	*
51
	* @param	string $action Requested action
52
	* @return	null
53
	*/
54
	public function display_confirm($action)
55
	{
56
		switch ($action)
57
		{
58
			case 'votes':
59
				$confirm = true;
60
				$confirm_lang = 'DIR_RESET_VOTES_CONFIRM';
61
				break;
62
63
			case 'comments':
64
				$confirm = true;
65
				$confirm_lang = 'DIR_RESET_COMMENTS_CONFIRM';
66
				break;
67
68
			case 'clicks':
69
				$confirm = true;
70
				$confirm_lang = 'DIR_RESET_CLICKS_CONFIRM';
71
				break;
72
73
			case 'orphans':
74
				$confirm = true;
75
				$confirm_lang = 'DIR_DELETE_ORPHANS';
76
				break;
77
78
			default:
79
				$confirm = false;
80
		}
81
82
		if ($confirm)
83
		{
84
			confirm_box(false, $this->language->lang($confirm_lang), build_hidden_fields(array(
85
				'action'	=> $action,
86
			)));
87
		}
88
	}
89
90
	/**
91
	* Display phpBB Directory statistics
92
	*
93
	* @return null
94
	*/
95
	public function display_stats()
96
	{
97
		// Count number of categories
98
		$sql = 'SELECT COUNT(cat_id) AS nb_cats
99
			FROM ' . $this->categories_table;
100
		$result = $this->db->sql_query($sql);
101
		$total_cats = (int) $this->db->sql_fetchfield('nb_cats');
102
		$this->db->sql_freeresult($result);
103
104
		// Cont number of links
105
		$sql = 'SELECT link_id, link_active
106
			FROM ' . $this->links_table;
107
		$result = $this->db->sql_query($sql);
108
		$total_links = $waiting_links = 0;
109
		while ($row = $this->db->sql_fetchrow($result))
110
		{
111
			$total_links++;
112
113
			if (!$row['link_active'])
114
			{
115
				$waiting_links++;
116
			}
117
		}
118
		$this->db->sql_freeresult($result);
119
120
		// Comments number calculating
121
		$sql = 'SELECT COUNT(comment_id) AS nb_comments
122
			FROM ' . $this->comments_table;
123
		$result = $this->db->sql_query($sql);
124
		$total_comments = (int) $this->db->sql_fetchfield('nb_comments');
125
		$this->db->sql_freeresult($result);
126
127
		// Votes number calculating
128
		$sql = 'SELECT COUNT(vote_id) AS nb_votes
129
			FROM ' . $this->votes_table;
130
		$result = $this->db->sql_query($sql);
131
		$total_votes = (int) $this->db->sql_fetchfield('nb_votes');
132
		$this->db->sql_freeresult($result);
133
134
		// Click number calculating
135
		$sql = 'SELECT SUM(link_view) AS nb_clicks
136
			FROM ' . $this->links_table;
137
		$result = $this->db->sql_query($sql);
138
		$total_clicks = (int) $this->db->sql_fetchfield('nb_clicks');
139
		$this->db->sql_freeresult($result);
140
141
		$banners_dir_size = 0;
142
143
		$banners_path = $this->get_banner_path();
144
145
		if ($banners_dir = @opendir($banners_path))
146
		{
147
			while (($file = readdir($banners_dir)) !== false)
148
			{
149
				if ($file[0] != '.' && $file[0] != '..' && strpos($file, 'index.') === false && strpos($file, '.db') === false)
150
				{
151
					$banners_dir_size += filesize($banners_path . $file);
152
				}
153
			}
154
			closedir($banners_dir);
155
156
			$banners_dir_size = get_formatted_filesize($banners_dir_size);
157
		}
158
		else
159
		{
160
			// Couldn't open banners dir.
161
			$banners_dir_size = $this->language->lang('NOT_AVAILABLE');
162
		}
163
164
		$total_orphan = $this->_orphan_files();
165
166
		$this->template->assign_vars(array(
167
			'U_ACTION'			=> $this->u_action,
168
169
			'TOTAL_CATS'		=> $total_cats,
170
			'TOTAL_LINKS'		=> $total_links-$waiting_links,
171
			'WAITING_LINKS'		=> $waiting_links,
172
			'TOTAL_COMMENTS'	=> $total_comments,
173
			'TOTAL_VOTES'		=> $total_votes,
174
			'TOTAL_CLICKS'		=> $total_clicks,
175
			'TOTAL_ORPHANS'		=> $total_orphan,
176
			'BANNERS_DIR_SIZE'	=> $banners_dir_size,
177
		));
178
	}
179
180
	/**
181
	* Execute action requested
182
	*
183
	* @param	string $action Requested action
184
	* @return	null
185
	*/
186
	public function exec_action($action)
187
	{
188
		switch ($action)
189
		{
190 View Code Duplication
			case 'votes':
191
				switch ($this->db->get_sql_layer())
192
				{
193
					case 'sqlite':
194
					case 'firebird':
195
						$this->db->sql_query('DELETE FROM ' . $this->votes_table);
196
					break;
197
198
					default:
199
						$this->db->sql_query('TRUNCATE TABLE ' . $this->votes_table);
200
					break;
201
				}
202
203
				$sql = 'UPDATE ' . $this->links_table . '
204
					SET link_vote = 0, link_note = 0';
205
				$this->db->sql_query($sql);
206
207
				if ($this->request->is_ajax())
208
				{
209
					trigger_error('DIR_RESET_VOTES_SUCCESS');
210
				}
211
			break;
212
213 View Code Duplication
			case 'comments':
214
				switch ($this->db->get_sql_layer())
215
				{
216
					case 'sqlite':
217
					case 'firebird':
218
						$this->db->sql_query('DELETE FROM ' . $this->comments_table);
219
					break;
220
221
					default:
222
						$this->db->sql_query('TRUNCATE TABLE ' . $this->comments_table);
223
					break;
224
				}
225
226
				$sql = 'UPDATE ' . $this->links_table . '
227
					SET link_comment = 0';
228
				$this->db->sql_query($sql);
229
230
				if ($this->request->is_ajax())
231
				{
232
					trigger_error('DIR_RESET_COMMENTS_SUCCESS');
233
				}
234
235
				break;
236
237
			case 'clicks':
238
				$sql = 'UPDATE ' . $this->links_table . '
239
					SET link_view = 0';
240
				$this->db->sql_query($sql);
241
242
				if ($this->request->is_ajax())
243
				{
244
					trigger_error('DIR_RESET_CLICKS_SUCCESS');
245
				}
246
			break;
247
248
			case 'orphans':
249
				$this->_orphan_files(true);
250
251
				if ($this->request->is_ajax())
252
				{
253
					trigger_error('DIR_DELETE_ORPHANS_SUCCESS');
254
				}
255
			break;
256
		}
257
	}
258
259
	/**
260
	* Set page url
261
	*
262
	* @param	string $u_action Custom form action
263
	* @return	null
264
	* @access	public
265
	*/
266
	public function set_page_url($u_action)
267
	{
268
		$this->u_action = $u_action;
269
	}
270
271
	/**
272
	* Get orphan banners
273
	*
274
	* @param	bool		$delete	True if we want to delete banners, else false
275
	* @return	null|int	Number of orphan files, else null
276
	*/
277
	private function _orphan_files($delete = false)
278
	{
279
		$banner_path = $this->get_banner_path();
280
		$imglist = filelist($banner_path);
281
		$physical_files = $logical_files = $orphan_files = array();
282
283
		if (!empty($imglist['']))
284
		{
285
			$imglist = array_values($imglist);
286
			$imglist = $imglist[0];
287
288
			foreach ($imglist as $img)
289
			{
290
				$physical_files[] = $img;
291
			}
292
			$sql = 'SELECT link_banner FROM ' . $this->links_table . '
293
				WHERE link_banner <> \'\'';
294
			$result = $this->db->sql_query($sql);
295
296
			while ($row = $this->db->sql_fetchrow($result))
297
			{
298
				if (!preg_match('/^(http:\/\/|https:\/\/|ftp:\/\/|ftps:\/\/|www\.).+/si', $row['link_banner']))
299
				{
300
					$logical_files[] = basename($row['link_banner']);
301
				}
302
			}
303
			$this->db->sql_freeresult($result);
304
305
			$orphan_files = array_diff($physical_files, $logical_files);
306
		}
307
308
		if (!$delete)
309
		{
310
			return count($orphan_files);
311
		}
312
313
		$dh = @opendir($banner_path);
314
		while (($file = readdir($dh)) !== false)
315
		{
316
			if (in_array($file, $orphan_files))
317
			{
318
				@unlink($this->get_banner_path($file));
319
			}
320
		}
321
	}
322
}
323