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