Passed
Branch develop (786e4f)
by Daniel
07:32
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 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0

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 */
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
	protected $config;
19
20
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
	protected $db;
22
23
	/** @var \phpbb\request\request_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\request\request_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
	protected $request;
25
26
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
	protected $translator;
28
29
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	/**
68
	 * @param array $topic_data
69
	 * @param \phpbb\template\twig\twig $template
70
	 */
71 2
	public function build(array $topic_data, \phpbb\template\twig\twig &$template)
0 ignored issues
show
Bug introduced by
The type phpbb\template\twig\twig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
	{
73 2
		$this->translator->add_lang('viewtopic');
74
75 2
		$forum_id = (int) $topic_data['forum_id'];
76 2
		$topic_id = (int) $topic_data['topic_id'];
77
78 2
		$cur_voted_id = $this->get_users_votes($topic_id);
79 2
		$s_can_vote = $this->user_can_vote($forum_id, $topic_data, $cur_voted_id);
80 2
		$viewtopic_url = append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "f=$forum_id&amp;t=$topic_id");
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
		$viewtopic_url = /** @scrutinizer ignore-call */ append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "f=$forum_id&amp;t=$topic_id");
Loading history...
81
82 2
		$poll_total = $poll_most = 0;
83 2
		$poll_info = $this->get_poll_info($topic_data, $poll_total, $poll_most);
84 2
		$poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
85
86 2
		$this->build_poll_options($cur_voted_id, $poll_info, $poll_total, $poll_most, $template);
87
88 2
		$template->assign_vars(array(
89 2
			'POLL_QUESTION'		=> $topic_data['poll_title'],
90 2
			'TOTAL_VOTES' 		=> $poll_total,
91 2
			'POLL_LEFT_CAP_IMG'	=> $this->user->img('poll_left'),
92 2
			'POLL_RIGHT_CAP_IMG'=> $this->user->img('poll_right'),
93
94 2
			'MAX_VOTES'			=> $this->translator->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']),
95 2
			'POLL_LENGTH'		=> $this->get_poll_length_lang($topic_data['poll_length'], $poll_end),
96
97 2
			'S_CAN_VOTE'		=> $s_can_vote,
98 2
			'S_DISPLAY_RESULTS'	=> $this->show_results($s_can_vote, $cur_voted_id),
99 2
			'S_IS_MULTI_CHOICE'	=> $this->poll_is_multiple_choice($topic_data['poll_max_options']),
100 2
			'S_POLL_ACTION'		=> $viewtopic_url,
101 2
			'S_FORM_TOKEN'		=> $this->sitemaker->get_form_key('posting'),
102
103 2
			'U_VIEW_RESULTS'	=> $viewtopic_url . '&amp;view=viewpoll',
104 2
		));
105 2
	}
106
107
	/**
108
	 * @param int $forum_id
109
	 * @param array $topic_data
110
	 * @param array $cur_voted_id
111
	 * @return bool
112
	 */
113 2
	private function user_can_vote($forum_id, array $topic_data, array $cur_voted_id)
114
	{
115
		return (
116 2
			$this->user_is_authorized($forum_id, $topic_data, $cur_voted_id) &&
117 2
			$this->poll_is_still_open($topic_data) &&
118 1
			$this->is_topic_status_eligible($topic_data)
119 2
		);
120
	}
121
122
	/**
123
	 * @param int $forum_id
124
	 * @param array $topic_data
125
	 * @param array $cur_voted_id
126
	 * @return bool
127
	 */
128 2
	private function user_is_authorized($forum_id, array $topic_data, array $cur_voted_id)
129
	{
130 2
		return ($this->auth->acl_get('f_vote', $forum_id) && $this->user_can_change_vote($forum_id, $topic_data, $cur_voted_id));
131
	}
132
133
	/**
134
	 * @param int $forum_id
135
	 * @param array $topic_data
136
	 * @param array $cur_voted_id
137
	 * @return bool
138
	 */
139 2
	private function user_can_change_vote($forum_id, array $topic_data, array $cur_voted_id)
140
	{
141 2
		return (!sizeof($cur_voted_id) || ($this->auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']));
142
	}
143
144
	/**
145
	 * @param array $topic_data
146
	 * @return bool
147
	 */
148 1
	private function poll_is_still_open(array $topic_data)
149
	{
150 1
		return (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0);
151
	}
152
153
	/**
154
	 * @param array $topic_data
155
	 * @return bool
156
	 */
157 1
	private function is_topic_status_eligible(array $topic_data)
158
	{
159 1
		return ($topic_data['topic_status'] != ITEM_LOCKED && $topic_data['forum_status'] != ITEM_LOCKED);
0 ignored issues
show
Bug introduced by
The constant blitze\sitemaker\services\ITEM_LOCKED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
160
	}
161
162
	/**
163
	 * @param array $topic_data
164
	 * @param int $poll_total
165
	 * @param int $poll_most
166
	 * @return array
167
	 */
168 2
	private function get_poll_info(array $topic_data, &$poll_total, &$poll_most)
169
	{
170 2
		$topic_id = (int) $topic_data['topic_id'];
171 2
		$post_id = (int) $topic_data['topic_first_post_id'];
172
173
		$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
174 2
			FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
0 ignored issues
show
Bug introduced by
The constant blitze\sitemaker\services\POLL_OPTIONS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant blitze\sitemaker\services\POSTS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
175
			WHERE o.topic_id = $topic_id
176 2
				AND p.post_id = $post_id
177
				AND p.topic_id = o.topic_id
178 2
			ORDER BY o.poll_option_id";
179 2
		$result = $this->db->sql_query($sql);
180
181 2
		$poll_info = array();
182 2
		while ($row = $this->db->sql_fetchrow($result))
183
		{
184 2
			$poll_info[] = $row;
185 2
			$poll_total += $row['poll_option_total'];
186 2
			$poll_most = ($row['poll_option_total'] >= $poll_most) ? $row['poll_option_total'] : $poll_most;
187 2
		}
188 2
		$this->db->sql_freeresult($result);
189
190 2
		return $this->parse_poll($topic_data, $poll_info);
191
	}
192
193
	/**
194
	 * @param array $topic_data
195
	 * @param array $poll_info
196
	 * @return array
197
	 */
198 2
	private function parse_poll(array &$topic_data, array $poll_info)
199
	{
200 2
		$parse_flags = ($poll_info[0]['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
0 ignored issues
show
Bug introduced by
The constant blitze\sitemaker\services\OPTION_FLAG_SMILIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant blitze\sitemaker\services\OPTION_FLAG_BBCODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
201
202 2
		for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
203
		{
204 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);
0 ignored issues
show
Bug introduced by
The function generate_text_for_display was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
			$poll_info[$i]['poll_option_text'] = /** @scrutinizer ignore-call */ generate_text_for_display($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_info[$i]['bbcode_bitfield'], $parse_flags, true);
Loading history...
205 2
		}
206
207 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);
208
209 2
		return $poll_info;
210
	}
211
212
	/**
213
	 * @param array $cur_voted_id
214
	 * @param array $poll_info
215
	 * @param int $poll_total
216
	 * @param int $poll_most
217
	 * @param \phpbb\template\twig\twig $template
218
	 */
219 2
	private function build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template)
220
	{
221 2
		foreach ($poll_info as $poll_option)
222
		{
223 2
			$option_pct = $this->calculate_option_percent($poll_option['poll_option_total'], $poll_total);
224 2
			$option_pct_rel = $this->calculate_option_percent_rel($poll_option['poll_option_total'], $poll_most);
225
226 2
			$template->assign_block_vars('poll_option', array(
227 2
				'POLL_OPTION_ID' 			=> $poll_option['poll_option_id'],
228 2
				'POLL_OPTION_CAPTION' 		=> $poll_option['poll_option_text'],
229 2
				'POLL_OPTION_RESULT' 		=> $poll_option['poll_option_total'],
230 2
				'POLL_OPTION_PERCENT' 		=> sprintf("%.1d%%", round($option_pct * 100)),
231 2
				'POLL_OPTION_PERCENT_REL' 	=> sprintf("%.1d%%", round($option_pct_rel * 100)),
232 2
				'POLL_OPTION_PCT'			=> round($option_pct * 100),
233 2
				'POLL_OPTION_WIDTH'     	=> round($option_pct * 250),
234 2
				'POLL_OPTION_VOTED'			=> $this->user_has_voted_option($poll_option['poll_option_id'], $cur_voted_id),
235 2
				'POLL_OPTION_MOST_VOTES'	=> $this->is_most_voted($poll_option['poll_option_total'], $poll_most),
236 2
			));
237 2
		}
238 2
	}
239
240
	/**
241
	 * @param int $topic_id
242
	 * @return array
243
	 */
244 2
	private function get_users_votes($topic_id)
245
	{
246 2
		$cur_voted_id = array();
247 2
		if ($this->user->data['is_registered'])
248 2
		{
249
			$sql = 'SELECT poll_option_id
250 1
			FROM ' . POLL_VOTES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant blitze\sitemaker\services\POLL_VOTES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
251 1
			WHERE topic_id = ' . (int) $topic_id . '
252 1
				AND vote_user_id = ' . (int) $this->user->data['user_id'];
253 1
			$result = $this->db->sql_query($sql);
254
255 1
			while ($row = $this->db->sql_fetchrow($result))
256
			{
257 1
				$cur_voted_id[] = $row['poll_option_id'];
258 1
			}
259 1
			$this->db->sql_freeresult($result);
260 1
		}
261
		else
262
		{
263
			// Cookie based guest tracking ... I don't like this but hum ho
264
			// it's oft requested. This relies on "nice" users who don't feel
265
			// the need to delete cookies to mess with results.
266 1
			if ($this->request->is_set($this->config['cookie_name'] . '_poll_' . $topic_id, \phpbb\request\request_interface::COOKIE))
267 1
			{
268 1
				$cur_voted_id = explode(',', $this->request->variable($this->config['cookie_name'] . '_poll_' . $topic_id, '', true, \phpbb\request\request_interface::COOKIE));
269 1
				$cur_voted_id = array_map('intval', $cur_voted_id);
270 1
			}
271
		}
272
273 2
		return $cur_voted_id;
274
	}
275
276
	/**
277
	 * @param int $poll_option_id
278
	 * @param array $cur_voted_id
279
	 * @return bool
280
	 */
281 2
	private function user_has_voted_option($poll_option_id, array $cur_voted_id)
282
	{
283 2
		return (in_array($poll_option_id, $cur_voted_id)) ? true : false;
284
	}
285
286
	/**
287
	 * @param int $poll_option_total
288
	 * @param int $poll_total
289
	 * @return float|int
290
	 */
291 2
	private function calculate_option_percent($poll_option_total, $poll_total)
292
	{
293 2
		return ($poll_total > 0) ? $poll_option_total / $poll_total : 0;
294
	}
295
296
	/**
297
	 * @param int $poll_option_total
298
	 * @param int $poll_most
299
	 * @return float|int
300
	 */
301 2
	private function calculate_option_percent_rel($poll_option_total, $poll_most)
302
	{
303 2
		return ($poll_most > 0) ? $poll_option_total / $poll_most : 0;
304
	}
305
306
	/**
307
	 * @param int $poll_option_total
308
	 * @param int $poll_most
309
	 * @return bool
310
	 */
311 2
	private function is_most_voted($poll_option_total, $poll_most)
312
	{
313 2
		return ($poll_option_total > 0 && $poll_option_total == $poll_most) ? true : false;
314
	}
315
316
	/**
317
	 * @param int $poll_max_options
318
	 * @return bool
319
	 */
320 2
	private function poll_is_multiple_choice($poll_max_options)
321
	{
322 2
		return ($poll_max_options > 1) ? true : false;
323
	}
324
325
	/**
326
	 * @param int $poll_length
327
	 * @param int $poll_end
328
	 * @return string
329
	 */
330 2
	private function get_poll_length_lang($poll_length, $poll_end)
331
	{
332 2
		return ($poll_length) ? $this->translator->lang(($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT', $this->user->format_date($poll_end)) : '';
333
	}
334
335
	/**
336
	 * @param bool $s_can_vote
337
	 * @param array $cur_voted_id
338
	 * @return bool
339
	 */
340 2
	private function show_results($s_can_vote, array $cur_voted_id)
341
	{
342 2
		return (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id))) ? true : false;
343
	}
344
}
345