Completed
Pull Request — master (#73)
by rxu
01:19
created

main_module   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 247
Duplicated Lines 5.26 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 0
dl 13
loc 247
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A main() 0 27 4
A display_settings() 0 10 1
A display_manage() 0 8 1
A get_sites() 0 23 3
A get_enabled_sites() 0 10 3
A save_manage() 0 10 1
A save_settings() 0 13 1
A purge_textformatter_cache() 0 5 1
B purge_mediaembed_cache() 13 28 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\acp;
12
13
/**
14
 * phpBB Media Embed Plugin ACP module.
15
 */
16
class main_module
17
{
18
	/** @var \phpbb\cache\driver\driver_interface $cache */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config $config */
22
	protected $config;
23
24
	/** @var \phpbb\config\db_text $config_text */
25
	protected $config_text;
26
27
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
28
	protected $container;
29
30
	/** @var \phpbb\language\language $language */
31
	protected $language;
32
33
	/** @var \phpbb\log\log $log */
34
	protected $log;
35
36
	/** @var \phpbb\request\request $request */
37
	protected $request;
38
39
	/** @var \phpbb\template\template $template */
40
	protected $template;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var array $enabled_sites */
46
	protected $enabled_sites;
47
48
	/** @var string $page_title */
49
	public $page_title;
50
51
	/** @var string $tpl_name */
52
	public $tpl_name;
53
54
	/** @var string $u_action */
55
	public $u_action;
56
57
	/** @var array An array of errors */
58
	protected $errors = [];
59
60
	/**
61
	 * Constructor
62
	 *
63
	 * @throws \Exception
64
	 */
65
	public function __construct()
66
	{
67
		global $phpbb_container;
68
69
		$this->container   = $phpbb_container;
70
		$this->cache       = $this->container->get('cache.driver');
71
		$this->config      = $this->container->get('config');
72
		$this->config_text = $this->container->get('config_text');
73
		$this->language    = $this->container->get('language');
74
		$this->log         = $this->container->get('log');
75
		$this->request     = $this->container->get('request');
76
		$this->template    = $this->container->get('template');
77
		$this->user        = $this->container->get('user');
78
79
		$this->language->add_lang('acp', 'phpbb/mediaembed');
80
	}
81
82
	/**
83
	 * Main ACP module
84
	 *
85
	 * @param int    $id   The module ID (not used)
86
	 * @param string $mode The module mode (manage|settings)
87
	 * @throws \Exception
88
	 */
89
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
	{
91
		$mode = strtolower($mode);
92
93
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . $mode;
94
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
95
96
		$form_key = 'phpbb/mediaembed';
97
		add_form_key($form_key);
98
99
		if ($this->request->is_set_post('action_purge_cache'))
100
		{
101
			$this->purge_mediaembed_cache();
102
		}
103
104
		if ($this->request->is_set_post('submit'))
105
		{
106
			if (!check_form_key($form_key))
107
			{
108
				trigger_error('FORM_INVALID', E_USER_WARNING);
109
			}
110
111
			$this->{'save_' . $mode}();
112
		}
113
114
		$this->{'display_' . $mode}();
115
	}
116
117
	/**
118
	 * Add settings template vars to the form
119
	 */
120
	protected function display_settings()
121
	{
122
		$this->template->assign_vars([
123
			'S_MEDIA_EMBED_BBCODE'		=> $this->config['media_embed_bbcode'],
124
			'S_MEDIA_EMBED_ALLOW_SIG'	=> $this->config['media_embed_allow_sig'],
125
			'S_MEDIA_EMBED_PARSE_URLS'	=> $this->config['media_embed_parse_urls'],
126
			'S_MEDIA_EMBED_ENABLE_CACHE'=> $this->config['media_embed_enable_cache'],
127
			'U_ACTION'					=> $this->u_action,
128
		]);
129
	}
130
131
	/**
132
	 * Add manage sites template vars to the form
133
	 *
134
	 * @throws \Exception
135
	 */
136
	protected function display_manage()
137
	{
138
		$this->template->assign_vars([
139
			'MEDIA_SITES'	=> $this->get_sites(),
140
			'U_ACTION'		=> $this->u_action,
141
			'ERRORS'		=> $this->errors,
142
		]);
143
	}
144
145
	/**
146
	 * Get a list of available sites
147
	 *
148
	 * @return array An array of available sites
149
	 * @throws \Exception
150
	 */
151
	protected function get_sites()
152
	{
153
		$sites = [];
154
155
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
156
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
157
		{
158
			$disabled = isset($configurator->BBCodes[$siteId]);
159
			$sites[$siteId] = [
160
				'id'		=> $siteId,
161
				'name'		=> $siteConfig['name'],
162
				'title'		=> $this->language->lang($disabled ? 'ACP_MEDIA_SITE_DISABLED' : 'ACP_MEDIA_SITE_TITLE', $siteId),
163
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
164
				'disabled'	=> $disabled,
165
			];
166
		}
167
168
		ksort($sites);
169
170
		$this->errors = array_diff($this->get_enabled_sites(), array_keys($sites));
171
172
		return $sites;
173
	}
174
175
	/**
176
	 * Get enabled media sites stored in the database
177
	 *
178
	 * @return array An array of enabled sites
179
	 */
180
	protected function get_enabled_sites()
181
	{
182
		if ($this->enabled_sites === null)
183
		{
184
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
185
			$this->enabled_sites = is_array($sites) ? $sites : [];
186
		}
187
188
		return $this->enabled_sites;
189
	}
190
191
	/**
192
	 * Save site managed data to the database
193
	 */
194
	protected function save_manage()
195
	{
196
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
197
198
		$this->purge_textformatter_cache();
199
200
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
201
202
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
203
	}
204
205
	/**
206
	 * Save settings data to the database
207
	 */
208
	protected function save_settings()
209
	{
210
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
211
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
212
		$this->config->set('media_embed_parse_urls', $this->request->variable('media_embed_parse_urls', 0));
213
		$this->config->set('media_embed_enable_cache', $this->request->variable('media_embed_enable_cache', 0));
214
215
		$this->purge_textformatter_cache();
216
217
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
218
219
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
220
	}
221
222
	/**
223
	 * Purge cached TextFormatter files
224
	 */
225
	protected function purge_textformatter_cache()
226
	{
227
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
228
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
229
	}
230
231
	/**
232
	 * Purge all MediaEmbed cache files
233
	 */
234
	protected function purge_mediaembed_cache()
235
	{
236
		try
237
		{
238
			$iterator = new \DirectoryIterator($this->cache->cache_dir);
239
		}
240
		catch (\Exception $e)
241
		{
242
			return;
243
		}
244 View Code Duplication
		foreach ($iterator as $fileInfo)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
		{
246
			if ($fileInfo->isDot() || $fileInfo->isDir())
247
			{
248
				continue;
249
			}
250
251
			$filename = $fileInfo->getFilename();
252
			if (strpos($filename, 'http.') === 0)
253
			{
254
				$this->cache->remove_file($fileInfo->getPathname());
255
			}
256
		}
257
258
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_CACHE_PURGED');
259
260
		trigger_error($this->language->lang('PURGE_CACHE_SUCCESS') . adm_back_link($this->u_action));
261
	}
262
}
263