Completed
Push — master ( c40e0f...de120c )
by
unknown
01:51 queued 12s
created

listener   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 2
dl 0
loc 324
rs 9.2
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 11 1
A ideas_forum_redirect() 0 9 3
A __construct() 0 14 1
A show_post_buttons() 0 13 3
D show_idea() 0 99 17
A adjust_quickmod_tools() 0 25 2
B viewonline_ideas() 0 21 6
A edit_idea_title() 0 13 5
A is_ideas_forum() 0 4 1
A is_first_post() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like listener often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use listener, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\event;
12
13
use phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\controller\helper;
16
use phpbb\ideas\factory\ideas;
17
use phpbb\ideas\factory\linkhelper;
18
use phpbb\language\language;
19
use phpbb\template\template;
20
use phpbb\user;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
class listener implements EventSubscriberInterface
24
{
25
	/** @var auth */
26
	protected $auth;
27
28
	/* @var config */
29
	protected $config;
30
31
	/* @var helper */
32
	protected $helper;
33
34
	/* @var ideas */
35
	protected $ideas;
36
37
	/** @var language */
38
	protected $language;
39
40
	/* @var linkhelper */
41
	protected $link_helper;
42
43
	/* @var template */
44
	protected $template;
45
46
	/* @var user */
47
	protected $user;
48
49
	/** @var string */
50
	protected $php_ext;
51
52
	/**
53
	 * @param \phpbb\auth\auth                $auth
54
	 * @param \phpbb\config\config            $config
55
	 * @param \phpbb\controller\helper        $helper
56
	 * @param \phpbb\ideas\factory\ideas      $ideas
57
	 * @param \phpbb\language\language        $language
58
	 * @param \phpbb\ideas\factory\linkhelper $link_helper
59
	 * @param \phpbb\template\template        $template
60
	 * @param \phpbb\user                     $user
61
	 * @param string                          $php_ext
62
	 */
63
	public function __construct(auth $auth, config $config, helper $helper, ideas $ideas, language $language, linkhelper $link_helper, template $template, user $user, $php_ext)
64
	{
65
		$this->auth = $auth;
66
		$this->config = $config;
67
		$this->helper = $helper;
68
		$this->ideas = $ideas;
69
		$this->language = $language;
70
		$this->link_helper = $link_helper;
71
		$this->template = $template;
72
		$this->user = $user;
73
		$this->php_ext = $php_ext;
74
75
		$this->language->add_lang('common', 'phpbb/ideas');
76
	}
77
78
	/**
79
	 * @inheritDoc
80
	 */
81
	public static function getSubscribedEvents()
82
	{
83
		return array(
84
			'core.viewforum_get_topic_data'				=> 'ideas_forum_redirect',
85
			'core.viewtopic_modify_post_row'			=> 'show_post_buttons',
86
			'core.viewtopic_modify_page_title'			=> 'show_idea',
87
			'core.viewtopic_add_quickmod_option_before'	=> 'adjust_quickmod_tools',
88
			'core.viewonline_overwrite_location'		=> 'viewonline_ideas',
89
			'core.posting_modify_submit_post_after'		=> 'edit_idea_title',
90
		);
91
	}
92
93
	/**
94
	 * Redirect users from the forum to the Ideas centre
95
	 *
96
	 * @param \phpbb\event\data $event The event object
97
	 * @return void
98
	 * @access public
99
	 */
100
	public function ideas_forum_redirect($event)
101
	{
102
		if ($this->is_ideas_forum($event['forum_id']))
103
		{
104
			// Use the custom base url if set, otherwise default to normal routing
105
			$url = $this->config['ideas_base_url'] ?: $this->helper->route('phpbb_ideas_index_controller');
106
			redirect($url);
107
		}
108
	}
109
110
	/**
111
	 * Show post buttons (hide delete, quote or warn user buttons)
112
	 *
113
	 * @param \phpbb\event\data $event The event object
114
	 * @return void
115
	 * @access public
116
	 */
117
	public function show_post_buttons($event)
118
	{
119
		if (!$this->is_ideas_forum($event['row']['forum_id']))
120
		{
121
			return;
122
		}
123
124
		if ($this->is_first_post($event['topic_data']['topic_first_post_id'], $event['row']['post_id']))
125
		{
126
			$event->update_subarray('post_row', 'U_DELETE', false);
127
			$event->update_subarray('post_row', 'U_WARN', false);
128
		}
129
	}
130
131
	/**
132
	 * Show the idea related to the current topic
133
	 *
134
	 * @param \phpbb\event\data $event The event object
135
	 * @return void
136
	 * @access public
137
	 */
138
	public function show_idea($event)
139
	{
140
		if (!$this->is_ideas_forum($event['forum_id']))
141
		{
142
			return;
143
		}
144
145
		$idea = $this->ideas->get_idea_by_topic_id($event['topic_data']['topic_id']);
146
147
		if (!$idea)
148
		{
149
			return;
150
		}
151
152
		$mod = $this->auth->acl_get('m_', (int) $this->config['ideas_forum_id']);
153
		$own = $idea['idea_author'] === $this->user->data['user_id'];
154
155
		if ($mod)
156
		{
157
			$this->template->assign_var('STATUS_ARY', ideas::$statuses);
158
159
			// Add quick mod option for deleting an idea
160
			$this->template->alter_block_array('quickmod', array(
161
				'VALUE'		=> 'delete_topic', // delete topic is used here simply to enable ajax
162
				'TITLE'		=> $this->language->lang('DELETE_IDEA'),
163
				'LINK'		=> $this->link_helper->get_idea_link($idea['idea_id'], 'delete'),
164
			));
165
		}
166
167
		$points = $idea['idea_votes_up'] - $idea['idea_votes_down'];
168
		$can_vote = (bool) ($idea['idea_status'] != ideas::$statuses['IMPLEMENTED'] &&
169
			$idea['idea_status'] != ideas::$statuses['DUPLICATE'] &&
170
			$this->auth->acl_get('f_vote', (int) $this->config['ideas_forum_id']) &&
171
			$event['topic_data']['topic_status'] != ITEM_LOCKED);
172
173
		$s_voted_up = $s_voted_down = false;
174
		if ($idea['idea_votes_up'] || $idea['idea_votes_down'])
175
		{
176
			$votes = $this->ideas->get_voters($idea['idea_id']);
177
178
			foreach ($votes as $vote)
179
			{
180
				$this->template->assign_block_vars('votes_' . ($vote['vote_value'] ? 'up' : 'down'), array(
181
					'USER' => $vote['user'],
182
				));
183
184
				if ($this->user->data['user_id'] == $vote['user_id'])
185
				{
186
					$s_voted_up = ((int) $vote['vote_value'] === 1);
187
					$s_voted_down = ((int) $vote['vote_value'] === 0);
188
				}
189
			}
190
		}
191
192
		$this->template->assign_vars(array(
193
			'IDEA_ID'			=> $idea['idea_id'],
194
			'IDEA_TITLE'		=> $idea['idea_title'],
195
			'IDEA_AUTHOR'		=> $this->link_helper->get_user_link($idea['idea_author']),
196
			'IDEA_DATE'			=> $this->user->format_date($idea['idea_date']),
197
			'IDEA_VOTES'		=> $idea['idea_votes_up'] + $idea['idea_votes_down'],
198
			'IDEA_VOTES_UP'		=> $idea['idea_votes_up'],
199
			'IDEA_VOTES_DOWN'	=> $idea['idea_votes_down'],
200
			'IDEA_POINTS'		=> $points,
201
			'IDEA_STATUS_ID'	=> $idea['idea_status'],
202
			'IDEA_STATUS_NAME'	=> $this->ideas->get_status_from_id($idea['idea_status']),
203
204
			'IDEA_DUPLICATE'	=> $idea['duplicate_id'] ? $this->ideas->get_title($idea['duplicate_id']) : '',
205
			'IDEA_RFC'			=> $idea['rfc_link'],
206
			'IDEA_TICKET'		=> $idea['ticket_id'],
207
			'IDEA_IMPLEMENTED'	=> $idea['implemented_version'],
208
209
			'S_IS_MOD'			=> $mod,
210
			'S_CAN_EDIT'		=> $mod || $own,
211
			'S_CAN_VOTE'		=> $can_vote,
212
			'S_CAN_VOTE_UP'		=> $can_vote && !$s_voted_up,
213
			'S_CAN_VOTE_DOWN'	=> $can_vote && !$s_voted_down,
214
			'S_VOTED'			=> $s_voted_up || $s_voted_down,
215
			'S_VOTED_UP'		=> $s_voted_up,
216
			'S_VOTED_DOWN'		=> $s_voted_down,
217
218
			'U_CHANGE_STATUS'	=> $this->link_helper->get_idea_link($idea['idea_id'], 'status', true),
219
			'U_EDIT_DUPLICATE'	=> $this->link_helper->get_idea_link($idea['idea_id'], 'duplicate', true),
220
			'U_EDIT_RFC'		=> $this->link_helper->get_idea_link($idea['idea_id'], 'rfc', true),
221
			'U_EDIT_IMPLEMENTED'=> $this->link_helper->get_idea_link($idea['idea_id'], 'implemented', true),
222
			'U_EDIT_TICKET'		=> $this->link_helper->get_idea_link($idea['idea_id'], 'ticket', true),
223
			'U_REMOVE_VOTE'		=> $this->link_helper->get_idea_link($idea['idea_id'], 'removevote', true),
224
			'U_IDEA_VOTE'		=> $this->link_helper->get_idea_link($idea['idea_id'], 'vote', true),
225
			'U_IDEA_DUPLICATE'	=> $this->link_helper->get_idea_link($idea['duplicate_id']),
226
			'U_IDEA_STATUS_LINK'=> $this->helper->route('phpbb_ideas_list_controller', array('status' => $idea['idea_status'])),
227
			'U_TITLE_LIVESEARCH'=> $this->helper->route('phpbb_ideas_livesearch_controller'),
228
		));
229
230
		// Use Ideas breadcrumbs
231
		$this->template->destroy_block_vars('navlinks');
232
		$this->template->assign_block_vars('navlinks', array(
233
			'U_VIEW_FORUM'		=> $this->helper->route('phpbb_ideas_index_controller'),
234
			'FORUM_NAME'		=> $this->language->lang('IDEAS'),
235
		));
236
	}
237
238
	/**
239
	 * Adjust the QuickMod tools displayed
240
	 * (hide options to delete, restore, make global, sticky or announcement)
241
	 *
242
	 * @param \phpbb\event\data $event The event object
243
	 * @return void
244
	 * @access public
245
	 */
246
	public function adjust_quickmod_tools($event)
247
	{
248
		if (!$this->is_ideas_forum($event['forum_id']))
249
		{
250
			return;
251
		}
252
253
		$quickmod_array = $event['quickmod_array'];
254
255
		//$quickmod_array['lock'][1] = false;
256
		//$quickmod_array['unlock'][1] = false;
257
		$quickmod_array['delete_topic'][1] = false;
258
		$quickmod_array['restore_topic'][1] = false;
259
		//$quickmod_array['move'][1] = false;
260
		//$quickmod_array['split'][1] = false;
261
		//$quickmod_array['merge'][1] = false;
262
		//$quickmod_array['merge_topic'][1] = false;
263
		//$quickmod_array['fork'][1] = false;
264
		$quickmod_array['make_normal'][1] = false;
265
		$quickmod_array['make_sticky'][1] = false;
266
		$quickmod_array['make_announce'][1] = false;
267
		$quickmod_array['make_global'][1] = false;
268
269
		$event['quickmod_array'] = $quickmod_array;
270
	}
271
272
	/**
273
	 * Show users as viewing Ideas on Who Is Online page
274
	 *
275
	 * @param \phpbb\event\data $event The event object
276
	 * @return void
277
	 * @access public
278
	 */
279
	public function viewonline_ideas($event)
280
	{
281
		if ($event['on_page'][1] === 'app')
282
		{
283
			if (strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas/post') === 0)
284
			{
285
				$event['location'] = $this->language->lang('POSTING_NEW_IDEA');
286
				$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
287
			}
288
			else if (strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas') === 0)
289
			{
290
				$event['location'] = $this->language->lang('VIEWING_IDEAS');
291
				$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
292
			}
293
		}
294
		else if ($event['on_page'][1] === 'viewtopic' && $event['row']['session_forum_id'] == $this->config['ideas_forum_id'])
295
		{
296
			$event['location'] = $this->language->lang('VIEWING_IDEAS');
297
			$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
298
		}
299
	}
300
301
	/**
302
	 * Update the idea's title when post title is edited.
303
	 *
304
	 * @param \phpbb\event\data $event The event object
305
	 * @return void
306
	 * @access public
307
	 */
308
	public function edit_idea_title($event)
309
	{
310
		if ($event['mode'] !== 'edit' ||
311
			!$event['update_subject'] ||
312
			!$this->is_ideas_forum($event['forum_id']) ||
313
			!$this->is_first_post($event['post_data']['topic_first_post_id'], $event['post_id']))
314
		{
315
			return;
316
		}
317
318
		$idea = $this->ideas->get_idea_by_topic_id($event['topic_id']);
319
		$this->ideas->set_title($idea['idea_id'], $event['post_data']['post_subject']);
320
	}
321
322
	/**
323
	 * Check if forum id is for the ideas the forum
324
	 *
325
	 * @param int $forum_id
326
	 * @return bool
327
	 * @access public
328
	 */
329
	protected function is_ideas_forum($forum_id)
330
	{
331
		return (int) $forum_id === (int) $this->config['ideas_forum_id'];
332
	}
333
334
	/**
335
	 * Check if a post is the first post in a topic
336
	 *
337
	 * @param int|string $topic_first_post_id
338
	 * @param int|string $post_id
339
	 * @return bool
340
	 * @access protected
341
	 */
342
	protected function is_first_post($topic_first_post_id, $post_id)
343
	{
344
		return (int) $topic_first_post_id === (int) $post_id;
345
	}
346
}
347