Completed
Push — develop ( 0afac9...6f1d60 )
by Daniel
22:03 queued 11:00
created

data::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 9
crap 1

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 sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\forum;
11
12
use blitze\sitemaker\services\forum\query_builder;
13
14
class data extends query_builder
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var \phpbb\content_visibility */
23
	protected $content_visibility;
24
25
	/** @var \phpbb\db\driver\driver_interface */
26
	protected $db;
27
28
	/** @var \phpbb\language\language */
29
	protected $translator;
30
31
	/** @var \phpbb\user */
32
	protected $user;
33
34
	/** @var string */
35
	protected $phpbb_root_path;
36
37
	/** @var string */
38
	protected $php_ext;
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param \phpbb\auth\auth					$auth					Auth object
44
	 * @param \phpbb\config\config				$config					Config object
45
	 * @param \phpbb\content_visibility			$content_visibility		Content visibility
46
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
47
	 * @param \phpbb\language\language			$translator				Language object
48
	 * @param \phpbb\user						$user					User object
49
	 * @param string							$phpbb_root_path		Path to the phpbb includes directory.
50
	 * @param string							$php_ext				php file extension
51
	 * @param integer							$cache_time				Cache results for 3 hours by default
52
	 */
53 26
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $translator, \phpbb\user $user, $phpbb_root_path, $php_ext, $cache_time = 10800)
54
	{
55 26
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
56
57 26
		$this->auth = $auth;
58 26
		$this->config = $config;
59 26
		$this->content_visibility = $content_visibility;
60 26
		$this->db = $db;
61 26
		$this->translator = $translator;
62 26
		$this->user = $user;
63 26
		$this->phpbb_root_path = $phpbb_root_path;
64 26
		$this->php_ext = $php_ext;
65 26
	}
66
67
	/**
68
	 * Get topics count
69
	 *
70
	 * @return integer
71
	 */
72
	public function get_topics_count()
73
	{
74 12
		$sql_array = array(
75
			'SELECT'	=> 'COUNT(*) AS total_topics',
76 12
			'FROM'		=> $this->store['sql_array']['FROM'],
77 12
			'WHERE'		=> $this->store['sql_array']['WHERE'],
78
		);
79 12
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
80
		$result = $this->db->sql_query($sql);
81 10
		$total_topics = $this->db->sql_fetchfield('total_topics');
82
		$this->db->sql_freeresult($result);
83 10
84 10
		return $total_topics;
85 10
	}
86 10
87 10
	/**
88 12
	 * Get topic data
89
	 *
90 12
	 * @param mixed|false $limit
91
	 * @param int $start
92
	 * @return array
93
	 */
94
	public function get_topic_data($limit = false, $start = 0)
95
	{
96
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
97
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
98
99
		while ($row = $this->db->sql_fetchrow($result))
100
		{
101
			$this->store['topic'][$row['topic_id']] = $row;
102
103 10
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
104
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
105 10
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
106 10
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
107
		}
108 10
		$this->db->sql_freeresult($result);
109 10
110
		return $this->store['topic'];
111 9
	}
112 9
113
	/**
114 9
	 * Get post data
115 9
	 *
116 9
	 * @param mixed|false $topic_first_or_last_post (first|last)
117 9
	 * @param array $post_ids
118 9
	 * @param bool|false $limit
119 9
	 * @param int $start
120 10
	 * @param array $sql_array
121
	 * @return array
122 10
	 */
123
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
124
	{
125
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array));
126
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
127
128
		$post_data = array();
129
		while ($row = $this->db->sql_fetchrow($result))
130
		{
131
			$post_data[$row['topic_id']][$row['post_id']] = $row;
132
			$this->store['poster_ids'][] = $row['poster_id'];
133
			$this->store['poster_ids'][] = $row['post_edit_user'];
134 7
			$this->store['poster_ids'][] = $row['post_delete_user'];
135
			$this->store['attachments'][] = $row['post_id'];
136 7
		}
137
		$this->db->sql_freeresult($result);
138 7
139 7
		return $post_data;
140 7
	}
141 6
142 6
	/**
143
	 * Get attachments...
144 6
	 *
145
	 * @param int $forum_id
146 6
	 * @param array $allowed_extensions
147 6
	 * @param bool $exclude_in_message
148 6
	 * @param string $order_by
149 6
	 * @return array
150 7
	 */
151
	public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC')
152 7
	{
153
		$this->store['attachments'] = array_filter($this->store['attachments']);
154
155
		$attachments = array();
156
		if ($this->_attachments_allowed($forum_id))
157
		{
158
			$sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by);
159
			$result = $this->db->sql_query($sql);
160
161 5
			while ($row = $this->db->sql_fetchrow($result))
162
			{
163 5
				$attachments[$row['post_msg_id']][] = $row;
164 5
			}
165
			$this->db->sql_freeresult($result);
166
		}
167
		$this->store['attachments'] = array();
168 5
169
		return $attachments;
170 5
	}
171
172
	/**
173
	 * Get topic tracking info
174
	 *
175
	 * @param int $forum_id
176
	 * @return array
177
	 */
178
	public function get_topic_tracking_info($forum_id = 0)
179 3
	{
180
		if (!sizeof($this->store['tracking']))
181 3
		{
182
			return array();
183
		}
184
185
		$tracking_info = $this->_get_tracking_info();
186
187
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
188
	}
189
190
	/**
191
	 * Returns an array of topic first post or last post ids
192
	 *
193
	 * @param string $first_or_last_post
194
	 * @return array
195
	 */
196
	public function get_topic_post_ids($first_or_last_post = 'first')
197
	{
198
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
199
	}
200
201
	/**
202
	 * Returns an array of topic first post or last post ids
203
	 */
204
	public function get_posters_info()
205 10
	{
206
		$this->store['poster_ids'] = array_filter(array_unique($this->store['poster_ids']));
207 10
208
		if (!sizeof($this->store['poster_ids']))
209 10
		{
210 10
			return array();
211 10
		}
212 10
213 10
		return $this->_get_user_data();
214
	}
215 10
216
	/**
217 10
	 * @param mixed $topic_first_or_last_post
218 10
	 * @param array $post_ids
219
	 * @param array $sql_array
220 10
	 * @return array
221
	 */
222
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
223
	{
224
		$sql_array = array_merge_recursive(
225
			array(
226
				'SELECT'	=> array('p.*'),
227
				'FROM'		=> array(POSTS_TABLE => 'p'),
228 10
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
229
				'ORDER_BY'	=> 'p.topic_id, p.post_time ASC',
230 10
			),
231
			$sql_array
232 10
		);
233 10
234 2
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
235 2
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
236 8
237 8
		return $sql_array;
238 3
	}
239 3
240
	/**
241 10
	 * @param array $post_ids
242
	 * @param string $topic_first_or_last_post
243 10
	 * @return array
244
	 */
245
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
246
	{
247
		$sql_where = array();
248
249
		if (sizeof($post_ids))
250 3
		{
251
			$sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids);
252 3
		}
253
		else if (sizeof($this->store['topic']))
254
		{
255 3
			$this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post);
256 3
		}
257 3
258 3
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
259
260
		return $sql_where;
261
	}
262
263 5
	/**
264
	 * @param array $sql_where
265 5
	 * @param string $topic_first_or_last_post
266 5
	 */
267 5
	private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post)
268 4
	{
269 4
		$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
270 1
271 1
		if ($topic_first_or_last_post)
272 1
		{
273 1
			$sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post));
274
		}
275 5
	}
276
277
	/**
278
	 * @return array
279
	 */
280
	private function _get_tracking_info()
281
	{
282 5
		$info = array();
283
		if ($this->_can_track_by_lastread())
284 5
		{
285 5
			$info = $this->_build_tracking_info('get_topic_tracking');
286
		}
287 5
		else if ($this->_can_track_anonymous())
288 5
		{
289
			$info = $this->_build_tracking_info('get_complete_topic_tracking');
290 5
		}
291
292
		return $info;
293
	}
294
295
	/**
296 5
	 * @param string $function
297
	 * @return array
298 5
	 */
299
	private function _build_tracking_info($function)
300
	{
301
		$tracking_info = array();
302
		foreach ($this->store['tracking'] as $fid => $forum)
303
		{
304 1
			$tracking_info[$fid] = call_user_func_array($function, array($fid, $forum['topic_list'], &$this->store['topic'], array($fid => $forum['mark_time'])));
305
		}
306 1
307
		return $tracking_info;
308
	}
309
310
	/**
311
	 * @return bool
312
	 */
313 7
	private function _can_track_by_lastread()
314
	{
315 7
		return ($this->config['load_db_lastread'] && $this->user->data['is_registered']) ? true : false;
316
	}
317
318
	/**
319
	 * @return bool
320
	 */
321
	private function _can_track_anonymous()
322 6
	{
323
		return ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) ? true : false;
324 6
	}
325
326
	/**
327
	 * @param int $forum_id
328
	 * @return bool
329
	 */
330
	private function _attachments_allowed($forum_id)
331
	{
332
		return ($this->store['attachments'] && $this->_user_can_download_attachments($forum_id)) ? true : false;
333 6
	}
334
335
	/**
336 6
	 * @param int $forum_id
337 6
	 * @return bool
338 6
	 */
339 6
	private function _user_can_download_attachments($forum_id)
340 6
	{
341
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
342
	}
343
344
	/**
345
	 * @param array $allowed_extensions
346
	 * @param bool $exclude_in_message
347
	 * @param string $order_by
348
	 * @return string
349
	 */
350
	private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by)
351
	{
352
		return 'SELECT *
353
			FROM ' . ATTACHMENTS_TABLE . '
354
			WHERE ' . $this->db->sql_in_set('post_msg_id', $this->store['attachments']) .
355
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
356
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
357
			ORDER BY ' . $order_by;
358
	}
359
360
	/**
361
	 * @return array
362
	 */
363
	private function _get_user_data()
364
	{
365
		$sql = 'SELECT *
366
			FROM ' . USERS_TABLE . '
367
			WHERE ' . $this->db->sql_in_set('user_id', $this->store['poster_ids']);
368
		$result = $this->db->sql_query($sql);
369
370
		$user_cache = array();
371
		while ($row = $this->db->sql_fetchrow($result))
372
		{
373
			$poster_id = $row['user_id'];
374
375
			$user_cache[$poster_id] = array(
376
				'user_type'					=> $row['user_type'],
377
				'user_inactive_reason'		=> $row['user_inactive_reason'],
378
379
				'joined'			=> $this->user->format_date($row['user_regdate'], 'M d, Y'),
380
				'posts'				=> $row['user_posts'],
381
				'warnings'			=> (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
382
				'allow_pm'			=> $row['user_allow_pm'],
383
				'avatar'			=> ($this->user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '',
384
385
				'contact_user' 		=> $this->translator->lang('CONTACT_USER', get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['username'])),
386
				'search'			=> ($this->auth->acl_get('u_search')) ? append_sid("{$this->phpbb_root_path}search.$this->php_ext", "author_id=$poster_id&amp;sr=posts") : '',
387
388
				'username'			=> get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
389
				'username_full'		=> get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
390
				'user_colour'		=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
391
				'user_profile'		=> get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
392
			);
393
394
			$user_cache[$poster_id] += $this->_get_user_rank($row);
395
			$user_cache[$poster_id] += $this->_get_user_email($row);
396
		}
397
		$this->db->sql_freeresult($result);
398
399
		return $user_cache;
400
	}
401
402
	/**
403
	 * @param array $row
404
	 */
405
	private function _get_user_rank(array $row)
406
	{
407
		if (!function_exists('phpbb_get_user_rank'))
408
		{
409
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
410
		}
411
412
		$user_rank_data = phpbb_get_user_rank($row, $row['user_posts']);
413
414
		if (!empty($user_rank_data))
415
		{
416
			return array(
417
				'rank_title'		=> $user_rank_data['title'],
418
				'rank_image'		=> $user_rank_data['img'],
419
				'rank_image_src'	=> $user_rank_data['img_src'],
420
			);
421
		}
422
		else
423
		{
424
			return array(
425
				'rank_title'		=> '',
426
				'rank_image'		=> '',
427
				'rank_image_src'	=> '',
428
			);
429
		}
430
	}
431
432
	/**
433
	 * @param array $row
434
	 */
435
	private function _get_user_email(array $row)
436
	{
437
		$email = '';
438
		if ((!empty($row['user_allow_viewemail']) && $this->auth->acl_get('u_sendemail')) || $this->auth->acl_get('a_email'))
439
		{
440
			$email = ($this->config['board_email_form'] && $this->config['email_enable']) ? append_sid("{$this->phpbb_root_path}memberlist.$this->php_ext", 'mode=email&amp;u=' . $row['user_id']) : (($this->config['board_hide_emails'] && !$this->auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
441
		}
442
443
		return array('email' => $email);
444
	}
445
}
446