Completed
Push — master ( db8e66...00afbe )
by Daniel
09:03
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 0
Metric Value
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
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 10
		$sql_array = $this->get_sql_array();
144 10
		$method = 'query_' . $this->settings['qtype'];
145
146 10
		if (is_callable(array($this, $method)))
147 10
		{
148 9
			call_user_func_array(array($this, $method), array(&$sql_array, $change_user));
149 9
		}
150
		else
151
		{
152 1
			return array();
153
		}
154
155 9
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $sql_array);
156 9
		$result = $this->db->sql_query_limit($sql, 1, 0, $this->get_cache_time($this->settings['qtype'], $this->settings['rotation']));
157 9
		$row = $this->db->sql_fetchrow($result);
158 9
		$this->db->sql_freeresult($result);
159
160 9
		return $row;
161
	}
162
163
	/**
164
	 * @return array
165
	 */
166 10
	private function get_sql_array()
167
	{
168
		return array(
169 10
			'SELECT'	=> 'u.user_id, u.username, u.user_colour, u.user_avatar, u.user_avatar_type, u.user_avatar_height, u.user_avatar_width, u.user_regdate, u.user_lastvisit, u.user_birthday, u.user_posts, u.user_rank',
170 10
			'FROM'		=> array(USERS_TABLE => 'u'),
171 10
			'WHERE'		=> $this->db->sql_in_set('u.user_type', array(USER_NORMAL, USER_FOUNDER)),
172 10
		);
173
	}
174
175
	/**
176
	 * @param array $sql_array
177
	 * @param bool $change_user
178
	 */
179 7
	private function query_featured(array &$sql_array, $change_user)
180
	{
181 7
		$sql_array['WHERE'] .= ' AND user_id = ' . userlist::get_user_id($this->settings, $change_user);
182 7
	}
183
184
	/**
185
	 * @param array $sql_array
186
	 */
187 1
	private function query_recent(array &$sql_array)
188
	{
189 1
		$sql_array['WHERE'] = 'user_id = ' . (int) $this->config['newest_user_id'];
190 1
	}
191
192
	/**
193
	 * @param array $sql_array
194
	 */
195 1
	private function query_posts(array &$sql_array)
196
	{
197 1
		$sql_array['ORDER_BY'] = 'user_posts DESC';
198 1
	}
199
200
	/**
201
	 * @param string $query_type
202
	 * @param string $rotation
203
	 * @return int
204
	 */
205 9
	private function get_cache_time($query_type, $rotation)
206
	{
207 9
		return ($rotation !== 'pageload' || in_array($query_type, array('posts', 'recent'))) ? $this->cache_time : 0;
208
	}
209
210
	/**
211
	 * @return bool
212
	 */
213 10
	private function change_user()
214
	{
215 10
		$change = false;
216 10
		if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']]))
217 10
		{
218 9
			$this->settings['last_changed'] = time();
219 9
			$change = true;
220 9
		}
221
222 10
		return $change;
223
	}
224
225
	/**
226
	 */
227 8
	private function explain_view()
228
	{
229 8
		$query_type = $this->settings['qtype'];
230 8
		$rotation = $this->settings['rotation'];
231
232 8
		$this->ptemplate->assign_vars(array(
233 8
			'QTYPE_EXPLAIN'		=> ($query_type == 'posts' || $query_type == 'recent') ? $this->user->lang('QTYPE_' . strtoupper($query_type)) : '',
234 8
			'TITLE_EXPLAIN'		=> ($rotation != 'pageload') ? $this->user->lang(strtoupper($rotation) . '_MEMBER') : '',
235 8
		));
236 8
	}
237
238
	/**
239
	 * @param array $bdata
240
	 * @return array
241
	 */
242 10
	private function get_settings(array $bdata)
243
	{
244 10
		$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']);
245 10
		$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings'];
246 10
		$settings['hash'] = $bdata['hash'];
247
248 10
		return $settings;
249
	}
250
251
	/**
252
	 * @param int $bid
253
	 * @param bool $change_user
254
	 */
255 10
	private function save_settings($bid, $change_user)
256
	{
257 10
		if ($change_user && $this->settings['qtype'] === 'featured')
258 10
		{
259 6
			$settings = $this->settings;
260 6
			unset($settings['hash']);
261
			$sql_data = array(
262 6
				'settings'	=> json_encode($settings)
263 6
			);
264 6
			$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid);
265 6
			$this->cache->put('pt_block_data_' . $bid, $this->settings);
266 6
		}
267 10
	}
268
269
	/**
270
	 * @param int   $block_id
271
	 * @param array $row
272
	 * @param bool  $change_user
273
	 * @return string
274
	 */
275 10
	private function display_user($block_id, array $row, $change_user)
276
	{
277 10
		$this->save_settings($block_id, $change_user);
278
279 10
		$html = '';
280 10
		if (sizeof($row))
281 10
		{
282 8
			$this->explain_view();
283
284 8
			$tpl_data = $this->get_template_data($row);
285 8
			$this->ptemplate->assign_vars($tpl_data['row']);
286 8
			$this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']);
287 8
			unset($tpl_data);
288
289 8
			$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block');
290 8
		}
291
292 10
		return $html;
293
	}
294
295
	/**
296
	 * @param array $row
297
	 * @return array
298
	 */
299 8
	private function get_template_data(array $row)
300
	{
301 8
		$date_format = $this->user->lang('DATE_FORMAT');
302 8
		$username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']);
303 8
		$rank = phpbb_get_user_rank($row, $row['user_posts']);
304
305 8
		$tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']);
306
307 8
		$tpl_data['row'] = array_merge($tpl_data['row'], array(
308 8
			'USERNAME'			=> $username,
309 8
			'AVATAR_IMG'		=> phpbb_get_user_avatar($row),
310 8
			'POSTS_PCT'			=> sprintf($this->user->lang('POST_PCT'), $this->calculate_percent_posts($row['user_posts'])),
311 8
			'L_VIEW_PROFILE'	=> sprintf($this->user->lang('VIEW_USER_PROFILE'), $username),
312 8
			'JOINED'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
313 8
			'VISITED'			=> $this->get_last_visit_date($row['user_lastvisit'], $date_format),
314 8
			'POSTS'				=> $row['user_posts'],
315 8
			'RANK_TITLE'		=> $rank['title'],
316 8
			'RANK_IMG'			=> $rank['img'],
317 8
			'U_PROFILE'			=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
318 8
			'U_SEARCH_USER'		=> append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&amp;sr=posts"),
319 8
		));
320
321 8
		return $tpl_data;
322
	}
323
324
	/**
325
	 * @param int $user_posts
326
	 * @return int|mixed
327
	 */
328 8
	private function calculate_percent_posts($user_posts)
329
	{
330 8
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
331
	}
332
333
	/**
334
	 * @param int $last_visited
335
	 * @param string $date_format
336
	 * @return string
337
	 */
338 8
	private function get_last_visit_date($last_visited, $date_format)
339
	{
340 8
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
341
	}
342
343
	/**
344
	 * @param string $qtype
345
	 * @return string
346
	 */
347 10
	private function get_block_title($qtype)
348
	{
349 10
		$qtypes = $this->get_query_types();
350 10
		return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER';
351
	}
352
353
	/**
354
	 * @return array
355
	 */
356 1
	private function get_rotation_frequencies()
357
	{
358
		return array(
359 1
			'pageload'	=> 'ROTATE_PAGELOAD',
360 1
			'hourly'	=> 'ROTATE_HOURLY',
361 1
			'daily'		=> 'ROTATE_DAILY',
362 1
			'weekly'	=> 'ROTATE_WEEKLY',
363 1
			'monthly'	=> 'ROTATE_MONTHLY',
364 1
		);
365
	}
366
367
	/**
368
	 * @return array
369
	 */
370 11
	private function get_query_types()
371
	{
372
		return array(
373 11
			'recent'	=> 'RECENT_MEMBER',
374 11
			'posts'		=> 'POSTS_MEMBER',
375 11
			'featured'	=> 'FEATURED_MEMBER',
376 11
		);
377
	}
378
}
379