Completed
Pull Request — master (#28)
by Matt
08:09
created

bbcodes_installer::install_bbcodes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
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;
14
use phpbb\request\request;
15
use phpbb\user;
16
17
/**
18
 * Class bbcodes_installer
19
 *
20
 * @package vse\abbc3\core
21
 */
22
class bbcodes_installer extends acp_manager
23
{
24
	/** @var \acp_bbcodes */
25
	protected $acp_bbcodes;
26
27
	/** @var string */
28
	protected $phpbb_root_path;
29
30
	/** @var string */
31
	protected $php_ext;
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param driver_interface $db
37
	 * @param request          $request
38
	 * @param user             $user
39
	 * @param string           $phpbb_root_path
40
	 * @param string           $php_ext
41
	 * @access public
42
	 */
43 2
	public function __construct(driver_interface $db, request $request, user $user, $phpbb_root_path, $php_ext)
44
	{
45 2
		parent::__construct($db, $request, $user);
46
47 2
		$this->phpbb_root_path = $phpbb_root_path;
48 2
		$this->php_ext         = $php_ext;
49
50 2
		$this->acp_bbcodes     = $this->get_acp_bbcodes();
51 2
	}
52
53
	/**
54
	 * Installs bbcodes, used by migrations to perform add/updates
55
	 *
56
	 * @param array $bbcodes Array of bbcodes to install
57
	 * @return null
58
	 * @access public
59
	 */
60 2
	public function install_bbcodes(array $bbcodes)
61
	{
62 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
63
		{
64 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
65
66 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
67 2
			{
68 1
				$this->update_bbcode($bbcode, $bbcode_data);
69 1
			}
70
			else
71
			{
72 2
				$this->add_bbcode($bbcode_data);
73
			}
74 2
		}
75
76 2
		$this->resynchronize_bbcode_order();
77 2
	}
78
79
	/**
80
	 * Get the acp_bbcodes class
81
	 *
82
	 * @return \acp_bbcodes
83
	 * @access protected
84
	 */
85 2
	protected function get_acp_bbcodes()
86
	{
87 2
		if (!class_exists('acp_bbcodes'))
88 2
		{
89 1
			include($this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext);
90 1
		}
91
92 2
		return new \acp_bbcodes();
93
	}
94
95
	/**
96
	 * Build the bbcode
97
	 *
98
	 * @param array $bbcode_data Initial bbcode data
99
	 * @return array Complete bbcode data array
100
	 * @access protected
101
	 */
102 2
	protected function build_bbcode(array $bbcode_data)
103
	{
104 2
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
105
106
		$bbcode_data += array(
107 2
			'bbcode_tag'          => $data['bbcode_tag'],
108 2
			'first_pass_match'    => $data['first_pass_match'],
109 2
			'first_pass_replace'  => $data['first_pass_replace'],
110 2
			'second_pass_match'   => $data['second_pass_match'],
111 2
			'second_pass_replace' => $data['second_pass_replace'],
112
		);
113
114 2
		return $bbcode_data;
115
	}
116
117
	/**
118
	 * Get the max bbcode id value
119
	 *
120
	 * @return int bbcode identifier
121
	 * @access protected
122
	 */
123 2 View Code Duplication
	protected function get_max_bbcode_id()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
	{
125
		$sql = 'SELECT MAX(bbcode_id) AS max_bbcode_id
126 2
			FROM ' . BBCODES_TABLE;
127 2
		$result = $this->db->sql_query($sql);
128 2
		$max_bbcode_id = $this->db->sql_fetchfield('max_bbcode_id');
129 2
		$this->db->sql_freeresult($result);
130
131 2
		return (int) $max_bbcode_id;
132
	}
133
134
	/**
135
	 * Check if bbcode exists
136
	 *
137
	 * @param string $bbcode_name Name of bbcode
138
	 * @param string $bbcode_tag  Tag name of bbcode
139
	 * @return mixed Existing bbcode data array or false if not found
140
	 * @access protected
141
	 */
142 2
	protected function bbcode_exists($bbcode_name, $bbcode_tag)
143
	{
144
		$sql = 'SELECT bbcode_id
145 2
			FROM ' . BBCODES_TABLE . "
146 2
			WHERE LOWER(bbcode_tag) = '" . strtolower($bbcode_name) . "'
147 2
			OR LOWER(bbcode_tag) = '" . strtolower($bbcode_tag) . "'";
148 2
		$result = $this->db->sql_query($sql);
149 2
		$row = $this->db->sql_fetchrow($result);
150 2
		$this->db->sql_freeresult($result);
151
152 2
		return $row;
153
	}
154
155
	/**
156
	 * Update existing bbcode
157
	 *
158
	 * @param array $old_bbcode Existing bbcode data
159
	 * @param array $new_bbcode New bbcode data
160
	 * @return null
161
	 * @access protected
162
	 */
163 1
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
164
	{
165 1
		$sql = 'UPDATE ' . BBCODES_TABLE . '
166 1
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
167 1
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
168 1
		$this->db->sql_query($sql);
169 1
	}
170
171
	/**
172
	 * Add new bbcode
173
	 *
174
	 * @param array $bbcode_data New bbcode data
175
	 * @return null
176
	 * @access protected
177
	 */
178 2
	protected function add_bbcode(array $bbcode_data)
179
	{
180 2
		$bbcode_id = $this->get_max_bbcode_id() + 1;
181
182 2
		if ($bbcode_id <= NUM_CORE_BBCODES)
183 2
		{
184 1
			$bbcode_id = NUM_CORE_BBCODES + 1;
185 1
		}
186
187 2
		if ($bbcode_id <= BBCODE_LIMIT)
188 2
		{
189 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
190
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
191 2
			$bbcode_data['display_on_posting'] = (int) !isset($bbcode_data['display_on_posting']);
192
193 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
194 2
		}
195 2
	}
196
}
197