main_listener   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Importance

Changes 27
Bugs 1 Features 1
Metric Value
eloc 98
c 27
b 1
f 1
dl 0
loc 360
rs 9.0399
wmc 42

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A enable_media_sites() 0 17 4
A configure_url_parsing() 0 7 2
A add_custom_sites() 0 7 2
A getSubscribedEvents() 0 13 1
A append_agreement() 0 10 3
A setup_media_configs() 0 5 1
A modify_tag_templates() 0 12 2
A disable_media_embed() 0 14 3
A set_permissions() 0 4 1
A get_siteIds() 0 5 2
A check_signature() 0 6 4
A check_magic_urls() 0 5 3
A check_forum_permission() 0 6 3
A check_bbcode_enabled() 0 8 2
A media_embed_help() 0 20 2
A setup_media_bbcode() 0 4 1
A setup_cache_dir() 0 9 3
A check_pm_permission() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like main_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.

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 main_listener, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\mediaembed\event;
12
13
use 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...
14
use 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
use phpbb\config\db_text;
0 ignored issues
show
Bug introduced by
The type phpbb\config\db_text 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...
16
use 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...
17
use phpbb\mediaembed\collection\customsitescollection;
18
use phpbb\mediaembed\ext;
19
use 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...
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\EventD...ventSubscriberInterface 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
use Symfony\Component\Yaml\Yaml;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Yaml\Yaml 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...
22
23
/**
24
 * Event listener
25
 */
26
class main_listener implements EventSubscriberInterface
27
{
28
	/** @var string A link to a rich content media site for demo purposes */
29
	public const MEDIA_DEMO_URL = 'https://youtu.be/Ne18ZQ7LLI0';
30
31
	/** @var auth */
32
	protected $auth;
33
34
	/** @var config $config */
35
	protected $config;
36
37
	/** @var db_text $config_text */
38
	protected $config_text;
39
40
	/** @var language $language */
41
	protected $language;
42
43
	/** @var template $template */
44
	protected $template;
45
46
	/** @var customsitescollection $custom_sites */
47
	protected $custom_sites;
48
49
	/** @var string $cache_dir */
50
	protected $cache_dir;
51
52
	/** @var bool Disable the media embed plugin (plain url parsing) */
53
	protected $disable_plugin = false;
54
55
	/** @var bool Disable the media tag (bbcode parsing) */
56
	protected $disable_tag = false;
57
58
	/**
59
	 * {@inheritDoc}
60
	 */
61
	public static function getSubscribedEvents()
62
	{
63
		return [
64
			'core.text_formatter_s9e_configure_after'	=> [['add_custom_sites', 3], ['enable_media_sites', 2], ['configure_url_parsing', 1], ['modify_tag_templates', 0]],
65
			'core.display_custom_bbcodes'				=> 'setup_media_bbcode',
66
			'core.permissions'							=> 'set_permissions',
67
			'core.help_manager_add_block_before'		=> 'media_embed_help',
68
			'core.posting_modify_message_text'			=> 'check_forum_permission',
69
			'core.ucp_pm_compose_modify_parse_before'	=> 'check_pm_permission',
70
			'core.message_parser_check_message'			=> [['check_signature'], ['check_magic_urls'], ['check_bbcode_enabled']],
71
			'core.text_formatter_s9e_parser_setup'		=> [['disable_media_embed'], ['setup_cache_dir']],
72
			'core.page_header' 							=> 'setup_media_configs',
73
			'core.page_footer'							=> 'append_agreement',
74
		];
75
	}
76
77
	/**
78
	 * Constructor
79
	 *
80
	 * @param auth					$auth
81
	 * @param config					$config
82
	 * @param db_text				$config_text
83
	 * @param language				$language
84
	 * @param template				$template
85
	 * @param customsitescollection	$custom_sites
86
	 * @param string				$cache_dir
87
	 */
88
	public function __construct(auth $auth, config $config, db_text $config_text, language $language, template $template, customsitescollection $custom_sites, $cache_dir)
89
	{
90
		$this->auth = $auth;
91
		$this->config = $config;
92
		$this->language = $language;
93
		$this->template = $template;
94
		$this->config_text = $config_text;
95
		$this->custom_sites = $custom_sites;
96
		$this->cache_dir = $cache_dir;
97
	}
98
99
	/**
100
	 * Add any custom site definitions to the default MediaEmbed sites object
101
	 *
102
	 * @param \phpbb\event\data $event The event object
0 ignored issues
show
Bug introduced by
The type phpbb\event\data 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...
103
	 * @return void
104
	 */
105
	public function add_custom_sites($event)
106
	{
107
		foreach ($this->custom_sites->get_collection() as $site)
108
		{
109
			$event['configurator']->MediaEmbed->defaultSites->add(
110
				basename($site, ext::YML),
111
				Yaml::parse(file_get_contents($site))
112
			);
113
		}
114
	}
115
116
	/**
117
	 * Enable media sites
118
	 *
119
	 * @param \phpbb\event\data $event The event object
120
	 * @return void
121
	 */
122
	public function enable_media_sites($event)
123
	{
124
		foreach ($this->get_siteIds() as $siteId)
125
		{
126
			// skip media sites that already exist as a BBCode
127
			if (isset($event['configurator']->BBCodes[$siteId]))
128
			{
129
				continue;
130
			}
131
132
			try
133
			{
134
				$event['configurator']->MediaEmbed->add($siteId);
135
			}
136
			catch (\RuntimeException $e)
137
			{
138
				continue;
139
			}
140
		}
141
	}
142
143
	/**
144
	 * Configure plain URL parsing
145
	 *
146
	 * @param \phpbb\event\data $event The event object
147
	 * @return void
148
	 */
149
	public function configure_url_parsing($event)
150
	{
151
		// Disable plain url parsing?
152
		if (!$this->config->offsetGet('media_embed_parse_urls'))
153
		{
154
			$event['configurator']->MediaEmbed->finalize();
155
			unset($event['configurator']->MediaEmbed);
156
		}
157
	}
158
159
	/**
160
	 * Modify bbcode tag templates
161
	 *
162
	 * @param \phpbb\event\data $event The event object
163
	 * @return void
164
	 */
165
	public function modify_tag_templates($event)
166
	{
167
		try
168
		{
169
			// force YouTube to use the no cookies until the user starts video playback
170
			$tag = $event['configurator']->tags['YOUTUBE'];
171
			$tag->template = str_replace('www.youtube.com', 'www.youtube-nocookie.com', $tag->template);
172
173
			$event['configurator']->finalize();
174
		}
175
		catch (\RuntimeException $e)
176
		{
177
			// do nothing
178
		}
179
	}
180
181
	/**
182
	 * Set template switch for displaying the [media] BBCode button
183
	 *
184
	 * @return void
185
	 */
186
	public function setup_media_bbcode()
187
	{
188
		$this->language->add_lang('common', 'phpbb/mediaembed');
189
		$this->template->assign_var('S_BBCODE_MEDIA', $this->config->offsetGet('media_embed_bbcode'));
190
	}
191
192
	/**
193
	 * Set media embed forum and user PM permission
194
	 *
195
	 * @param	\phpbb\event\data	$event	The event object
196
	 * @return	void
197
	 */
198
	public function set_permissions($event)
199
	{
200
		$event->update_subarray('permissions', 'f_mediaembed', ['lang' => 'ACL_F_MEDIAEMBED', 'cat' => 'content']);
201
		$event->update_subarray('permissions', 'u_pm_mediaembed', ['lang' => 'ACL_U_PM_MEDIAEMBED', 'cat' => 'pm']);
202
	}
203
204
	/**
205
	 * Add Media Embed help to the BBCode Guide
206
	 *
207
	 * @param \phpbb\event\data $event The event object
208
	 * @return void
209
	 */
210
	public function media_embed_help($event)
211
	{
212
		if ($event['block_name'] === 'HELP_BBCODE_BLOCK_OTHERS')
213
		{
214
			$this->language->add_lang('help', 'phpbb/mediaembed');
215
216
			$this->template->assign_block_vars('faq_block', [
217
				'BLOCK_TITLE'	=> $this->language->lang('HELP_EMBEDDING_MEDIA'),
218
				'SWITCH_COLUMN'	=> false,
219
			]);
220
221
			$uid = $bitfield = $flags = '';
222
			$demo_text = self::MEDIA_DEMO_URL;
223
			generate_text_for_storage($demo_text, $uid, $bitfield, $flags, true, true);
0 ignored issues
show
Bug introduced by
The function generate_text_for_storage 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

223
			/** @scrutinizer ignore-call */ 
224
   generate_text_for_storage($demo_text, $uid, $bitfield, $flags, true, true);
Loading history...
224
			$demo_display = generate_text_for_display($demo_text, $uid, $bitfield, $flags);
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

224
			$demo_display = /** @scrutinizer ignore-call */ generate_text_for_display($demo_text, $uid, $bitfield, $flags);
Loading history...
225
			$list_sites = implode(', ', $this->get_siteIds());
226
227
			$this->template->assign_block_vars('faq_block.faq_row', [
228
				'FAQ_QUESTION'	=> $this->language->lang('HELP_EMBEDDING_MEDIA_QUESTION'),
229
				'FAQ_ANSWER'	=> $this->language->lang('HELP_EMBEDDING_MEDIA_ANSWER', self::MEDIA_DEMO_URL, $demo_display, $list_sites),
230
			]);
231
		}
232
	}
233
234
	/**
235
	 * Disable Media Embed plugin and tag if necessary
236
	 *
237
	 * @param \phpbb\event\data $event The event object
238
	 * @return void
239
	 */
240
	public function disable_media_embed($event)
241
	{
242
		/** @var \phpbb\textformatter\s9e\parser $service  */
243
		$service = $event['parser'];
244
		$parser = $service->get_parser();
245
246
		if ($this->disable_plugin)
247
		{
248
			$parser->disablePlugin('MediaEmbed');
249
		}
250
251
		if ($this->disable_tag)
252
		{
253
			$parser->disableTag('MEDIA');
254
		}
255
	}
256
257
	/**
258
	 * Set up a cache directory to improve scraping performance
259
	 *
260
	 * @param \phpbb\event\data $event The event object
261
	 * @return void
262
	 */
263
	public function setup_cache_dir($event)
264
	{
265
		if ($this->cache_dir && $this->config->offsetGet('media_embed_enable_cache'))
266
		{
267
			/** @var \phpbb\textformatter\s9e\parser $service  */
268
			$service = $event['parser'];
269
			$parser = $service->get_parser();
270
271
			$parser->registeredVars['cacheDir'] = $this->cache_dir;
272
		}
273
	}
274
275
	/**
276
	 * Check if forum permission allows Media Embed
277
	 *
278
	 * @param \phpbb\event\data $event The event object
279
	 * @return void
280
	 */
281
	public function check_forum_permission($event)
282
	{
283
		if (!$this->auth->acl_get('f_mediaembed', $event['forum_id']) || !$this->auth->acl_get('f_bbcode', $event['forum_id']))
284
		{
285
			$this->disable_plugin = true;
286
			$this->disable_tag = true;
287
		}
288
	}
289
290
	/**
291
	 * Check if user permission allows Media Embed in private messages
292
	 *
293
	 * @return void
294
	 */
295
	public function check_pm_permission()
296
	{
297
		if (!$this->auth->acl_get('u_pm_mediaembed'))
298
		{
299
			$this->disable_plugin = true;
300
			$this->disable_tag = true;
301
		}
302
	}
303
304
	/**
305
	 * Check if signature posting is allowed.
306
	 * Posting signatures is 'sig', reparsing signatures is 'user_signature'.
307
	 *
308
	 * @param \phpbb\event\data $event The event object
309
	 * @return void
310
	 */
311
	public function check_signature($event)
312
	{
313
		if (($event['mode'] === 'sig' || $event['mode'] === 'text_reparser.user_signature') && !$this->config->offsetGet('media_embed_allow_sig'))
314
		{
315
			$this->disable_plugin = true;
316
			$this->disable_tag = true;
317
		}
318
	}
319
320
	/**
321
	 * Check if magic urls is allowed.
322
	 *
323
	 * @param \phpbb\event\data $event The event object
324
	 * @return void
325
	 */
326
	public function check_magic_urls($event)
327
	{
328
		if (!$event['allow_magic_url'] || !$this->config->offsetGet('media_embed_parse_urls'))
329
		{
330
			$this->disable_plugin = true;
331
		}
332
	}
333
334
	/**
335
	 * Check if bbcodes are allowed.
336
	 *
337
	 * @param \phpbb\event\data $event The event object
338
	 * @return void
339
	 */
340
	public function check_bbcode_enabled($event)
341
	{
342
		if (!$event['allow_bbcode'])
343
		{
344
			// Want to leave plugin enabled, but it seems plugin won't work
345
			// when tag is disabled, so we have to disable both it seems.
346
			$this->disable_plugin = true;
347
			$this->disable_tag = true;
348
		}
349
	}
350
351
	public function setup_media_configs()
352
	{
353
		$this->template->assign_vars([
354
			'S_MEDIA_EMBED_FULL_WIDTH' => $this->config->offsetGet('media_embed_full_width'),
355
			'S_MEDIA_EMBED_MAX_WIDTHS' => json_decode($this->config_text->get('media_embed_max_width'), true),
356
		]);
357
	}
358
359
	/**
360
	 * Get allowed sites for media embedding
361
	 *
362
	 * @return array An array of sites
363
	 */
364
	protected function get_siteIds()
365
	{
366
		$siteIds = $this->config_text->get('media_embed_sites');
367
368
		return $siteIds ? json_decode($siteIds, true) : [];
369
	}
370
371
	/**
372
	 * Appends additional language to the privacy policy agreement text.
373
	 *
374
	 * @return void
375
	 */
376
	public function append_agreement()
377
	{
378
		if (!$this->template->retrieve_var('S_AGREEMENT') || ($this->template->retrieve_var('AGREEMENT_TITLE') !== $this->language->lang('PRIVACY')))
379
		{
380
			return;
381
		}
382
383
		$this->language->add_lang('ucp', 'phpbb/mediaembed');
384
385
		$this->template->append_var('AGREEMENT_TEXT', $this->language->lang('MEDIA_EMBED_PRIVACY_POLICY', $this->config['sitename']));
386
	}
387
}
388