Completed
Push — master ( df7035...349ca4 )
by Daniel
08:26
created

poll::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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