Passed
Push — master ( 2c064d...2ccf61 )
by Matt
01:59
created

abbc3_module::validate_google_fonts()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2020 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\acp;
12
13
class abbc3_module
14
{
15
	/** @var \phpbb\cache\driver\driver_interface */
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...
16
	protected $cache;
17
18
	/** @var \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...
19
	protected $config;
20
21
	/** @var \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...
22
	protected $config_text;
23
24
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface 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...
25
	protected $container;
26
27
	/** @var \phpbb\db\driver\driver_interface */
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...
28
	protected $db;
29
30
	/** @var \phpbb\extension\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...
31
	protected $ext_manager;
32
33
	/** @var \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...
34
	protected $language;
35
36
	/** @var \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...
37
	protected $request;
38
39
	/** @var \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...
40
	protected $template;
41
42
	/** @var string */
43
	public $page_title;
44
45
	/** @var string */
46
	public $tpl_name;
47
48
	/** @var string */
49
	public $u_action;
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @throws \Exception
55
	 */
56
	public function __construct()
57
	{
58
		global $phpbb_container;
59
60
		$this->container   = $phpbb_container;
61
		$this->cache       = $this->container->get('cache');
62
		$this->config      = $this->container->get('config');
63
		$this->config_text = $this->container->get('config_text');
64
		$this->db          = $this->container->get('dbal.conn');
65
		$this->ext_manager = $this->container->get('ext.manager');
66
		$this->language    = $this->container->get('language');
67
		$this->request     = $this->container->get('request');
68
		$this->template    = $this->container->get('template');
69
	}
70
71
	/**
72
	 * Main ACP module
73
	 */
74
	public function main()
75
	{
76
		$this->language->add_lang('abbc3', 'vse/abbc3');
77
78
		$this->tpl_name   = 'acp_abbc3_settings';
79
		$this->page_title = $this->language->lang('ACP_ABBC3_SETTINGS');
80
81
		$form_key = 'vse/abbc3';
82
		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

82
		/** @scrutinizer ignore-call */ 
83
  add_form_key($form_key);
Loading history...
83
84
		if ($this->request->is_set_post('submit'))
85
		{
86
			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

86
			if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
87
			{
88
				trigger_error('FORM_INVALID', E_USER_WARNING);
89
			}
90
91
			$this->save_settings();
92
		}
93
94
		$this->display_settings();
95
	}
96
97
	/**
98
	 * Add settings template vars to the form
99
	 */
100
	protected function display_settings()
101
	{
102
		$this->template->assign_vars([
103
			'S_ABBC3_PIPES'			=> $this->config['abbc3_pipes'],
104
			'S_ABBC3_BBCODE_BAR'	=> $this->config['abbc3_bbcode_bar'],
105
			'S_ABBC3_QR_BBCODES'	=> $this->config['abbc3_qr_bbcodes'],
106
			'S_ABBC3_AUTO_VIDEO'	=> $this->config['abbc3_auto_video'],
107
			'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

107
			'S_ABBC3_ICONS_TYPE'	=> /** @scrutinizer ignore-call */ build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
Loading history...
108
			'S_ABBC3_GOOGLE_FONTS'	=> $this->show_google_fonts(),
109
			'S_ABBC3_MEDIA_EMBED'	=> (int) $this->ext_manager->is_enabled('phpbb/mediaembed'),
110
			'U_ACTION'				=> $this->u_action,
111
		]);
112
	}
113
114
	/**
115
	 * Save settings data to the database
116
	 */
117
	protected function save_settings()
118
	{
119
		$this->config->set('abbc3_bbcode_bar', $this->request->variable('abbc3_bbcode_bar', 0));
120
		$this->config->set('abbc3_qr_bbcodes', $this->request->variable('abbc3_qr_bbcodes', 0));
121
		$this->config->set('abbc3_auto_video', $this->request->variable('abbc3_auto_video', 0));
122
		$this->config->set('abbc3_icons_type', $this->request->variable('abbc3_icons_type', 'png'));
123
		$this->save_pipes();
124
		$this->save_google_fonts();
125
126
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
127
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
128
129
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
0 ignored issues
show
Bug introduced by
The function adm_back_link 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

129
		trigger_error($this->language->lang('CONFIG_UPDATED') . /** @scrutinizer ignore-call */ adm_back_link($this->u_action));
Loading history...
130
	}
131
132
	/**
133
	 * Save the Pipes Table setting.
134
	 * - Set the config
135
	 * - Show or hide the Pipes BBCode button
136
	 * - Purge BBCode caches.
137
	 */
138
	protected function save_pipes()
139
	{
140
		$enable_pipes = $this->request->variable('abbc3_pipes', 0);
141
142
		$this->config->set('abbc3_pipes', $enable_pipes);
143
144
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\acp\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
145
			SET display_on_posting = ' . (int) $enable_pipes . "
146
			WHERE bbcode_tag = 'pipes'";
147
		$this->db->sql_query($sql);
148
	}
149
150
	/**
151
	 * Get the Google font setting data and format it for the form.
152
	 *
153
	 * @return string
154
	 */
155
	protected function show_google_fonts()
156
	{
157
		$fonts = json_decode($this->config_text->get('abbc3_google_fonts'), true);
158
		return $fonts ? implode("\n", $fonts) : '';
159
	}
160
161
	/**
162
	 * Save the Google fonts setting.
163
	 * - If field has data, explode it to an array and save as JSON data.
164
	 * - If field is empty, store just an empty string.
165
	 */
166
	protected function save_google_fonts()
167
	{
168
		$fonts = $this->request->variable('abbc3_google_fonts', '');
169
		$fonts = $fonts ? json_encode(explode("\n", $fonts)) : '';
170
		$this->config_text->set('abbc3_google_fonts', $fonts);
171
	}
172
}
173