Completed
Push — master ( 9866a1...02e5e5 )
by Daniel
08:18
created

poll::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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;
11
12
class poll
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\request\request_interface */
24
	protected $request;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/** @var \blitze\sitemaker\services\util */
30
	protected $sitemaker;
31
32
	/** @var string */
33
	protected $phpbb_root_path;
34
35
	/** @var string */
36
	protected $php_ext;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\auth\auth						$auth				Permission object
42
	 * @param \phpbb\config\config					$config				Config object
43
	 * @param \phpbb\db\driver\driver_interface		$db	 				Database connection
44
	 * @param \phpbb\request\request_interface		$request			Request object
45
	 * @param \phpbb\user							$user				User object
46
	 * @param \blitze\sitemaker\services\util		$sitemaker			Sitemaker Object
47
	 * @param string								$phpbb_root_path	Path to the phpbb includes directory.
48
	 * @param string								$php_ext			php file extension
49
	 */
50
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\user $user, \blitze\sitemaker\services\util $sitemaker, $phpbb_root_path, $php_ext)
51
	{
52
		$this->auth = $auth;
53
		$this->config = $config;
54
		$this->db = $db;
55
		$this->request = $request;
56
		$this->user = $user;
57
		$this->sitemaker = $sitemaker;
58
		$this->phpbb_root_path = $phpbb_root_path;
59
		$this->php_ext = $php_ext;
60
	}
61
62
	public function build(array $topic_data, \phpbb\template\twig\twig &$template)
63
	{
64
		$this->user->add_lang('viewtopic');
65
66
		$forum_id = (int) $topic_data['forum_id'];
67
		$topic_id = (int) $topic_data['topic_id'];
68
69
		$cur_voted_id = $this->_get_users_votes($topic_id);
70
		$s_can_vote = $this->_user_can_vote($forum_id, $topic_data, $cur_voted_id);
71
		$viewtopic_url = append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "f=$forum_id&amp;t=$topic_id");
72
73
		$poll_total = $poll_most = 0;
74
		$poll_info = $this->_get_poll_info($topic_data, $poll_total, $poll_most);
75
		$poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
76
77
		$this->_build_poll_options($cur_voted_id, $poll_info, $poll_total, $poll_most, $template);
78
79
		$template->assign_vars(array(
80
			'POLL_QUESTION'		=> $topic_data['poll_title'],
81
			'TOTAL_VOTES' 		=> $poll_total,
82
			'POLL_LEFT_CAP_IMG'	=> $this->user->img('poll_left'),
83
			'POLL_RIGHT_CAP_IMG'=> $this->user->img('poll_right'),
84
85
			'L_MAX_VOTES'		=> $this->user->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']),
86
			'L_POLL_LENGTH'		=> $this->_get_poll_length_lang($topic_data['poll_length'], $poll_end),
87
88
			'S_CAN_VOTE'		=> $s_can_vote,
89
			'S_DISPLAY_RESULTS'	=> $this->_show_results($s_can_vote, $cur_voted_id),
90
			'S_IS_MULTI_CHOICE'	=> $this->_poll_is_multiple_choice($topic_data['poll_max_options']),
91
			'S_POLL_ACTION'		=> $viewtopic_url,
92
			'S_FORM_TOKEN'		=> $this->sitemaker->get_form_key('posting'),
93
94
			'U_VIEW_RESULTS'	=> $viewtopic_url . '&amp;view=viewpoll',
95
		));
96
	}
97
98
	/**
99
	 * @param int $forum_id
100
	 * @param array $topic_data
101
	 * @param array $cur_voted_id
102
	 * @return bool
103
	 */
104
	private function _user_can_vote($forum_id, array $topic_data, array $cur_voted_id)
105
	{
106
		return ($this->auth->acl_get('f_vote', $forum_id) &&
107
			(($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
108
			$topic_data['topic_status'] != ITEM_LOCKED &&
109
			$topic_data['forum_status'] != ITEM_LOCKED &&
110
			(!sizeof($cur_voted_id) ||
111
			($this->auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
112
	}
113
114
	/**
115
	 * @param array $topic_data
116
	 * @param int $poll_total
117
	 * @param int $poll_most
118
	 * @return array
119
	 */
120
	private function _get_poll_info(array $topic_data, &$poll_total, &$poll_most)
121
	{
122
		$topic_id = (int) $topic_data['topic_id'];
123
		$post_id = (int) $topic_data['topic_first_post_id'];
124
125
		$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
126
			FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
127
			WHERE o.topic_id = $topic_id
128
				AND p.post_id = $post_id
129
				AND p.topic_id = o.topic_id
130
			ORDER BY o.poll_option_id";
131
		$result = $this->db->sql_query($sql);
132
133
		$poll_info = array();
134
		while ($row = $this->db->sql_fetchrow($result))
135
		{
136
			$poll_info[] = $row;
137
			$poll_total += $row['poll_option_total'];
138
			$poll_most = ($row['poll_option_total'] >= $poll_most) ? $row['poll_option_total'] : $poll_most;
139
		}
140
		$this->db->sql_freeresult($result);
141
142
		return $this->_parse_poll($topic_data, $poll_info);
143
	}
144
145
	/**
146
	 * @param array $topic_data
147
	 * @param array $poll_info
148
	 * @return array
149
	 */
150
	private function _parse_poll(array &$topic_data, array $poll_info)
151
	{
152
		$parse_flags = ($poll_info[0]['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
153
154
		for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
155
		{
156
			$poll_info[$i]['poll_option_text'] = generate_text_for_display($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_info[$i]['bbcode_bitfield'], $parse_flags, true);
157
		}
158
159
		$topic_data['poll_title'] = generate_text_for_display($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield'], $parse_flags, true);
160
161
		return $poll_info;
162
	}
163
164
	/**
165
	 * @param array $cur_voted_id
166
	 * @param array $poll_info
167
	 * @param int $poll_total
168
	 * @param int $poll_most
169
	 * @param \phpbb\template\twig\twig $template
170
	 */
171
	private function _build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template)
172
	{
173
		foreach ($poll_info as $poll_option)
174
		{
175
			$option_pct = $this->_calculate_option_percent($poll_option['poll_option_total'], $poll_total);
176
			$option_pct_rel = $this->_calculate_option_percent_rel($poll_option['poll_option_total'], $poll_most);
177
178
			$template->assign_block_vars('poll_option', array(
179
				'POLL_OPTION_ID' 			=> $poll_option['poll_option_id'],
180
				'POLL_OPTION_CAPTION' 		=> $poll_option['poll_option_text'],
181
				'POLL_OPTION_RESULT' 		=> $poll_option['poll_option_total'],
182
				'POLL_OPTION_PERCENT' 		=> sprintf("%.1d%%", round($option_pct * 100)),
183
				'POLL_OPTION_PERCENT_REL' 	=> sprintf("%.1d%%", round($option_pct_rel * 100)),
184
				'POLL_OPTION_PCT'			=> round($option_pct * 100),
185
				'POLL_OPTION_WIDTH'     	=> round($option_pct * 250),
186
				'POLL_OPTION_VOTED'			=> $this->_user_has_voted_option($poll_option['poll_option_id'], $cur_voted_id),
187
				'POLL_OPTION_MOST_VOTES'	=> $this->_is_most_voted($poll_option['poll_option_total'], $poll_most),
188
			));
189
		}
190
	}
191
192
	/**
193
	 * @param int $topic_id
194
	 * @return array
195
	 */
196
	private function _get_users_votes($topic_id)
197
	{
198
		$cur_voted_id = array();
199
		if ($this->user->data['is_registered'])
200
		{
201
			$sql = 'SELECT poll_option_id
202
			FROM ' . POLL_VOTES_TABLE . '
203
			WHERE topic_id = ' . $topic_id . '
204
				AND vote_user_id = ' . $this->user->data['user_id'];
205
			$result = $this->db->sql_query($sql);
206
207
			while ($row = $this->db->sql_fetchrow($result))
208
			{
209
				$cur_voted_id[] = $row['poll_option_id'];
210
			}
211
			$this->db->sql_freeresult($result);
212
		}
213
		else
214
		{
215
			// Cookie based guest tracking ... I don't like this but hum ho
216
			// it's oft requested. This relies on "nice" users who don't feel
217
			// the need to delete cookies to mess with results.
218
			if ($this->request->is_set($this->config['cookie_name'] . '_poll_' . $topic_id, \phpbb\request\request_interface::COOKIE))
219
			{
220
				$cur_voted_id = explode(',', $this->request->variable($this->config['cookie_name'] . '_poll_' . $topic_id, '', true, \phpbb\request\request_interface::COOKIE));
221
				$cur_voted_id = array_map('intval', $cur_voted_id);
222
			}
223
		}
224
225
		return $cur_voted_id;
226
	}
227
228
	/**
229
	 * @param int $poll_option_id
230
	 * @param array $cur_voted_id
231
	 * @return bool
232
	 */
233
	private function _user_has_voted_option($poll_option_id, array $cur_voted_id)
234
	{
235
		return (in_array($poll_option_id, $cur_voted_id)) ? true : false;
236
	}
237
238
	/**
239
	 * @param int $poll_option_total
240
	 * @param int $poll_total
241
	 * @return float|int
242
	 */
243
	private function _calculate_option_percent($poll_option_total, $poll_total)
244
	{
245
		return ($poll_total > 0) ? $poll_option_total / $poll_total : 0;
246
	}
247
248
	/**
249
	 * @param int $poll_option_total
250
	 * @param int $poll_most
251
	 * @return float|int
252
	 */
253
	private function _calculate_option_percent_rel($poll_option_total, $poll_most)
254
	{
255
		return ($poll_most > 0) ? $poll_option_total / $poll_most : 0;
256
	}
257
258
	/**
259
	 * @param int $poll_option_total
260
	 * @param int $poll_most
261
	 * @return bool
262
	 */
263
	private function _is_most_voted($poll_option_total, $poll_most)
264
	{
265
		return ($poll_option_total > 0 && $poll_option_total == $poll_most) ? true : false;
266
	}
267
268
	/**
269
	 * @param int $poll_max_options
270
	 * @return bool
271
	 */
272
	private function _poll_is_multiple_choice($poll_max_options)
273
	{
274
		return ($poll_max_options > 1) ? true : false;
275
	}
276
277
	/**
278
	 * @param int $poll_length
279
	 * @param int $poll_end
280
	 * @return string
281
	 */
282
	private function _get_poll_length_lang($poll_length, $poll_end)
283
	{
284
		return ($poll_length) ? sprintf($this->user->lang(($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'), $this->user->format_date($poll_end)) : '';
285
	}
286
287
	/**
288
	 * @param bool $s_can_vote
289
	 * @param array $cur_voted_id
290
	 * @return bool
291
	 */
292
	private function _show_results($s_can_vote, array $cur_voted_id)
293
	{
294
		return (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id))) ? true : false;
295
	}
296
}
297