Passed
Push — master ( 3456da...537772 )
by Matt
02:15
created

bbcodes_installer::remove_bbcode()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 15
rs 10
ccs 0
cts 0
cp 0
crap 20
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2016 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\core;
12
13
use 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...
14
use phpbb\group\helper;
0 ignored issues
show
Bug introduced by
The type phpbb\group\helper 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\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...
17
18
/**
19
 * Class bbcodes_installer
20
 *
21
 * @package vse\abbc3\core
22
 */
23
class bbcodes_installer extends acp_manager
24
{
25
	/** @var \acp_bbcodes */
0 ignored issues
show
Bug introduced by
The type acp_bbcodes 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...
26
	protected $acp_bbcodes;
27
28
	/** @var string */
29
	protected $phpbb_root_path;
30
31
	/** @var string */
32
	protected $php_ext;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param driver_interface $db
38
	 * @param helper           $group_helper
39
	 * @param language         $language
40
	 * @param request          $request
41
	 * @param string           $phpbb_root_path
42
	 * @param string           $php_ext
43
	 * @access public
44
	 */
45 2
	public function __construct(driver_interface $db, helper $group_helper, language $language, request $request, $phpbb_root_path, $php_ext)
46
	{
47 2
		parent::__construct($db, $group_helper, $language, $request);
48
49 2
		$this->phpbb_root_path = $phpbb_root_path;
50 2
		$this->php_ext         = $php_ext;
51
52 2
		$this->acp_bbcodes     = $this->get_acp_bbcodes();
53
54 2
		$this->language->add_lang('acp/posting');
55 2
	}
56
57
	/**
58
	 * Installs bbcodes, used by migrations to perform add/updates
59
	 *
60
	 * @param array $bbcodes Array of bbcodes to install
61
	 * @access public
62
	 */
63 2
	public function install_bbcodes(array $bbcodes)
64
	{
65 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
66
		{
67 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
68
69 2
			if ($bbcode_id = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
70 2
			{
71 1
				$this->update_bbcode($bbcode_id, $bbcode_data);
0 ignored issues
show
Bug introduced by
$bbcode_id of type string is incompatible with the type integer expected by parameter $bbcode_id of vse\abbc3\core\bbcodes_installer::update_bbcode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
				$this->update_bbcode(/** @scrutinizer ignore-type */ $bbcode_id, $bbcode_data);
Loading history...
72 1
				continue;
73
			}
74
75 2
			$this->add_bbcode($bbcode_data);
76 2
		}
77
78 2
		$this->resynchronize_bbcode_order();
79 2
	}
80
81
	/**
82
	 * Deletes bbcodes, used by migrations to perform add/updates.
83
	 *
84
	 * @param array $bbcodes Array of bbcodes to delete
85
	 * @access public
86
	 */
87 2
	public function delete_bbcodes(array $bbcodes)
88
	{
89 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
90
		{
91 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
92
93 2
			if ($bbcode_id = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
94 2
			{
95 2
				$this->remove_bbcode($bbcode_id, $bbcode_data);
0 ignored issues
show
Bug introduced by
$bbcode_id of type string is incompatible with the type integer expected by parameter $bbcode_id of vse\abbc3\core\bbcodes_installer::remove_bbcode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
				$this->remove_bbcode(/** @scrutinizer ignore-type */ $bbcode_id, $bbcode_data);
Loading history...
96 2
			}
97 2
		}
98 2
99 2
		$this->resynchronize_bbcode_order();
100 2
	}
101 2
102
	/**
103 2
	 * Get the acp_bbcodes class
104 2
	 *
105
	 * @return \acp_bbcodes
106
	 * @access protected
107
	 */
108
	protected function get_acp_bbcodes()
109
	{
110
		if (!class_exists('acp_bbcodes'))
111
		{
112 2
			include $this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext;
113
		}
114 2
115 2
		return new \acp_bbcodes();
116 1
	}
117 1
118
	/**
119 2
	 * Build the bbcode
120
	 *
121
	 * @param array $bbcode_data Initial bbcode data
122
	 * @return array Complete bbcode data array
123
	 * @access protected
124
	 */
125
	protected function build_bbcode(array $bbcode_data)
126
	{
127
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
128
129 2
		return array_replace($bbcode_data, [
130
			'bbcode_tag'          => $data['bbcode_tag'],
131 2
			'first_pass_match'    => $data['first_pass_match'],
132
			'first_pass_replace'  => $data['first_pass_replace'],
133 2
			'second_pass_match'   => $data['second_pass_match'],
134 2
			'second_pass_replace' => $data['second_pass_replace'],
135 2
		]);
136 2
	}
137 2
138 2
	/**
139 2
	 * Get the max bbcode id value
140
	 *
141 2
	 * @return int bbcode identifier
142
	 * @access protected
143
	 */
144
	protected function get_max_bbcode_id()
145
	{
146
		return $this->get_max_column_value('bbcode_id');
147
	}
148
149
	/**
150 2
	 * Check if bbcode exists
151
	 *
152 2
	 * @param string $bbcode_name Name of bbcode
153
	 * @param string $bbcode_tag  Tag name of bbcode
154
	 * @return string|false Existing bbcode identifier or false if not found
155
	 * @access protected
156
	 */
157
	public function bbcode_exists($bbcode_name, $bbcode_tag)
158
	{
159
		$sql = 'SELECT bbcode_id
160
			FROM ' . BBCODES_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
161
			WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "'
162
			OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'";
163 2
		$result = $this->db->sql_query($sql);
164
		$bbcode_id = $this->db->sql_fetchfield('bbcode_id');
165
		$this->db->sql_freeresult($result);
166 2
167 2
		return $bbcode_id;
168 2
	}
169 2
170 2
	/**
171 2
	 * Update existing bbcode
172
	 *
173 2
	 * @param int $bbcode_id bbcode identifier
174
	 * @param array $bbcode_data bbcode data
175
	 * @access protected
176
	 */
177
	protected function update_bbcode($bbcode_id, array $bbcode_data)
178
	{
179
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
180
			SET ' . $this->db->sql_build_array('UPDATE', $bbcode_data) . '
181
			WHERE bbcode_id = ' . (int) $bbcode_id;
182
		$this->db->sql_query($sql);
183 1
	}
184
185 1
	/**
186 1
	 * Add new bbcode
187 1
	 *
188 1
	 * @param array $bbcode_data New bbcode data
189 1
	 * @access protected
190
	 */
191
	protected function add_bbcode(array $bbcode_data)
192
	{
193
		$bbcode_id = max($this->get_max_bbcode_id(), NUM_CORE_BBCODES) + 1;
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\NUM_CORE_BBCODES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
194
195
		if ($bbcode_id <= BBCODE_LIMIT)
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODE_LIMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
196
		{
197 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
198
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
199 2
			$bbcode_data['display_on_posting'] = (int) !array_key_exists('display_on_posting', $bbcode_data);
200
201 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
202 2
		}
203 1
	}
204 1
205
	/**
206 2
	 * Remove a bbcode
207 2
	 * Don't remove just by ID. Also match replace fields. This is to ensure we only delete BBCodes created by ABBC3,
208 2
	 * and not if, for example ABBC3 BBCodes that have been altered by the user.
209
	 *
210 2
	 * Note special handling for MSSQL Error: 402 The data types ntext and varchar are incompatible in the equal to operator
211
	 *
212 2
	 * @param int $bbcode_id bbcode identifier
213 2
	 * @param array $bbcode_data bbcode data
214 2
	 * @access protected
215
	 */
216
	protected function remove_bbcode($bbcode_id, array $bbcode_data)
217
	{
218
		static $is_mssql;
219
220
		if (!isset($is_mssql))
221
		{
222
			$is_mssql = strpos($this->db->get_sql_layer(), 'mssql') === 0;
223
		}
224
225
		$sql = 'DELETE FROM ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
226
			WHERE ' . ($is_mssql ? 'CONVERT(NVARCHAR(MAX), first_pass_match) = N' : 'first_pass_match = ') . "'" . $this->db->sql_escape($bbcode_data['first_pass_match']) . "'
227
				AND " . ($is_mssql ? 'CONVERT(NVARCHAR(MAX), first_pass_replace) = N' : 'first_pass_replace = ') . "'" . $this->db->sql_escape($bbcode_data['first_pass_replace']) . "'
228
				AND bbcode_id = " . (int) $bbcode_id;
229
230
		$this->db->sql_query($sql);
231
	}
232
}
233