Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

topic::get_detail_template_data()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 23
cp 0
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 19
nc 1
nop 8
crap 6

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 */
15
	protected $config;
16
17
	/** @var \phpbb\controller\helper */
18
	protected $controller_helper;
19
20
	/** @var \phpbb\event\dispatcher_interface */
21
	protected $phpbb_dispatcher;
22
23
	/** @var \phpbb\language\language */
24
	protected $language;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var \phpbb\user */
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']),
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
				'MESSAGE'				=> $this->get_parsed_text($post_data, $attachments, $update_count),
99
100
				'S_TOPIC_TYPE'			=> $topic_data['topic_type'],
101
				'S_HAS_ATTACHMENTS'		=> $topic_data['topic_attachment'],
102
				'S_HAS_POLL'			=> (bool) $post_data['poll_start'],
103
			),
104
			$this->get_topic_status_data($type, $topic_data, $post_data),
105
			array_change_key_case($users_cache[$post_data['poster_id']], CASE_UPPER)
106
		);
107
108
		/**
109
		 * Event to modify template data
110
		 *
111
		 * @event blitze.content.modify_template_data
112
		 * @var	array	tpl_data	Array containing template data
113
		 */
114
		$vars = array('tpl_data');
115
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.modify_template_data', compact($vars)));
116
117
		return $tpl_data;
118
	}
119
120
	/**
121
	 * @param string $type
122
	 * @param array $topic_data
123
	 * @param array $post_data
124
	 * @param array $users_cache
125
	 * @param array $attachments
126
	 * @param array $topic_tracking_info
127
	 * @param array $update_count
128
	 * @param string $mode
129
	 * @return array
130
	 */
131
	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 = '')
132
	{
133
		return array_merge(
134
			$this->get_summary_template_data($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count),
135
			$this->show_delete_reason($post_data, $users_cache),
136
			$this->show_edit_reason($post_data, $users_cache),
137
			array(
138
				'S_DISPLAY_NOTICE'		=> $this->helper->display_attachments_notice($post_data),
139
				'S_DELETE_PERMANENT'	=> $this->helper->permanent_delete_allowed($post_data),
140
				'S_IS_LOCKED'			=> $this->helper->topic_is_locked($topic_data),
141
142
				'U_EDIT'			=> $this->helper->get_edit_url($post_data, $topic_data, $mode),
143
				'U_QUOTE'			=> $this->helper->get_quote_url($post_data, $topic_data),
144
				'U_INFO'			=> $this->helper->get_info_url($post_data),
145
				'U_DELETE'			=> $this->helper->get_delete_url($post_data, $topic_data, $mode),
146
				'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'])) : '',
147
				'U_APPROVE_ACTION'	=> $this->helper->get_approve_url($post_data, $topic_data['topic_url']),
148
				'U_MCP_EDIT'		=> $this->helper->get_mcp_edit_url($post_data, $topic_data),
149
				'U_MCP_RESTORE'		=> $this->helper->get_mcp_restore_url($post_data, $topic_data),
150
				'U_NOTES'			=> $this->helper->get_notes_url($post_data),
151
				'U_WARN'			=> $this->helper->get_warning_url($post_data),
152
			)
153
		);
154
	}
155
156
	/**
157
	 * @param string $type
158
	 * @param array $topic_data
159
	 * @return string
160
	 */
161
	public function get_topic_url($type, array $topic_data)
162
	{
163
		return $this->controller_helper->route('blitze_content_show', array(
164
			'type'		=> $type,
165
			'topic_id'	=> $topic_data['topic_id'],
166
			'slug'		=> $topic_data['topic_slug']
167
		));
168
	}
169
170
	/**
171
	 * @param array $attachments
172
	 * @param int $post_id
173
	 * @param string $handle
174
	 * @return void
175
	 */
176
	 public function show_attachments(array $attachments, $post_id, $handle = 'attachment')
177
	 {
178
		if (!empty($attachments[$post_id]))
179
		{
180
			foreach ($attachments[$post_id] as $attachment)
181
			{
182
				$this->template->assign_block_vars($handle, array(
183
					'DISPLAY_ATTACHMENT' => $attachment)
184
				);
185
			}
186
		}
187
	 }
188
189
	/**
190
	 * @param array $post_data
191
	 * @param array $attachments
192
	 * @param array $update_count
193
	 * @return array
194
	 */
195
	protected function get_parsed_text(array $post_data, array &$attachments, array &$update_count)
196
	{
197
		$parse_flags = ($post_data['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
198
		$message = generate_text_for_display($post_data['post_text'], $post_data['bbcode_uid'], $post_data['bbcode_bitfield'], $parse_flags, true);
199
200
		if (!empty($attachments[$post_data['post_id']]))
201
		{
202
			parse_attachments($post_data['forum_id'], $message, $attachments[$post_data['post_id']], $update_count);
203
		}
204
205
		return $message;
206
	}
207
208
	/**
209
	 * @param string $type
210
	 * @param array $topic_data
211
	 * @param array $post_data
212
	 * @return array
213
	 */
214
	 protected function get_topic_status_data($type, array $topic_data, array $post_data)
215
	 {
216
	 	return array(
217
			'S_POST_UNAPPROVED'		=> $this->helper->post_is_unapproved($post_data),
218
			'S_POSTS_UNAPPROVED'	=> $this->helper->topic_has_unapproved_posts($topic_data),
219
			'S_TOPIC_REPORTED'		=> $this->helper->topic_is_reported($topic_data),
220
			'S_TOPIC_DELETED'		=> $topic_data['topic_visibility'] == ITEM_DELETED,
221
222
			'U_MINI_POST'			=> $this->get_mini_post_url($topic_data, $post_data),
223
			'U_MCP_REPORT'			=> $this->helper->get_mcp_report_url($post_data),
224
			'U_MCP_REVIEW'			=> $this->helper->get_mcp_review_url($type, $topic_data['topic_id']),
225
			'U_MCP_QUEUE'			=> $this->helper->get_mcp_queue_url($topic_data['topic_id']),
226
	 	);
227
	 }
228
229
	/**
230
	 * @param array $topic_data
231
	 * @param array $post_data
232
	 * @return array
233
	 */
234
	 protected function get_mini_post_url(array $topic_data, array $post_data)
235
	 {
236
		if ($topic_data['topic_first_post_id'] === $post_data['post_id'])
237
		{
238
			return append_sid($topic_data['topic_url'], 'view=unread') . '#unread';
239
		}
240
241
 		return append_sid($topic_data['topic_url'], 'p=' . $post_data['post_id']) . '#p' . $post_data['post_id'];
242
	 }
243
244
	/**
245
	 * @param array $row
246
	 * @param array $users_cache
247
	 * @return array
248
	 */
249
	protected function show_edit_reason(array $row, array $users_cache)
250
	{
251
		$l_edited_by = $edit_reason = '';
252
		if (($row['post_edit_count'] && $this->config['display_last_edited']) || $row['post_edit_reason'])
253
		{
254
			$display_username	= $users_cache[$row['poster_id']]['username_full'];
255
			$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));
256
			$edit_reason = $row['post_edit_reason'];
257
		}
258
259
		return array(
260
			'EDITED_MESSAGE'	=> $l_edited_by,
261
			'EDIT_REASON'		=> $edit_reason,
262
		);
263
	}
264
265
	/**
266
	 * @param array $row
267
	 * @param array $users_cache
268
	 * @return array
269
	 */
270
	protected function show_delete_reason(array $row, array $users_cache)
271
	{
272
		$l_deleted_by = $delete_reason = $l_deleted_message = '';
273
		$s_post_deleted = ($row['post_visibility'] == ITEM_DELETED) ? true : false;
274
275
		if ($s_post_deleted && $row['post_delete_user'])
276
		{
277
			$display_postername	= $users_cache[$row['poster_id']]['username_full'];
0 ignored issues
show
Unused Code introduced by
$display_postername is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
278
			$display_username	= $users_cache[$row['post_delete_user']]['username_full'];
279
280
			$l_deleted_message = $this->get_deleted_message();
0 ignored issues
show
Bug introduced by
The method get_deleted_message() does not exist on blitze\content\services\topic. Did you maybe mean get_delete_message()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
281
			$l_deleted_by = $this->language->lang('DELETED_INFORMATION', $display_username, $this->user->format_date($row['post_delete_time'], false, true));
282
			$delete_reason = $row['post_delete_reason'];
283
		}
284
285
		return array(
286
			'DELETED_MESSAGE'			=> $l_deleted_by,
287
			'DELETE_REASON'				=> $delete_reason,
288
			'L_POST_DELETED_MESSAGE'	=> $l_deleted_message,
289
			'S_POST_DELETED'			=> $s_post_deleted,
290
		);
291
	}
292
293
	/**
294
	 * @param array $row
295
	 * @param string $display_postername
296
	 * @param string $display_username
297
	 * @return string
298
	 */
299
	protected function get_delete_message(array $row, $display_postername, $display_username)
300
	{
301
		if ($row['post_delete_reason'])
302
		{
303
			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']);
304
		}
305
		else
306
		{
307
			return $this->language->lang('POST_DELETED_BY', $display_postername, $display_username, $this->user->format_date($row['post_delete_time'], false, true));
308
		}
309
	}
310
}
311