Completed
Push — master ( 9718a3...ba473b )
by Matt
07:13
created

bbcodes_installer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 13
c 5
b 0
f 1
lcom 1
cbo 1
dl 0
loc 168
ccs 62
cts 62
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A install_bbcodes() 0 17 3
A get_acp_bbcodes() 0 9 2
A get_max_bbcode_id() 0 4 1
A bbcode_exists() 0 12 1
A update_bbcode() 0 7 1
A add_bbcode() 0 18 3
A build_bbcode() 0 14 1
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
				continue;
70
			}
71
72 2
			$this->add_bbcode($bbcode_data);
73 2
		}
74
75 2
		$this->resynchronize_bbcode_order();
76 2
	}
77
78
	/**
79
	 * Get the acp_bbcodes class
80
	 *
81
	 * @return \acp_bbcodes
82
	 * @access protected
83
	 */
84 2
	protected function get_acp_bbcodes()
85
	{
86 2
		if (!class_exists('acp_bbcodes'))
87 2
		{
88 1
			include($this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext);
89 1
		}
90
91 2
		return new \acp_bbcodes();
92
	}
93
94
	/**
95
	 * Build the bbcode
96
	 *
97
	 * @param array $bbcode_data Initial bbcode data
98
	 * @return array Complete bbcode data array
99
	 * @access protected
100
	 */
101 2
	protected function build_bbcode(array $bbcode_data)
102
	{
103 2
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
104
105 2
		$bbcode_data = array_replace($bbcode_data, array(
106 2
			'bbcode_tag'          => $data['bbcode_tag'],
107 2
			'first_pass_match'    => $data['first_pass_match'],
108 2
			'first_pass_replace'  => $data['first_pass_replace'],
109 2
			'second_pass_match'   => $data['second_pass_match'],
110 2
			'second_pass_replace' => $data['second_pass_replace'],
111 2
		));
112
113 2
		return $bbcode_data;
114
	}
115
116
	/**
117
	 * Get the max bbcode id value
118
	 *
119
	 * @return int bbcode identifier
120
	 * @access protected
121
	 */
122 2
	protected function get_max_bbcode_id()
123
	{
124 2
		return $this->get_max_column_value('bbcode_id');
125
	}
126
127
	/**
128
	 * Check if bbcode exists
129
	 *
130
	 * @param string $bbcode_name Name of bbcode
131
	 * @param string $bbcode_tag  Tag name of bbcode
132
	 * @return mixed Existing bbcode data array or false if not found
133
	 * @access protected
134
	 */
135 2
	protected function bbcode_exists($bbcode_name, $bbcode_tag)
136
	{
137
		$sql = 'SELECT bbcode_id
138 2
			FROM ' . BBCODES_TABLE . "
139 2
			WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "'
140 2
			OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'";
141 2
		$result = $this->db->sql_query($sql);
142 2
		$row = $this->db->sql_fetchrow($result);
143 2
		$this->db->sql_freeresult($result);
144
145 2
		return $row;
146
	}
147
148
	/**
149
	 * Update existing bbcode
150
	 *
151
	 * @param array $old_bbcode Existing bbcode data
152
	 * @param array $new_bbcode New bbcode data
153
	 * @return null
154
	 * @access protected
155
	 */
156 1
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
157
	{
158 1
		$sql = 'UPDATE ' . BBCODES_TABLE . '
159 1
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
160 1
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
161 1
		$this->db->sql_query($sql);
162 1
	}
163
164
	/**
165
	 * Add new bbcode
166
	 *
167
	 * @param array $bbcode_data New bbcode data
168
	 * @return null
169
	 * @access protected
170
	 */
171 2
	protected function add_bbcode(array $bbcode_data)
172
	{
173 2
		$bbcode_id = $this->get_max_bbcode_id() + 1;
174
175 2
		if ($bbcode_id <= NUM_CORE_BBCODES)
176 2
		{
177 1
			$bbcode_id = NUM_CORE_BBCODES + 1;
178 1
		}
179
180 2
		if ($bbcode_id <= BBCODE_LIMIT)
181 2
		{
182 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
183
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
184 2
			$bbcode_data['display_on_posting'] = (int) !array_key_exists('display_on_posting', $bbcode_data);
185
186 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
187 2
		}
188 2
	}
189
}
190