Passed
Push — master ( c83538...085220 )
by Matt
01:39
created

acp_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0

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
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2020, 2023 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\controller;
12
13
use phpbb\cache\driver\driver_interface as cache;
0 ignored issues
show
Bug introduced by
The type phpbb\cache\driver\driver_interface 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\db\driver\driver_interface as db;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface 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\exception\runtime_exception;
0 ignored issues
show
Bug introduced by
The type phpbb\exception\runtime_exception 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...
18
use phpbb\extension\manager as ext_manager;
0 ignored issues
show
Bug introduced by
The type phpbb\extension\manager 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\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...
20
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...
21
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...
22
23
class acp_controller
24
{
25
	/** @var cache */
26
	protected $cache;
27
28
	/** @var config */
29
	protected $config;
30
31
	/** @var db_text */
32
	protected $config_text;
33
34
	/** @var db */
35
	protected $db;
36
37
	/** @var ext_manager */
38
	protected $ext_manager;
39
40
	/** @var language */
41
	protected $language;
42
43
	/** @var request */
44
	protected $request;
45
46
	/** @var template */
47
	protected $template;
48
49
	/** @var string */
50
	protected $parser_key;
51
52
	/** @var string */
53
	protected $renderer_key;
54
55
	/** @var string */
56
	public $u_action;
57
58
	/** @var array */
59
	protected $errors = [];
60
61
	/**
62
	 * Constructor
63
	 *
64
	 * @param cache $cache
65
	 * @param config $config
66
	 * @param db_text $db_text
67
	 * @param db $db
68
	 * @param ext_manager $ext_manager
69
	 * @param language $language
70
	 * @param request $request
71
	 * @param template $template
72
	 * @param $parser_key
73
	 * @param $renderer_key
74
	 */
75
	public function __construct(cache $cache, config $config, db_text $db_text, db $db, ext_manager $ext_manager, language $language, request $request, template $template, $parser_key, $renderer_key)
76
	{
77
		$this->cache = $cache;
78
		$this->config = $config;
79
		$this->config_text = $db_text;
80
		$this->db = $db;
81
		$this->ext_manager = $ext_manager;
82
		$this->language = $language;
83
		$this->request = $request;
84
		$this->template = $template;
85
		$this->parser_key = $parser_key;
86
		$this->renderer_key = $renderer_key;
87
	}
88
89
	/**
90
	 * Main handler for this controller
91
	 *
92
	 * @throws runtime_exception
93
	 */
94
	public function handle()
95
	{
96
		$this->language->add_lang('acp_abbc3', 'vse/abbc3');
97
98
		$form_key = 'vse/abbc3';
99
		add_form_key($form_key);
0 ignored issues
show
Bug introduced by
The function add_form_key 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

99
		/** @scrutinizer ignore-call */ 
100
  add_form_key($form_key);
Loading history...
100
101
		if ($this->request->is_set_post('submit'))
102
		{
103
			if (!check_form_key($form_key))
0 ignored issues
show
Bug introduced by
The function check_form_key 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

103
			if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
104
			{
105
				throw new runtime_exception($this->language->lang('FORM_INVALID'), [], null, E_USER_WARNING);
106
			}
107
108
			$this->save_settings();
109
		}
110
111
		$this->display_settings();
112
	}
113
114
	/**
115
	 * Add settings template vars to the form
116
	 */
117
	protected function display_settings()
118
	{
119
		$this->template->assign_vars([
120
			'S_ABBC3_PIPES'			=> $this->config['abbc3_pipes'],
121
			'S_ABBC3_BBCODE_BAR'	=> $this->config['abbc3_bbcode_bar'],
122
			'S_ABBC3_QR_BBCODES'	=> $this->config['abbc3_qr_bbcodes'],
123
			'S_ABBC3_AUTO_VIDEO'	=> $this->config['abbc3_auto_video'],
124
			'S_ABBC3_ICONS_TYPE'	=> build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
0 ignored issues
show
Bug introduced by
The function build_select 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

124
			'S_ABBC3_ICONS_TYPE'	=> /** @scrutinizer ignore-call */ build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
Loading history...
125
			'S_ABBC3_GOOGLE_FONTS'	=> $this->get_google_fonts(),
126
			'S_ABBC3_MEDIA_EMBED'	=> $this->ext_manager->is_enabled('phpbb/mediaembed'),
127
			'U_ACTION'				=> $this->u_action,
128
		]);
129
	}
130
131
	/**
132
	 * Save settings data to the database
133
	 *
134
	 * @throws runtime_exception
135
	 */
136
	protected function save_settings()
137
	{
138
		$this->config->set('abbc3_bbcode_bar', $this->request->variable('abbc3_bbcode_bar', 0));
139
		$this->config->set('abbc3_qr_bbcodes', $this->request->variable('abbc3_qr_bbcodes', 0));
140
		$this->config->set('abbc3_auto_video', $this->request->variable('abbc3_auto_video', 0));
141
		$this->config->set('abbc3_icons_type', $this->request->variable('abbc3_icons_type', 'png'));
142
		$this->save_pipes();
143
		$this->save_google_fonts();
144
145
		$this->cache->destroy($this->parser_key);
146
		$this->cache->destroy($this->renderer_key);
147
148
		if (!empty($this->errors))
149
		{
150
			throw new runtime_exception(implode('<br>', $this->errors), [], null, E_USER_WARNING);
151
		}
152
153
		throw new runtime_exception($this->language->lang('CONFIG_UPDATED'), [], null, E_USER_NOTICE);
154
	}
155
156
	/**
157
	 * Save the Pipes Table setting.
158
	 * - Set the config
159
	 * - Show or hide the Pipes BBCode button
160
	 * - Purge BBCode caches.
161
	 */
162
	protected function save_pipes()
163
	{
164
		$enable_pipes = $this->request->variable('abbc3_pipes', 0);
165
166
		$this->config->set('abbc3_pipes', $enable_pipes);
167
168
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\controller\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
169
			SET display_on_posting = ' . (int) $enable_pipes . "
170
			WHERE bbcode_tag = 'pipes'";
171
		$this->db->sql_query($sql);
172
	}
173
174
	/**
175
	 * Get the Google font setting data and format it for the form.
176
	 *
177
	 * @return string
178
	 */
179
	protected function get_google_fonts()
180
	{
181
		$fonts = json_decode($this->config_text->get('abbc3_google_fonts'), true);
182
		return $fonts ? implode("\n", $fonts) : '';
183
	}
184
185
	/**
186
	 * Save the Google fonts setting.
187
	 * - If field has data, explode it to an array and save as JSON data.
188
	 * - If field is empty, store just an empty string.
189
	 */
190
	protected function save_google_fonts()
191
	{
192
		$fonts = $this->request->variable('abbc3_google_fonts', '');
193
		$fonts = explode("\n", $fonts);
194
		$this->validate_google_fonts($fonts);
195
		$this->config_text->set('abbc3_google_fonts', json_encode($fonts));
196
	}
197
198
	/**
199
	 * Validate Google Font names provided link to a CSS file
200
	 *
201
	 * @param array $fonts
202
	 */
203
	protected function validate_google_fonts(&$fonts)
204
	{
205
		foreach ($fonts as $key => $font)
206
		{
207
			if (empty($font) || $this->valid_url('https://fonts.googleapis.com/css?family=' . urlencode($font)))
208
			{
209
				continue;
210
			}
211
212
			$this->errors[] = $this->language->lang('ABBC3_INVALID_FONT', $font);
213
			unset($fonts[$key]);
214
		}
215
	}
216
217
	/**
218
	 * Check for valid URL headers if possible
219
	 *
220
	 * @param string $url
221
	 * @return bool Return false only if URL could be checked and wasn't found, otherwise true.
222
	 */
223
	protected function valid_url($url)
224
	{
225
		if (!function_exists('get_headers'))
226
		{
227
			return true;
228
		}
229
230
		$headers = @get_headers($url);
231
		return !$headers || stripos($headers[0], '200 OK') !== false;
232
	}
233
234
	/**
235
	 * Get the translated page title
236
	 *
237
	 * @return string
238
	 */
239
	public function get_page_title()
240
	{
241
		return $this->language->lang('ACP_ABBC3_SETTINGS');
242
	}
243
244
	/**
245
	 * Set the u_action variable
246
	 *
247
	 * @param string $u_action
248
	 */
249
	public function set_u_action($u_action)
250
	{
251
		$this->u_action = $u_action;
252
	}
253
}
254