acp_controller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 13
rs 9.9332

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
 * 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\controller;
12
13
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...
14
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...
15
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...
16
use phpbb\log\log;
0 ignored issues
show
Bug introduced by
The type phpbb\log\log 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\cache\cache as media_cache;
18
use phpbb\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request 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...
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 phpbb\textformatter\s9e\factory as textformatter;
0 ignored issues
show
Bug introduced by
The type phpbb\textformatter\s9e\factory 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 phpbb\user;
0 ignored issues
show
Bug introduced by
The type phpbb\user 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
 * phpBB Media Embed ACP module controller.
25
 */
26
class acp_controller implements acp_controller_interface
27
{
28
	/** @var config $config */
29
	protected $config;
30
31
	/** @var db_text $config_text */
32
	protected $config_text;
33
34
	/** @var language $language */
35
	protected $language;
36
37
	/** @var log $log */
38
	protected $log;
39
40
	/** @var media_cache $media_cache */
41
	protected $media_cache;
42
43
	/** @var request $request */
44
	protected $request;
45
46
	/** @var template $template */
47
	protected $template;
48
49
	/** @var textformatter $textformatter */
50
	protected $textformatter;
51
52
	/** @var user $user */
53
	protected $user;
54
55
	/** @var array $enabled_sites */
56
	protected $enabled_sites;
57
58
	/** @var string $u_action */
59
	public $u_action;
60
61
	/** @var array An array of errors */
62
	protected $errors = [];
63
64
	/**
65
	 * Constructor
66
	 */
67
	public function __construct(config $config, db_text $config_text, language $language, log $log, media_cache $media_cache, request $request, template $template, textformatter $textformatter, user $user)
68
	{
69
		$this->config = $config;
70
		$this->config_text = $config_text;
71
		$this->language = $language;
72
		$this->log = $log;
73
		$this->media_cache = $media_cache;
74
		$this->request = $request;
75
		$this->template = $template;
76
		$this->textformatter = $textformatter;
77
		$this->user = $user;
78
79
		$this->language->add_lang('acp', 'phpbb/mediaembed');
80
	}
81
82
	/**
83
	 * Set page url
84
	 *
85
	 * @param string $u_action Custom form action
86
	 */
87
	public function set_page_url($u_action)
88
	{
89
		$this->u_action = $u_action;
90
	}
91
92
	/**
93
	 * Add settings template vars to the form
94
	 */
95
	public function display_settings()
96
	{
97
		$this->template->assign_vars([
98
			'S_MEDIA_EMBED_BBCODE'       => $this->config['media_embed_bbcode'],
99
			'S_MEDIA_EMBED_ALLOW_SIG'    => $this->config['media_embed_allow_sig'],
100
			'S_MEDIA_EMBED_PARSE_URLS'   => $this->config['media_embed_parse_urls'],
101
			'S_MEDIA_EMBED_ENABLE_CACHE' => $this->config['media_embed_enable_cache'],
102
			'S_MEDIA_EMBED_FULL_WIDTH'   => $this->config['media_embed_full_width'],
103
			'S_MEDIA_EMBED_MAX_WIDTHS'   => $this->get_media_embed_max_width(),
104
			'U_ACTION'                   => $this->u_action,
105
		]);
106
	}
107
108
	/**
109
	 * Add manage sites template vars to the form
110
	 */
111
	public function display_manage()
112
	{
113
		$this->template->assign_vars([
114
			'MEDIA_SITES' => $this->get_sites(),
115
			'U_ACTION'    => $this->u_action,
116
			'ERRORS'      => $this->errors,
117
		]);
118
	}
119
120
	/**
121
	 * Save settings data to the database
122
	 *
123
	 * @return array Message and code for trigger error
124
	 */
125
	public function save_settings()
126
	{
127
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
128
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
129
		$this->config->set('media_embed_parse_urls', $this->request->variable('media_embed_parse_urls', 0));
130
		$this->config->set('media_embed_enable_cache', $this->request->variable('media_embed_enable_cache', 0));
131
		$this->config->set('media_embed_full_width', $this->request->variable('media_embed_full_width', 0));
132
133
		$this->set_media_embed_max_width();
134
135
		$this->media_cache->purge_textformatter_cache();
136
137
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
138
139
		if (count($this->errors))
140
		{
141
			return [
142
				'code' => E_USER_WARNING,
143
				'message' => $this->language->lang('ACP_MEDIA_ERROR_MSG', implode('<br>', $this->errors))
144
			];
145
		}
146
147
		return [
148
			'code' => E_USER_NOTICE,
149
			'message' => $this->language->lang('CONFIG_UPDATED')
150
		];
151
	}
152
153
	/**
154
	 * Save site managed data to the database
155
	 *
156
	 * @return array Message and code for trigger error
157
	 */
158
	public function save_manage()
159
	{
160
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
161
162
		$this->media_cache->purge_textformatter_cache();
163
164
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
165
166
		return [
167
			'code' => E_USER_NOTICE,
168
			'message' => $this->language->lang('CONFIG_UPDATED')
169
		];
170
	}
171
172
	/**
173
	 * Purge all Media Embed cache files
174
	 */
175
	public function purge_mediaembed_cache()
176
	{
177
		$this->media_cache->purge_mediaembed_cache();
178
179
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_CACHE_PURGED');
180
181
		return [
182
			'code' => E_USER_NOTICE,
183
			'message' => $this->language->lang('PURGE_CACHE_SUCCESS')
184
		];
185
186
	}
187
188
	/**
189
	 * Get a list of available sites
190
	 *
191
	 * @return array An array of available sites
192
	 */
193
	protected function get_sites()
194
	{
195
		$sites = [];
196
197
		$configurator = $this->textformatter->get_configurator();
198
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
199
		{
200
			$disabled = isset($configurator->BBCodes[$siteId]);
201
			$sites[$siteId] = [
202
				'id'       => $siteId,
203
				'name'     => $siteConfig['name'],
204
				'title'    => $this->language->lang($disabled ? 'ACP_MEDIA_SITE_DISABLED' : 'ACP_MEDIA_SITE_TITLE', $siteId),
205
				'enabled'  => in_array($siteId, $this->get_enabled_sites()),
206
				'disabled' => $disabled,
207
			];
208
		}
209
210
		ksort($sites);
211
212
		$this->errors = array_diff($this->get_enabled_sites(), array_keys($sites));
213
214
		return $sites;
215
	}
216
217
	/**
218
	 * Get enabled media sites stored in the database
219
	 *
220
	 * @return array An array of enabled sites
221
	 */
222
	protected function get_enabled_sites()
223
	{
224
		if ($this->enabled_sites === null)
225
		{
226
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
227
			$this->enabled_sites = is_array($sites) ? $sites : [];
228
		}
229
230
		return $this->enabled_sites;
231
	}
232
233
	/**
234
	 * Store the media embed max width value to the config text as JSON,
235
	 * with some basic input validation and array formatting.
236
	 */
237
	protected function set_media_embed_max_width()
238
	{
239
		$input = $this->request->variable('media_embed_max_width', '');
240
241
		if ($input)
242
		{
243
			$lines = array_unique(explode("\n", $input));
244
245
			foreach ($lines as $key => $line)
246
			{
247
				$parts = explode(':', $line);
248
				if (count($parts) !== 2)
249
				{
250
					unset($lines[$key]);
251
					continue;
252
				}
253
254
				$lines[$key] = array_combine(['site', 'width'], array_map('trim', $parts));
255
			}
256
257
			$input = json_encode(array_filter($lines, [$this, 'validate']));
258
		}
259
260
		$this->config_text->set('media_embed_max_width', strtolower($input));
261
	}
262
263
	/**
264
	 * Get the stored media embed max width data from config text and convert
265
	 * from JSON to the formatting used in the ACP textarea field.
266
	 *
267
	 * @return string
268
	 */
269
	protected function get_media_embed_max_width()
270
	{
271
		$config = json_decode($this->config_text->get('media_embed_max_width'), true);
272
273
		if ($config)
274
		{
275
			foreach ($config as &$item)
276
			{
277
				$item = implode(':', $item);
278
			}
279
280
			unset($item);
281
		}
282
283
		return $config ? implode("\n", $config) : '';
284
	}
285
286
	/**
287
	 * Validate the input for media embed max widths
288
	 * 'site' key value should be a word
289
	 * 'width' key value should be a number appended with either px or %
290
	 *
291
	 * @param array $input The array to check
292
	 * @return bool True if array contains valid values, false if not
293
	 * @throws \Exception
294
	 */
295
	protected function validate($input)
296
	{
297
		// First, lets get all the available media embed site IDs
298
		static $default_sites;
299
300
		if (null === $default_sites)
301
		{
302
			$configurator = $this->textformatter->get_configurator();
303
			$default_sites = array_keys(iterator_to_array($configurator->MediaEmbed->defaultSites));
304
		}
305
306
		// Next create an array to hold any errors
307
		$errors = [];
308
309
		// Check to see if the site id provided exists in Media Embed
310
		if (!in_array($input['site'], $default_sites))
311
		{
312
			$errors[] = $this->language->lang('ACP_MEDIA_INVALID_SITE', $input['site'], $input['width']);
313
		}
314
315
		// Check to see if the width provided is a valid number followed px or %
316
		if (!preg_match('/^\d+(?:%|px)$/', $input['width']))
317
		{
318
			$errors[] = $this->language->lang('ACP_MEDIA_INVALID_WIDTH', $input['site'], $input['width']);
319
		}
320
321
		// Update the errors object with any new errors
322
		$this->errors = array_merge($this->errors, $errors);
323
324
		return empty($errors);
325
	}
326
}
327