Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

forum_topics::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 1
rs 9.9332
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
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\blocks;
12
13
/**
14
 * Forum Topics Block
15
 */
16
class forum_topics extends forum_topics_config
17
{
18
	/** @var \phpbb\auth\auth */
19
	protected $auth;
20
21
	/** @var \phpbb\content_visibility */
22
	protected $content_visibility;
23
24
	/** @var \phpbb\language\language */
25
	protected $translator;
26
27
	/** @var \phpbb\user */
28
	protected $user;
29
30
	/** @var \Urodoz\Truncate\TruncateService */
31
	protected $truncator;
32
33
	/** @var \blitze\sitemaker\services\date_range */
34
	protected $date_range;
35
36
	/** @var \blitze\sitemaker\services\forum\data */
37
	protected $forum_data;
38
39
	/** @var \blitze\sitemaker\services\forum\options */
40
	protected $forum_options;
41
42
	/** @var string */
43
	protected $phpbb_root_path;
44
45
	/** @var string */
46
	protected $php_ext;
47
48
	/** @var array */
49
	private $fields = array();
50
51
	/** @var array */
52
	private $settings = array();
53
54
	/** @var array */
55
	private $topic_tracking_info = array();
56
57
	/**
58
	 * Constructor
59
	 *
60
	 * @param \phpbb\auth\auth							$auth				Permission object
61
	 * @param \phpbb\content_visibility					$content_visibility	Content visibility object
62
	 * @param \phpbb\language\language					$translator			Language object
63
	 * @param \phpbb\user								$user				User object
64
	 * @param \Urodoz\Truncate\TruncateService			$truncator			Truncator service
65
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
66
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
67
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
68 7
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
69
	 * @param string									$php_ext			php file extension
70 7
	 */
71
	public function __construct(\phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, \phpbb\language\language $translator, \phpbb\user $user, \Urodoz\Truncate\TruncateService $truncator, \blitze\sitemaker\services\date_range $date_range, \blitze\sitemaker\services\forum\data $forum_data, \blitze\sitemaker\services\forum\options $forum_options, $phpbb_root_path, $php_ext)
72 7
	{
73 7
		parent::__construct($forum_options);
74 7
75 7
		$this->auth = $auth;
76 7
		$this->content_visibility = $content_visibility;
77 7
		$this->translator = $translator;
78 7
		$this->user = $user;
79 7
		$this->truncator = $truncator;
80 7
		$this->date_range = $date_range;
81
		$this->forum_data = $forum_data;
82
		$this->phpbb_root_path = $phpbb_root_path;
83
		$this->php_ext = $php_ext;
84
	}
85 6
86
	/**
87 6
	 * {@inheritdoc}
88
	 */
89 6
	public function display(array $bdata, $edit_mode = false)
90
	{
91 6
		$this->settings = $bdata['settings'];
92 6
93 6
		$topic_data = $this->get_topic_data();
94 6
95 6
		$data = null;
96
		if (sizeof($topic_data))
97
		{
98 6
			$data = $this->get_block_content($topic_data);
99 6
		}
100 6
101
		return array(
102
			'title'	=> $this->get_block_title(),
103
			'data'	=> $data,
104
		);
105
	}
106
107 6
	/**
108
	 * @param array $topic_data
109 6
	 * @return array
110
	 */
111 6
	protected function get_block_content(array $topic_data)
112 6
	{
113 6
		$this->set_display_fields();
114
115 6
		$post_data = $this->get_post_data($topic_data);
116 6
		$topic_data = array_values($topic_data);
117
118 6
		return array(
119 6
			'CONTEXT'			=> $this->settings['context'],
120 6
			'TEMPLATE'			=> $this->settings['template'],
121 6
			'TOPICS'			=> $this->get_topics($topic_data, $post_data),
122 6
			'S_IS_BOT'			=> $this->user->data['is_bot'],
123 6
			'LAST_POST_IMG'		=> $this->user->img('icon_topic_latest'),
124
			'NEWEST_POST_IMG'	=> $this->user->img('icon_topic_newest'),
125 6
		);
126
	}
127
128
	/**
129
	 * @param array $topic_data
130
	 * @param array $post_data
131
	 * @return array
132 6
	 */
133
	protected function get_topics(array &$topic_data, array &$post_data)
134 6
	{
135
		$user_data = $this->forum_data->get_posters_info();
136 6
137
		$topics = [];
138 6
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
139 6
		{
140 6
			$row = $topic_data[$i];
141 6
			$forum_id = $row['forum_id'];
142 6
			$topic_id = $row['topic_id'];
143
			$author = $user_data[$row[$this->fields['user_id']]];
144 6
			$last_poster = $user_data[$row['topic_last_poster_id']];
145 6
146 6
			$topics[] = array(
147 6
				'USERNAME'			=> $author['username_full'],
148 6
				'AVATAR'			=> $author['avatar'],
149
				'LAST_POSTER'		=> $last_poster['username_full'],
150 6
				'LAST_AVATAR'		=> $last_poster['avatar'],
151 6
152 6
				'FORUM_TITLE'		=> $row['forum_name'],
153 6
				'TOPIC_TITLE'		=> truncate_string(censor_text($row['topic_title']), $this->settings['topic_title_limit'], 255, false, '...'),
154 6
				'TOPIC_PREVIEW'		=> $this->get_post_preview(array_pop($post_data[$topic_id])),
155 6
				'TOPIC_POST_TIME'	=> $this->user->format_date($row[$this->fields['time']]),
156 6
				'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($forum_id, $row['topic_attachment']),
157 6
				'REPLIES'			=> $this->content_visibility->get_count('topic_posts', $row, $forum_id) - 1,
158
				'VIEWS'				=> (int) $row['topic_views'],
159 6
				'S_UNREAD_TOPIC'	=> $this->is_unread_topic($forum_id, $topic_id, $row['topic_last_post_time']),
160 6
161 6
				'U_VIEWPROFILE'		=> $author['u_viewprofile'],
162 6
				'U_VIEWTOPIC'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
163 6
				'U_VIEWFORUM'		=> append_sid($this->phpbb_root_path . 'viewforum.' . $this->php_ext, "f=$forum_id"),
164 6
				'U_NEW_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
165 6
				'U_LAST_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;p=" . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
166 6
			);
167
			unset($topic_data[$i], $post_data[$topic_id]);
168
		}
169
170
		return $topics;
171 6
	}
172
173
	/**
174 6
	 * @return string
175 6
	 */
176 6
	protected function get_block_title()
177 6
	{
178 6
		$types = array(
179
			POST_GLOBAL		=> 'FORUM_GLOBAL_ANNOUNCEMENTS',
180
			POST_ANNOUNCE	=> 'FORUM_ANNOUNCEMENTS',
181 6
			POST_STICKY		=> 'FORUM_STICKY_POSTS',
182
			POST_NORMAL		=> 'FORUM_RECENT_TOPICS',
183 6
		);
184
185
		// if more than one topic type is selected, we default to RECENT_TOPICS
186
		$topic_type = join(',', $this->settings['topic_type']);
187
188
		return ($this->settings['order_by'] !== self::FORUMS_ORDER_LAST_READ) ? (isset($types[$topic_type]) ? $types[$topic_type] : 'FORUM_RECENT_TOPICS') : 'TOPICS_LAST_READ';
189
	}
190 6
191
	/**
192 6
	 * @param array $row
193 6
	 * @return string
194 6
	 */
195 3
	protected function get_post_preview(array $row)
196 3
	{
197 3
		$preview = '';
198
		if ($this->settings['preview_chars'])
199 6
		{
200
			$method = ($this->settings['template'] === 'context') ? 'get_trimmed_text' : 'get_tooltip_text';
201
			$preview = call_user_func_array(array($this, $method), array($row));
202
		}
203
204
		return $preview;
205 1
	}
206
207 1
	/**
208 1
	 * @param array $row
209
	 */
210 1
	protected function get_trimmed_text(array $row)
211 1
	{
212
		$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
213
		$row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
214
215
		return $this->truncator->truncate($row['post_text'], $this->settings['preview_chars']);
216
	}
217
218 2
	/**
219
	 * @param array $row
220 2
	 * @return string
221
	 */
222 2
	protected function get_tooltip_text(array $row)
223 2
	{
224
		strip_bbcode($row['post_text'], $row['bbcode_uid']);
225
226
		$row['post_text'] = truncate_string($row['post_text'], $this->settings['preview_chars']);
227
		return wordwrap($row['post_text'], 40, "\n");
228
	}
229 6
230
	/**
231
	 * @return array
232 6
	 */
233 6
	private function get_topic_data()
234 6
	{
235 6
		$sort_order = array(
236
			self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
237 6
			self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
238
			self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
239 6
		);
240 6
241 6
		$range_info = $this->date_range->get($this->settings['date_range']);
242 6
243 6
		$this->forum_data->query($this->settings['enable_tracking'])
244 6
			->fetch_forum($this->settings['forum_ids'])
245
			->fetch_topic_type($this->settings['topic_type'])
246 6
			->fetch_date_range($range_info['start'], $range_info['stop'])
247 6
			->set_sorting($sort_order[$this->settings['order_by']])
248
			->build();
249 6
250
		$topic_data = $this->forum_data->get_topic_data($this->settings['max_topics']);
251
		$this->topic_tracking_info = $this->forum_data->get_topic_tracking_info();
252
253
		return $topic_data;
254
	}
255
256 6
	/**
257
	 * @param array $topic_data
258 6
	 * @return array
259 6
	 */
260 3
	private function get_post_data(array $topic_data)
261 3
	{
262
		if ($this->settings['context'] && $this->settings['preview_chars'])
263
		{
264 3
			$post_data = $this->forum_data->get_post_data($this->settings['context']);
265
		}
266
		else
267 6
		{
268
			$post_data = array_fill_keys(array_keys($topic_data), array(array('post_text' => '', 'bbcode_uid' => '', 'bbcode_bitfield' => '')));
269
		}
270
271
		return $post_data;
272
	}
273 6
274
	/**
275 6
	 * @return void
276 6
	 */
277 3
	private function set_display_fields()
278 3
	{
279
		if ($this->settings['context'] === 'last')
280 3
		{
281 3
			$this->fields['time'] = 'topic_last_post_time';
282
			$this->fields['user_id'] = 'topic_last_poster_id';
283
		}
284 3
		else
285 3
		{
286
			$this->fields['time'] = 'topic_time';
287 6
			$this->fields['user_id'] = 'topic_poster';
288
		}
289
	}
290
291
	/**
292
	 * @param int $forum_id
293
	 * @param int $topic_attachment
294 6
	 * @return string
295
	 */
296 6
	private function get_attachment_icon($forum_id, $topic_attachment)
297
	{
298
		return ($this->user_can_view_attachments($forum_id) && $topic_attachment) ? $this->user->img('icon_topic_attach', $this->translator->lang('TOTAL_ATTACHMENTS')) : '';
299
	}
300
301
	/**
302
	 * @param int $forum_id
303 6
	 * @return bool
304
	 */
305 6
	private function user_can_view_attachments($forum_id)
306
	{
307
		return ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $forum_id)) ? true : false;
308
	}
309
310
	/**
311
	 * @param int $forum_id
312
	 * @param int $topic_id
313
	 * @param int $topic_last_post_time
314 6
	 * @return bool
315
	 */
316 6
	private function is_unread_topic($forum_id, $topic_id, $topic_last_post_time)
317
	{
318
		return (isset($this->topic_tracking_info[$forum_id][$topic_id]) && $topic_last_post_time > $this->topic_tracking_info[$forum_id][$topic_id]) ? true : false;
319
	}
320
}
321