Completed
Push — develop ( 1f2a5d...e6d06f )
by Daniel
21:42 queued 08:26
created

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