Completed
Push — develop ( 7acac6...1f96ef )
by Daniel
10:51
created

data::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 9.4285
cc 1
eloc 11
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) 2016 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\users;
11
12
class data extends contacts
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\db\driver\driver_interface */
21
	protected $db;
22
23
	/** @var \phpbb\profilefields\manager */
24
	protected $profile_fields;
25
26
	/** @var \phpbb\language\language */
27
	protected $translator;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var \blitze\sitemaker\services\util */
33
	protected $util;
34
35
	/** @var string */
36
	protected $phpbb_root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var string */
42
	protected $default_avatar;
43
44
	/** @var array */
45
	protected $user_cache = array();
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param \phpbb\auth\auth					$auth					Auth object
51
	 * @param \phpbb\config\config				$config					Config object
52
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
53
	 * @param \phpbb\profilefields\manager      $profile_fields			Profile fields manager
54
	 * @param \phpbb\language\language			$translator				Language object
55
	 * @param \phpbb\user						$user					User Object
56
	 * @param \blitze\sitemaker\services\util	$util					Sitemaker utility Object
57
	 * @param string							$phpbb_root_path		Path to the phpbb includes directory.
58
	 * @param string							$php_ext				php file extension
59
	 */
60 89
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\profilefields\manager $profile_fields, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\util $util, $phpbb_root_path, $php_ext)
61
	{
62 89
		parent::__construct($auth, $config, $translator, $user, $phpbb_root_path, $php_ext);
63
64 89
		$this->auth = $auth;
65 89
		$this->config = $config;
66 89
		$this->db = $db;
67 89
		$this->profile_fields = $profile_fields;
68 89
		$this->translator = $translator;
69 89
		$this->user = $user;
70 89
		$this->util = $util;
71 89
		$this->phpbb_root_path = $phpbb_root_path;
72 89
		$this->php_ext = $php_ext;
73 89
	}
74
75
	/**
76
	 * @param array $user_ids
77
	 * @param string $sql_where
78
	 * @return array
79
	 */
80 13
	public function get_users(array $user_ids, $sql_where = '')
81
	{
82 13
		$query_ids = array_diff($user_ids, array_keys($this->user_cache));
83
84 13
		if (sizeof($query_ids))
85 13
		{
86 13
			$sql_where .= (($sql_where) ? ' AND ' : '') . $this->db->sql_in_set('user_id', $query_ids);
87 13
			$this->query($sql_where);
88 13
		}
89
90 13
		return array_intersect_key($this->user_cache, array_flip($user_ids));
91
	}
92
93
	/**
94
	 * @param string $sql_where
95
	 * @param string $order_by
96
	 * @param int|bool $limit
97
	 * @return array|bool
98
	 */
99 15
	public function query($sql_where = '', $order_by = '', $limit = false)
100
	{
101 15
		$sql = $this->get_sql_statement($sql_where, $order_by);
102 15
		$result = $this->db->sql_query_limit($sql, $limit);
103
104 15
		$users = array();
105 15
		while ($row = $this->db->sql_fetchrow($result))
106
		{
107 13
			$user_id = $row['user_id'];
108 13
			$users[$user_id] = $row;
109
110 13
			$this->user_cache[$user_id] = $this->get_data($row);
111 13
			$this->user_cache[$user_id] += $this->get_rank($row);
112 13
		}
113 15
		$this->db->sql_freeresult($result);
114
115 15
		if (!sizeof($users))
116 15
		{
117 4
			return false;
118
		}
119
120 13
		$this->get_additional_fields($users);
121 13
		unset($users);
122
123 13
		return $this->user_cache;
124
	}
125
126
	/**
127
	 * @param array $row
128
	 * @return array
129
	 */
130 13
	public function get_data($row)
131
	{
132 13
		$user_id = $row['user_id'];
133 13
		$date_format = $this->translator->lang('DATE_FORMAT');
134
135 13
		$this->default_avatar = $this->util->get_default_avatar();
136
137
		return array(
138 13
			'avatar'			=> $this->get_avatar($row),
139 13
			'username'			=> get_username_string('username', $user_id, $row['username'], $row['user_colour']),
140 13
			'username_full'		=> get_username_string('full', $user_id, $row['username'], $row['user_colour']),
141
142 13
			'joined'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
143 13
			'visited'			=> $this->get_last_visit_date($row['user_lastvisit'], $date_format),
144 13
			'posts'				=> $row['user_posts'],
145 13
			'posts_pct'			=> $this->translator->lang_array('POST_PCT', $this->calculate_percent_posts($row['user_posts'])),
146
147 13
			'contact_user' 		=> $this->translator->lang('CONTACT_USER', get_username_string('username', $user_id, $row['username'], $row['user_colour'], $row['username'])),
148
149 13
			'u_search_posts'	=> $this->get_search_url($user_id),
150 13
			'u_viewprofile'		=> get_username_string('profile', $user_id, $row['username'], $row['user_colour']),
151
152 13
			'contact_fields'	=> array(),
153 13
			'profile_fields'	=> array(),
154 13
		);
155
	}
156
157
	/**
158
	 * @return array
159
	 */
160 1
	public function get_profile_fields()
161
	{
162
		$sql = 'SELECT l.lang_name, f.field_ident
163 1
			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
164 1
			WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . '
165
				AND f.field_active = 1
166
				AND f.field_no_view = 0
167
				AND f.field_hide = 0
168
				AND l.field_id = f.field_id
169 1
			ORDER BY f.field_order';
170 1
		$result = $this->db->sql_query($sql);
171
172 1
		$cpf_options = false;
173 1
		while ($row = $this->db->sql_fetchrow($result))
174
		{
175 1
			$cpf_options[$row['field_ident']] = $row['lang_name'];
176 1
		}
177 1
		$this->db->sql_freeresult($result);
178
179 1
		return $cpf_options;
180
	}
181
182
	/**
183
	 * @param array $row
184
	 * @return string
185
	 */
186 13
	protected function get_avatar(array $row)
187
	{
188 13
		$avatar = '';
189 13
		if ($this->user->optionget('viewavatars'))
190 13
		{
191 13
			$avatar = ($row['avatar']) ? phpbb_get_user_avatar($row) : $this->default_avatar;
192 13
		}
193
194 13
		return $avatar;
195
	}
196
197
	/**
198
	 * @param int $user_posts
199
	 * @return int
200
	 */
201 13
	protected function calculate_percent_posts($user_posts)
202
	{
203 13
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
204
	}
205
206
	/**
207
	 * @param int $last_visited
208
	 * @param string $date_format
209
	 * @return string
210
	 */
211 13
	protected function get_last_visit_date($last_visited, $date_format)
212
	{
213 13
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
214
	}
215
216
	/**
217
	 * @param int $user_id
218
	 * @return string
219
	 */
220 13
	protected function get_search_url($user_id)
221
	{
222 13
		return ($this->auth->acl_get('u_search')) ? append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id=$user_id&amp;sr=posts") : '';
223
	}
224
225
	/**
226
	 * @param array $row
227
	 * @return array
228
	 */
229 13
	public function get_rank(array $row)
230
	{
231 13
		if (!function_exists('phpbb_get_user_rank'))
232 13
		{
233 1
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
234 1
		}
235
236 13
		$user_rank_data = phpbb_get_user_rank($row, $row['user_posts']);
237
238
		return array(
239 13
			'RANK_TITLE'		=> $user_rank_data['title'],
240 13
			'RANK_IMAGE'		=> $user_rank_data['img'],
241 13
			'RANK_IMAGE_SRC'	=> $user_rank_data['img_src'],
242 13
		);
243
	}
244
245
	/**
246
	 * @param array $users
247
	 */
248 13
	protected function get_additional_fields(array $users)
249
	{
250 13
		$user_ids = array_keys($users);
251
252 13
		$can_receive_pm_list = $this->get_can_receive_pm_list($user_ids);
253 13
		$permanently_banned_users = $this->get_banned_users_list($user_ids);
254
255
		// Grab all profile fields from users in id cache for later use - similar to the poster cache
256 13
		$profile_fields_cache = $this->profile_fields->grab_profile_fields_data($user_ids);
257
258 13
		for ($i = 0, $size = sizeof($user_ids); $i < $size; $i++)
259
		{
260 13
			$id = $user_ids[$i];
261 13
			$row = $users[$id];
262
263 13
			$this->user_cache[$id]['contact_fields'] = array_filter(array(
264 13
				'pm'		=> $this->get_pm_contact($row, $can_receive_pm_list, $permanently_banned_users),
265 13
				'email'		=> $this->get_email_contact($row),
266 13
				'jabber'	=> $this->get_jabber_contact($row),
267 13
			));
268 13
			$this->get_custom_profile_fields($id, $profile_fields_cache);
269 13
		}
270 13
	}
271
272
	/**
273
	 * @param int $user_id
274
	 * @param array $profile_fields_cache
275
	 */
276 13
	protected function get_custom_profile_fields($user_id, array $profile_fields_cache)
277
	{
278 13
		$cp_row = (isset($profile_fields_cache[$user_id])) ? $this->profile_fields->generate_profile_fields_template_data($profile_fields_cache[$user_id]) : array('blockrow' => array());
279
280 13
		foreach ($cp_row['blockrow'] as $field_data)
281
		{
282 9
			$field = $field_data['PROFILE_FIELD_IDENT'];
283
284 9
			if ($field_data['S_PROFILE_CONTACT'])
285 9
			{
286 9
				$this->user_cache[$user_id]['contact_fields'][$field] = array(
287 9
					'ID'		=> $field,
288 9
					'NAME'		=> $field_data['PROFILE_FIELD_NAME'],
289 9
					'U_CONTACT'	=> $field_data['PROFILE_FIELD_CONTACT'],
290
				);
291 9
			}
292
			else
293
			{
294 9
				$this->user_cache[$user_id]['profile_fields'][$field] = $field_data;
295
			}
296 13
		}
297 13
	}
298
299
	/**
300
	 * @param string $sql_where
301
	 * @param string $order_by
302
	 * @return string
303
	 */
304 15
	protected function get_sql_statement($sql_where = '', $order_by = '')
305
	{
306
		return 'SELECT user_id, username, user_type, user_colour, user_avatar, user_avatar_type, user_avatar_height, user_avatar_width, user_regdate, user_lastvisit, user_birthday, user_posts, user_rank, user_allow_viewemail, user_allow_pm, user_jabber, user_inactive_reason
307 15
			FROM ' . USERS_TABLE .
308 15
			(($sql_where) ? ' WHERE ' . $sql_where : '') .
309 15
			(($order_by) ? ' ORDER BY ' . $order_by : '');
310
	}
311
}
312