Completed
Push — master ( a0ceb6...422577 )
by Daniel
08:41
created

featured_member::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 12
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\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
use blitze\sitemaker\services\userlist;
14
15
/**
16
 * Featured Member Block
17
 */
18
class featured_member extends block
19
{
20
	/** @var \phpbb\cache\driver\driver_interface */
21
	protected $cache;
22
23
	/** @var \phpbb\config\config */
24
	protected $config;
25
26
	/** @var \phpbb\db\driver\driver_interface */
27
	protected $db;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var \blitze\sitemaker\services\profilefields */
33
	protected $profilefields;
34
35
	/** @var string */
36
	protected $phpbb_root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var string */
42
	protected $blocks_table;
43
44
	/** @var int */
45
	protected $cache_time;
46
47
	/** @var array */
48
	private $settings;
49
50
	/** @var array */
51
	private static $rotations = array(
52
		'hourly'	=> 'hour',
53
		'daily'		=> 'day',
54
		'weekly'	=> 'week',
55
		'monthly'	=> 'month'
56
	);
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param \phpbb\cache\driver\driver_interface		$cache					Cache driver interface
62
	 * @param \phpbb\config\config						$config					Config object
63
	 * @param \phpbb\db\driver\driver_interface			$db	 					Database connection
64
	 * @param \blitze\sitemaker\services\profilefields	$profilefields			Profile fields manager object
65
	 * @param \phpbb\user								$user					User object
66
	 * @param string									$phpbb_root_path		Path to the phpbb includes directory.
67
	 * @param string									$php_ext				php file extension
68
	 * @param string									$blocks_table			Name of blocks database table
69
	 * @param int										$cache_time
70
	 */
71 11
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, \blitze\sitemaker\services\profilefields $profilefields, $phpbb_root_path, $php_ext, $blocks_table, $cache_time = 3600)
72
	{
73 11
		$this->cache = $cache;
74 11
		$this->config = $config;
75 11
		$this->db = $db;
76 11
		$this->user = $user;
77 11
		$this->profilefields = $profilefields;
78 11
		$this->phpbb_root_path = $phpbb_root_path;
79 11
		$this->php_ext = $php_ext;
80 11
		$this->blocks_table = $blocks_table;
81 11
		$this->cache_time = $cache_time;
82 11
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87 1
	public function get_config(array $settings)
88
	{
89 1
		$rotation_options = $this->get_rotation_frequencies();
90 1
		$qtype_options = $this->get_query_types();
91 1
		$cpf_options = $this->profilefields->get_all_fields();
92
93
		return array(
94 1
			'legend1'		=> 'SETTINGS',
95 1
			'qtype'			=> array('lang' => 'QUERY_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $qtype_options, 'default' => 'recent', 'explain' => false),
96 1
			'rotation'		=> array('lang' => 'FREQUENCY', 'validate' => 'string', 'type' => 'select', 'options' => $rotation_options, 'default' => 'daily', 'explain' => false),
97 1
			'userlist'		=> array('lang' => 'FEATURED_MEMBER_IDS', 'validate' => 'string', 'type' => 'textarea:3:40', 'default' => '', 'explain' => true),
98
99 1
			'legend2'		=> 'CUSTOM_PROFILE_FIELDS',
100 1
			'show_cpf'		=> array('lang' => 'SELECT_PROFILE_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $cpf_options, 'default' => array(), 'explain' => true),
101 1
			'last_changed'	=> array('type' => 'hidden', 'default' => 0),
102 1
			'current_user'	=> array('type' => 'hidden', 'default' => 0),
103 1
		);
104
	}
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109 10
	public function display(array $bdata, $edit_mode = false, $loop_count = 0)
110
	{
111 10
		$this->settings = $this->get_settings($bdata);
112
113 10
		$change_user = $this->change_user();
114 10
		$block_title = $this->get_block_title($this->settings['qtype']);
115
116 10
		if (($row = $this->get_user_data($change_user)) === false)
117 10
		{
118 3
			userlist::update($this->settings);
119
120 3
			$bdata['settings'] = $this->settings;
121 3
			$bdata['hash'] = 0;
122
123
			// Prevent endless loop looking for valid user
124 3
			if ($loop_count < 3)
125 3
			{
126 3
				return $this->display($bdata, $edit_mode, ++$loop_count);
127
			}
128 1
			$row = array();
129 1
		}
130
131
		return array(
132 10
			'title'		=> $block_title,
133 10
			'content'	=> $this->display_user($bdata['bid'], $row, $change_user),
134 10
		);
135
	}
136
137
	/**
138
	 * @param bool $change_user
139
	 * @return array|false
140
	 */
141 10
	private function get_user_data($change_user)
142
	{
143
		$sql = 'SELECT user_id, username, user_colour, user_avatar, user_avatar_type, user_avatar_height, user_avatar_width, user_regdate, user_lastvisit, user_birthday, user_posts, user_rank
144 10
			FROM ' . USERS_TABLE . '
145 10
			WHERE ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER));
146
147 10
		$method = 'query_' . $this->settings['qtype'];
148
149 10
		if (is_callable(array($this, $method)))
150 10
		{
151 9
			call_user_func_array(array($this, $method), array(&$sql, $change_user));
152 9
		}
153
		else
154
		{
155 1
			return array();
156
		}
157
158 9
		$result = $this->db->sql_query_limit($sql, 1, 0, $this->get_cache_time($this->settings['qtype'], $this->settings['rotation']));
159 9
		$row = $this->db->sql_fetchrow($result);
160 9
		$this->db->sql_freeresult($result);
161
162 9
		return $row;
163
	}
164
165
	/**
166
	 * @param string $sql
167
	 * @param bool $change_user
168
	 */
169 7
	private function query_featured(&$sql, $change_user)
170
	{
171 7
		$sql .= ' AND user_id = ' . userlist::get_user_id($this->settings, $change_user);
172 7
	}
173
174
	/**
175
	 * @param string $sql
176
	 */
177 1
	private function query_recent(&$sql)
178
	{
179 1
		$sql .= ' AND user_posts > 0 ORDER BY user_regdate DESC';
180 1
	}
181
182
	/**
183
	 * @param string $sql
184
	 */
185 1
	private function query_posts(&$sql)
186
	{
187 1
		$sql .= ' AND user_posts > 0 ORDER BY user_posts DESC';
188 1
	}
189
190
	/**
191
	 * @param string $query_type
192
	 * @param string $rotation
193
	 * @return int
194
	 */
195 9
	private function get_cache_time($query_type, $rotation)
196
	{
197 9
		return ($rotation !== 'pageload' || in_array($query_type, array('posts', 'recent'))) ? $this->cache_time : 0;
198
	}
199
200
	/**
201
	 * @return bool
202
	 */
203 10
	private function change_user()
204
	{
205 10
		$change = false;
206 10
		if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']]))
207 10
		{
208 9
			$this->settings['last_changed'] = time();
209 9
			$change = true;
210 9
		}
211
212 10
		return $change;
213
	}
214
215
	/**
216
	 */
217 8
	private function explain_view()
218
	{
219 8
		$query_type = $this->settings['qtype'];
220 8
		$rotation = $this->settings['rotation'];
221
222 8
		$this->ptemplate->assign_vars(array(
223 8
			'QTYPE_EXPLAIN'		=> ($query_type == 'posts' || $query_type == 'recent') ? $this->user->lang('QTYPE_' . strtoupper($query_type)) : '',
224 8
			'TITLE_EXPLAIN'		=> ($rotation != 'pageload') ? $this->user->lang(strtoupper($rotation) . '_MEMBER') : '',
225 8
		));
226 8
	}
227
228
	/**
229
	 * @param array $bdata
230
	 * @return array
231
	 */
232 10
	private function get_settings(array $bdata)
233
	{
234 10
		$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']);
235 10
		$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings'];
236 10
		$settings['hash'] = $bdata['hash'];
237
238 10
		return $settings;
239
	}
240
241
	/**
242
	 * @param int $bid
243
	 * @param bool $change_user
244
	 */
245 10
	private function save_settings($bid, $change_user)
246
	{
247 10
		if ($change_user && ($this->settings['rotation'] !== 'pageload' || $this->settings['qtype'] === 'featured'))
248 10
		{
249 8
			$settings = $this->settings;
250 8
			unset($settings['hash']);
251
			$sql_data = array(
252 8
				'settings'	=> json_encode($settings)
253 8
			);
254 8
			$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid);
255 8
			$this->cache->put('pt_block_data_' . $bid, $this->settings);
256 8
		}
257 10
	}
258
259
	/**
260
	 * @param int   $block_id
261
	 * @param array $row
262
	 * @param bool  $change_user
263
	 * @return string
264
	 */
265 10
	private function display_user($block_id, array $row, $change_user)
266
	{
267 10
		$this->save_settings($block_id, $change_user);
268
269 10
		$html = '';
270 10
		if (sizeof($row))
271 10
		{
272 8
			$this->explain_view();
273
274 8
			$tpl_data = $this->get_template_data($row);
275 8
			$this->ptemplate->assign_vars($tpl_data['row']);
276 8
			$this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']);
277 8
			unset($tpl_data);
278
279 8
			$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block');
280 8
		}
281
282 10
		return $html;
283
	}
284
285
	/**
286
	 * @param array $row
287
	 * @return array
288
	 */
289 8
	private function get_template_data(array $row)
290
	{
291 8
		$date_format = $this->user->lang('DATE_FORMAT');
292 8
		$username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']);
293 8
		$rank = phpbb_get_user_rank($row, $row['user_posts']);
294
295 8
		$tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']);
296
297 8
		$tpl_data['row'] = array_merge($tpl_data['row'], array(
298 8
			'USERNAME'			=> $username,
299 8
			'AVATAR_IMG'		=> phpbb_get_user_avatar($row),
300 8
			'POSTS_PCT'			=> sprintf($this->user->lang('POST_PCT'), $this->calculate_percent_posts($row['user_posts'])),
301 8
			'L_VIEW_PROFILE'	=> sprintf($this->user->lang('VIEW_USER_PROFILE'), $username),
302 8
			'JOINED'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
303 8
			'VISITED'			=> $this->get_last_visit_date($row['user_lastvisit'], $date_format),
304 8
			'POSTS'				=> $row['user_posts'],
305 8
			'RANK_TITLE'		=> $rank['title'],
306 8
			'RANK_IMG'			=> $rank['img'],
307 8
			'U_PROFILE'			=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
308 8
			'U_SEARCH_USER'		=> append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&amp;sr=posts"),
309 8
		));
310
311 8
		return $tpl_data;
312
	}
313
314
	/**
315
	 * @param int $user_posts
316
	 * @return int|mixed
317
	 */
318 8
	private function calculate_percent_posts($user_posts)
319
	{
320 8
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
321
	}
322
323
	/**
324
	 * @param int $last_visited
325
	 * @param string $date_format
326
	 * @return string
327
	 */
328 8
	private function get_last_visit_date($last_visited, $date_format)
329
	{
330 8
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
331
	}
332
333
	/**
334
	 * @param string $qtype
335
	 * @return string
336
	 */
337 10
	private function get_block_title($qtype)
338
	{
339 10
		$qtypes = $this->get_query_types();
340 10
		return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER';
341
	}
342
343
	/**
344
	 * @return array
345
	 */
346 1
	private function get_rotation_frequencies()
347
	{
348
		return array(
349 1
			'pageload'	=> 'ROTATE_PAGELOAD',
350 1
			'hourly'	=> 'ROTATE_HOURLY',
351 1
			'daily'		=> 'ROTATE_DAILY',
352 1
			'weekly'	=> 'ROTATE_WEEKLY',
353 1
			'monthly'	=> 'ROTATE_MONTHLY',
354 1
		);
355
	}
356
357
	/**
358
	 * @return array
359
	 */
360 11
	private function get_query_types()
361
	{
362
		return array(
363 11
			'recent'	=> 'RECENT_MEMBER',
364 11
			'posts'		=> 'POSTS_MEMBER',
365 11
			'featured'	=> 'FEATURED_MEMBER',
366 11
		);
367
	}
368
}
369