| Total Complexity | 42 |
| Total Lines | 360 |
| Duplicated Lines | 0 % |
| Changes | 27 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 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) |
||
| 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 |
||
| 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) |
||
| 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) |
||
| 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); |
||
| 224 | $demo_display = generate_text_for_display($demo_text, $uid, $bitfield, $flags); |
||
| 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() |
||
| 386 | } |
||
| 387 | } |
||
| 388 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths