Passed
Push — develop ( 73b4e9...8d0058 )
by Daniel
05:19
created

topic::get_detail_template_data()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 1
nop 8
dl 0
loc 21
ccs 0
cts 20
cp 0
crap 6
rs 9.3142
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) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services;
11
12
class topic
13
{
14
	/** @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...
15
	protected $config;
16
17
	/** @var \phpbb\controller\helper */
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper 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 $controller_helper;
19
20
	/** @var \phpbb\event\dispatcher_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\event\dispatcher_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 $phpbb_dispatcher;
22
23
	/** @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...
24
	protected $language;
25
26
	/** @var \phpbb\template\template */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template 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 $template;
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\content\services\helper */
33
	protected $helper;
34
35
	/**
36
	 * Construct
37
	 *
38
	 * @param \phpbb\config\config					$config					Config object
39
	 * @param \phpbb\controller\helper				$controller_helper		Controller Helper object
40
	 * @param \phpbb\event\dispatcher_interface		$phpbb_dispatcher		Event dispatcher object
41
	 * @param \phpbb\language\language				$language				Language object
42
	 * @param \phpbb\template\template				$template				Template object
43
	 * @param \phpbb\user							$user					User object
44
	 * @param \blitze\content\services\helper		$helper					Content helper object
45
	 */
46
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $controller_helper, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \blitze\content\services\helper $helper)
47
	{
48
		$this->config = $config;
49
		$this->controller_helper = $controller_helper;
50
		$this->phpbb_dispatcher = $phpbb_dispatcher;
51
		$this->language = $language;
52
		$this->template = $template;
53
		$this->user = $user;
54
		$this->helper = $helper;
55
	}
56
57
	/**
58
	 * @param string $type
59
	 * @param array $topic_data
60
	 * @param array $topic_tracking_info
61
	 * @return array
62
	 */
63
	public function get_min_topic_info($type, array &$topic_data, array $topic_tracking_info)
64
	{
65
		$topic_id = $topic_data['topic_id'];
66
		$post_unread = (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
67
		$topic_data['topic_url'] = $this->get_topic_url($type, $topic_data);
68
69
		return array(
70
			'TOPIC_ID'			=> $topic_data['topic_id'],
71
			'TOPIC_VIEWS'		=> $topic_data['topic_views'],
72
			'TOPIC_TITLE'		=> censor_text($topic_data['topic_title']),
0 ignored issues
show
Bug introduced by
The function censor_text 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

72
			'TOPIC_TITLE'		=> /** @scrutinizer ignore-call */ censor_text($topic_data['topic_title']),
Loading history...
73
			'TOPIC_DATE'		=> $this->user->format_date($topic_data['topic_time']),
74
			'TOPIC_URL'			=> $topic_data['topic_url'],
75
			'MINI_POST'			=> ($post_unread) ? $this->user->img('icon_post_target_unread', 'UNREAD_POST') : $this->user->img('icon_post_target', 'POST'),
76
			'S_REQ_MOD_INPUT'	=> $topic_data['req_mod_input'],
77
			'S_UNREAD_POST'		=> $post_unread,
78
		);
79
	}
80
81
	/**
82
	 * @param string $type
83
	 * @param array $topic_data
84
	 * @param array $post_data
85
	 * @param array $users_cache
86
	 * @param array $attachments
87
	 * @param array $topic_tracking_info
88
	 * @param array $update_count
89
	 * @return array
90
	 */
91
	public function get_summary_template_data($type, array &$topic_data, array $post_data, array $users_cache, array &$attachments, array $topic_tracking_info, array &$update_count)
92
	{
93
		$tpl_data = array_merge(
94
			(empty($topic_data['topic_url'])) ? $this->get_min_topic_info($type, $topic_data, $topic_tracking_info) : array(),
95
			array(
96
				'POST_ID'				=> $post_data['post_id'],
97
				'POSTER_ID'				=> $post_data['poster_id'],
98
				'UPDATED'				=> max($post_data['post_edit_time'], $topic_data['topic_time']),
99
				'MESSAGE'				=> $this->get_parsed_text($post_data, $attachments, $update_count),
100
101
				'S_TOPIC_TYPE'			=> $topic_data['topic_type'],
102
				'S_HAS_ATTACHMENTS'		=> $topic_data['topic_attachment'],
103
				'S_HAS_POLL'			=> (bool) $post_data['poll_start'],
104
			),
105
			$this->get_topic_status_data($type, $topic_data, $post_data),
106
			array_change_key_case($users_cache[$post_data['poster_id']], CASE_UPPER)
107
		);
108
109
		/**
110
		 * Event to modify template data
111
		 *
112
		 * @event blitze.content.modify_template_data
113
		 * @var	array	tpl_data	Array containing template data
114
		 */
115
		$vars = array('tpl_data');
116
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.modify_template_data', compact($vars)));
117
118
		return $tpl_data;
119
	}
120
121
	/**
122
	 * @param string $type
123
	 * @param array $topic_data
124
	 * @param array $post_data
125
	 * @param array $users_cache
126
	 * @param array $attachments
127
	 * @param array $topic_tracking_info
128
	 * @param array $update_count
129
	 * @param string $mode
130
	 * @return array
131
	 */
132
	public function get_detail_template_data($type, array &$topic_data, array $post_data, array $users_cache, array &$attachments, array $topic_tracking_info, array &$update_count, $mode = '')
133
	{
134
		return array_merge(
135
			$this->get_summary_template_data($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count),
136
			$this->show_delete_reason($post_data, $users_cache),
137
			$this->show_edit_reason($post_data, $users_cache),
138
			array(
139
				'S_DISPLAY_NOTICE'		=> $this->helper->display_attachments_notice($post_data),
140
				'S_DELETE_PERMANENT'	=> $this->helper->permanent_delete_allowed($post_data),
141
				'S_IS_LOCKED'			=> $this->helper->topic_is_locked($topic_data),
142
143
				'U_EDIT'			=> $this->helper->get_edit_url($post_data, $topic_data, $mode),
144
				'U_QUOTE'			=> $this->helper->get_quote_url($post_data, $topic_data),
145
				'U_INFO'			=> $this->helper->get_info_url($post_data),
146
				'U_DELETE'			=> $this->helper->get_delete_url($post_data, $topic_data, $mode),
147
				'U_REPORT'			=> $this->helper->can_report_post($post_data['forum_id']) ? $this->controller_helper->route('phpbb_report_post_controller', array('id' => $post_data['post_id'])) : '',
148
				'U_APPROVE_ACTION'	=> $this->helper->get_approve_url($post_data, $topic_data['topic_url']),
149
				'U_MCP_EDIT'		=> $this->helper->get_mcp_edit_url($post_data, $topic_data),
150
				'U_MCP_RESTORE'		=> $this->helper->get_mcp_restore_url($post_data, $topic_data),
151
				'U_NOTES'			=> $this->helper->get_notes_url($post_data),
152
				'U_WARN'			=> $this->helper->get_warning_url($post_data),
153
			)
154
		);
155
	}
156
157
	/**
158
	 * @param array $topic_data
159
	 * @return array
160
	 */
161
	public function get_topic_tools_data(array $topic_data)
162
	{
163
		return array_merge(array(
164
				'U_PRINT_TOPIC'		=> $this->helper->get_print_topic_url($topic_data),
165
				'U_EMAIL_TOPIC'		=> $this->helper->get_email_topic_url($topic_data),
166
			),
167
			$this->get_watch_status_data($topic_data),
168
			$this->get_bookmark_status_data($topic_data)
169
		);
170
	}
171
172
	/**
173
	 * @param string $type
174
	 * @param array $topic_data
175
	 * @return string
176
	 */
177
	public function get_topic_url($type, array $topic_data)
178
	{
179
		return $this->controller_helper->route('blitze_content_show', array(
180
			'type'		=> $type,
181
			'topic_id'	=> $topic_data['topic_id'],
182
			'slug'		=> $topic_data['topic_slug']
183
		));
184
	}
185
186
	/**
187
	 * @param array $attachments
188
	 * @param int $post_id
189
	 * @param string $handle
190
	 * @return void
191
	 */
192
	 public function show_attachments(array $attachments, $post_id, $handle = 'attachment')
193
	 {
194
		if (!empty($attachments[$post_id]))
195
		{
196
			foreach ($attachments[$post_id] as $attachment)
197
			{
198
				$this->template->assign_block_vars($handle, array(
199
					'DISPLAY_ATTACHMENT' => $attachment)
200
				);
201
			}
202
		}
203
	 }
204
205
	/**
206
	 * @param array $post_data
207
	 * @param array $attachments
208
	 * @param array $update_count
209
	 * @return array
210
	 */
211
	protected function get_parsed_text(array $post_data, array &$attachments, array &$update_count)
212
	{
213
		$parse_flags = ($post_data['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
2 ignored issues
show
Bug introduced by
The constant blitze\content\services\OPTION_FLAG_BBCODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant blitze\content\services\OPTION_FLAG_SMILIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
214
		$message = generate_text_for_display($post_data['post_text'], $post_data['bbcode_uid'], $post_data['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

214
		$message = /** @scrutinizer ignore-call */ generate_text_for_display($post_data['post_text'], $post_data['bbcode_uid'], $post_data['bbcode_bitfield'], $parse_flags, true);
Loading history...
215
216
		if (!empty($attachments[$post_data['post_id']]))
217
		{
218
			parse_attachments($post_data['forum_id'], $message, $attachments[$post_data['post_id']], $update_count);
0 ignored issues
show
Bug introduced by
The function parse_attachments 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

218
			/** @scrutinizer ignore-call */ 
219
   parse_attachments($post_data['forum_id'], $message, $attachments[$post_data['post_id']], $update_count);
Loading history...
219
		}
220
221
		return $message;
222
	}
223
224
	/**
225
	 * @param string $type
226
	 * @param array $topic_data
227
	 * @param array $post_data
228
	 * @return array
229
	 */
230
	protected function get_topic_status_data($type, array $topic_data, array $post_data)
231
	{
232
		return array(
233
			'S_POST_UNAPPROVED'		=> $this->helper->post_is_unapproved($post_data),
234
			'S_POSTS_UNAPPROVED'	=> $this->helper->topic_has_unapproved_posts($topic_data),
235
			'S_TOPIC_REPORTED'		=> $this->helper->topic_is_reported($topic_data),
236
			'S_TOPIC_DELETED'		=> $topic_data['topic_visibility'] == ITEM_DELETED,
1 ignored issue
show
Bug introduced by
The constant blitze\content\services\ITEM_DELETED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
237
238
			'U_MINI_POST'			=> $this->get_mini_post_url($topic_data, $post_data),
239
			'U_MCP_REPORT'			=> $this->helper->get_mcp_report_url($post_data),
240
			'U_MCP_REVIEW'			=> $this->helper->get_mcp_review_url($type, $topic_data['topic_id']),
241
			'U_MCP_QUEUE'			=> $this->helper->get_mcp_queue_url($topic_data['topic_id']),
242
		);
243
	}
244
245
	/**
246
	 * @param array $topic_data
247
	 * @param array $post_data
248
	 * @return string
249
	 */
250
	protected function get_mini_post_url(array $topic_data, array $post_data)
251
	{
252
		if ($topic_data['topic_first_post_id'] === $post_data['post_id'])
253
		{
254
			return append_sid($topic_data['topic_url'], 'view=unread') . '#unread';
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

254
			return /** @scrutinizer ignore-call */ append_sid($topic_data['topic_url'], 'view=unread') . '#unread';
Loading history...
255
		}
256
257
		return append_sid($topic_data['topic_url'], 'p=' . $post_data['post_id']) . '#p' . $post_data['post_id'];
258
	}
259
260
	/**
261
	 * @param array $row
262
	 * @param array $users_cache
263
	 * @return array
264
	 */
265
	protected function show_edit_reason(array $row, array $users_cache)
266
	{
267
		$l_edited_by = $edit_reason = '';
268
		if (($row['post_edit_count'] && $this->config['display_last_edited']) || $row['post_edit_reason'])
269
		{
270
			$display_username	= $users_cache[$row['poster_id']]['username_full'];
271
			$l_edited_by = $this->language->lang('EDITED_TIMES_TOTAL', (int) $row['post_edit_count'], $display_username, $this->user->format_date($row['post_edit_time'], false, true));
272
			$edit_reason = $row['post_edit_reason'];
273
		}
274
275
		return array(
276
			'EDITED_MESSAGE'	=> $l_edited_by,
277
			'EDIT_REASON'		=> $edit_reason,
278
		);
279
	}
280
281
	/**
282
	 * @param array $row
283
	 * @param array $users_cache
284
	 * @return array
285
	 */
286
	protected function show_delete_reason(array $row, array $users_cache)
287
	{
288
		$l_deleted_by = $delete_reason = $l_deleted_message = '';
289
		$s_post_deleted = ($row['post_visibility'] == ITEM_DELETED) ? true : false;
1 ignored issue
show
Bug introduced by
The constant blitze\content\services\ITEM_DELETED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
290
291
		if ($s_post_deleted && $row['post_delete_user'])
292
		{
293
			$display_postername	= $users_cache[$row['poster_id']]['username_full'];
294
			$display_username	= $users_cache[$row['post_delete_user']]['username_full'];
295
296
			$l_deleted_message = $this->get_delete_message($row, $display_postername, $display_username);
297
			$l_deleted_by = $this->language->lang('DELETED_INFORMATION', $display_username, $this->user->format_date($row['post_delete_time'], false, true));
298
			$delete_reason = $row['post_delete_reason'];
299
		}
300
301
		return array(
302
			'DELETED_MESSAGE'			=> $l_deleted_by,
303
			'DELETE_REASON'				=> $delete_reason,
304
			'L_POST_DELETED_MESSAGE'	=> $l_deleted_message,
305
			'S_POST_DELETED'			=> $s_post_deleted,
306
		);
307
	}
308
309
	/**
310
	 * @param array $row
311
	 * @param string $display_postername
312
	 * @param string $display_username
313
	 * @return string
314
	 */
315
	protected function get_delete_message(array $row, $display_postername, $display_username)
316
	{
317
		if ($row['post_delete_reason'])
318
		{
319
			return $this->language->lang('POST_DELETED_BY_REASON', $display_postername, $display_username, $this->user->format_date($row['post_delete_time'], false, true), $row['post_delete_reason']);
320
		}
321
		else
322
		{
323
			return $this->language->lang('POST_DELETED_BY', $display_postername, $display_username, $this->user->format_date($row['post_delete_time'], false, true));
324
		}
325
	}
326
327
	/**
328
	 * @param array $topic_data
329
	 * @return array
330
	 */
331
	protected function get_watch_status_data(array $topic_data)
332
	{
333
		$s_watching_topic = array(
334
			'link'			=> '',
335
			'link_toggle'	=> '',
336
			'title'			=> '',
337
			'title_toggle'	=> '',
338
			'is_watching'	=> false,
339
		);
340
341
		if ($this->config['allow_topic_notify'])
342
		{
343
			$notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
344
			watch_topic_forum('topic', $s_watching_topic, $this->user->data['user_id'], $topic_data['forum_id'], $topic_data['topic_id'], $notify_status, 0, $topic_data['topic_title']);
0 ignored issues
show
Bug introduced by
The function watch_topic_forum 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

344
			/** @scrutinizer ignore-call */ 
345
   watch_topic_forum('topic', $s_watching_topic, $this->user->data['user_id'], $topic_data['forum_id'], $topic_data['topic_id'], $notify_status, 0, $topic_data['topic_title']);
Loading history...
345
		}
346
347
		return array(
348
			'U_WATCH_TOPIC'			=> $s_watching_topic['link'],
349
			'U_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['link_toggle'],
350
			'S_WATCH_TOPIC_TITLE'	=> $s_watching_topic['title'],
351
			'S_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['title_toggle'],
352
			'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
353
		);
354
	}
355
356
	/**
357
	 * @param array $topic_data
358
	 * @return array
359
	 */
360
	protected function get_bookmark_status_data(array $topic_data)
361
	{
362
		$bookmarked = (bool) $topic_data['bookmarked'];
363
		$state_key = (int) $bookmarked;
364
		$lang_keys = array(
365
			'toggle'	=> array('BOOKMARK_TOPIC_REMOVE', 'BOOKMARK_TOPIC'),
366
			'bookmark'	=> array('BOOKMARK_TOPIC', 'BOOKMARK_TOPIC_REMOVE'),
367
		);
368
369
		return array(
370
			'U_BOOKMARK_TOPIC'		=> ($this->user->data['is_registered'] && $this->config['allow_bookmarks']) ? $this->helper->get_viewtopic_url($topic_data) . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_{$topic_data['topic_id']}") : '',
0 ignored issues
show
Bug introduced by
The function generate_link_hash 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

370
			'U_BOOKMARK_TOPIC'		=> ($this->user->data['is_registered'] && $this->config['allow_bookmarks']) ? $this->helper->get_viewtopic_url($topic_data) . '&amp;bookmark=1&amp;hash=' . /** @scrutinizer ignore-call */ generate_link_hash("topic_{$topic_data['topic_id']}") : '',
Loading history...
371
			'S_BOOKMARK_TOPIC'		=> $this->language->lang($lang_keys['bookmark'][$state_key]),
372
			'S_BOOKMARK_TOGGLE'		=> $this->language->lang($lang_keys['toggle'][$state_key]),
373
			'S_BOOKMARKED_TOPIC'	=> $bookmarked,
374
		);
375
376
	}
377
}
378