Passed
Branch master (e751cb)
by Matt
02:48
created

abbc3_module   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 56
dl 0
loc 151
rs 10
c 3
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A save_settings() 0 9 1
A display_settings() 0 9 1
A save_google_fonts() 0 5 2
A __construct() 0 12 1
A save_pipes() 0 13 1
A main() 0 21 3
A show_google_fonts() 0 4 2
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\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...
31
	protected $language;
32
33
	/** @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...
34
	protected $request;
35
36
	/** @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...
37
	protected $template;
38
39
	/** @var string */
40
	public $page_title;
41
42
	/** @var string */
43
	public $tpl_name;
44
45
	/** @var string */
46
	public $u_action;
47
48
	/**
49
	 * Constructor
50
	 *
51
	 * @throws \Exception
52
	 */
53
	public function __construct()
54
	{
55
		global $phpbb_container;
56
57
		$this->container   = $phpbb_container;
58
		$this->cache       = $this->container->get('cache');
59
		$this->config      = $this->container->get('config');
60
		$this->config_text = $this->container->get('config_text');
61
		$this->db          = $this->container->get('dbal.conn');
62
		$this->language    = $this->container->get('language');
63
		$this->request     = $this->container->get('request');
64
		$this->template    = $this->container->get('template');
65
	}
66
67
	/**
68
	 * Main ACP module
69
	 */
70
	public function main()
71
	{
72
		$this->language->add_lang('abbc3', 'vse/abbc3');
73
74
		$this->tpl_name   = 'acp_abbc3_settings';
75
		$this->page_title = $this->language->lang('ACP_ABBC3_SETTINGS');
76
77
		$form_key = 'vse/abbc3';
78
		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

78
		/** @scrutinizer ignore-call */ 
79
  add_form_key($form_key);
Loading history...
79
80
		if ($this->request->is_set_post('submit'))
81
		{
82
			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

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

102
			'S_ABBC3_ICONS_TYPE'	=> /** @scrutinizer ignore-call */ build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
Loading history...
103
			'S_ABBC3_GOOGLE_FONTS'	=> $this->show_google_fonts(),
104
			'U_ACTION'				=> $this->u_action,
105
		]);
106
	}
107
108
	/**
109
	 * Save settings data to the database
110
	 */
111
	protected function save_settings()
112
	{
113
		$this->config->set('abbc3_bbcode_bar', $this->request->variable('abbc3_bbcode_bar', 0));
114
		$this->config->set('abbc3_qr_bbcodes', $this->request->variable('abbc3_qr_bbcodes', 0));
115
		$this->config->set('abbc3_icons_type', $this->request->variable('abbc3_icons_type', 'png'));
116
		$this->save_pipes();
117
		$this->save_google_fonts();
118
119
		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

119
		trigger_error($this->language->lang('CONFIG_UPDATED') . /** @scrutinizer ignore-call */ adm_back_link($this->u_action));
Loading history...
120
	}
121
122
	/**
123
	 * Save the Pipes Table setting.
124
	 * - Set the config
125
	 * - Show or hide the Pipes BBCode button
126
	 * - Purge BBCode caches.
127
	 */
128
	protected function save_pipes()
129
	{
130
		$enable_pipes = $this->request->variable('abbc3_pipes', 0);
131
132
		$this->config->set('abbc3_pipes', $enable_pipes);
133
134
		$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...
135
			SET display_on_posting = ' . (int) $enable_pipes . "
136
			WHERE bbcode_tag = 'pipes'";
137
		$this->db->sql_query($sql);
138
139
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
140
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
141
	}
142
143
	/**
144
	 * Get the Google font setting data and format it for the form.
145
	 *
146
	 * @return string
147
	 */
148
	protected function show_google_fonts()
149
	{
150
		$fonts = json_decode($this->config_text->get('abbc3_google_fonts'), true);
151
		return $fonts ? implode("\n", $fonts) : '';
152
	}
153
154
	/**
155
	 * Save the Google fonts setting.
156
	 * - If field has data, explode it to an array and save as JSON data.
157
	 * - If field is empty, store just an empty string.
158
	 */
159
	protected function save_google_fonts()
160
	{
161
		$fonts = $this->request->variable('abbc3_google_fonts', '');
162
		$fonts = $fonts ? json_encode(explode("\n", $fonts)) : '';
163
		$this->config_text->set('abbc3_google_fonts', $fonts);
164
	}
165
}
166