Completed
Push — develop-3.2.x ( 9a1518...6fced6 )
by Matt
11:19 queued 06:39
created

bbcodes_installer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 98.36%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 13
c 4
b 0
f 1
lcom 1
cbo 1
dl 0
loc 169
ccs 60
cts 61
cp 0.9836
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 build_bbcode() 0 14 1
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
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\group\helper;
15
use phpbb\request\request;
16
use phpbb\user;
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 */
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 request          $request
40
	 * @param user             $user
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, request $request, user $user, $phpbb_root_path, $php_ext)
46
	{
47 2
		parent::__construct($db, $group_helper, $request, $user);
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 2
	}
54
55
	/**
56
	 * Installs bbcodes, used by migrations to perform add/updates
57
	 *
58
	 * @param array $bbcodes Array of bbcodes to install
59
	 * @return null
60
	 * @access public
61
	 */
62 2
	public function install_bbcodes(array $bbcodes)
63
	{
64 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
65
		{
66 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
67
68 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
69 2
			{
70 1
				$this->update_bbcode($bbcode, $bbcode_data);
71 1
				continue;
72
			}
73
74 2
			$this->add_bbcode($bbcode_data);
75 2
		}
76
77 2
		$this->resynchronize_bbcode_order();
78 2
	}
79
80
	/**
81
	 * Get the acp_bbcodes class
82
	 *
83
	 * @return \acp_bbcodes
84
	 * @access protected
85
	 */
86 2
	protected function get_acp_bbcodes()
87
	{
88 2
		if (!class_exists('acp_bbcodes'))
89 2
		{
90 1
			include($this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext);
91 1
		}
92
93 2
		return new \acp_bbcodes();
94
	}
95
96
	/**
97
	 * Build the bbcode
98
	 *
99
	 * @param array $bbcode_data Initial bbcode data
100
	 * @return array Complete bbcode data array
101
	 * @access protected
102
	 */
103 2
	protected function build_bbcode(array $bbcode_data)
104
	{
105 2
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
106
107
		$bbcode_data += array(
108 2
			'bbcode_tag'          => $data['bbcode_tag'],
109 2
			'first_pass_match'    => $data['first_pass_match'],
110 2
			'first_pass_replace'  => $data['first_pass_replace'],
111 2
			'second_pass_match'   => $data['second_pass_match'],
112 2
			'second_pass_replace' => $data['second_pass_replace'],
113
		);
114
115 2
		return $bbcode_data;
116
	}
117
118
	/**
119
	 * Get the max bbcode id value
120
	 *
121
	 * @return int bbcode identifier
122
	 * @access protected
123
	 */
124 2
	protected function get_max_bbcode_id()
125
	{
126 2
		return $this->get_max_column_value('bbcode_id');
127
	}
128
129
	/**
130
	 * Check if bbcode exists
131
	 *
132
	 * @param string $bbcode_name Name of bbcode
133
	 * @param string $bbcode_tag  Tag name of bbcode
134
	 * @return mixed Existing bbcode data array or false if not found
135
	 * @access protected
136
	 */
137 2
	protected function bbcode_exists($bbcode_name, $bbcode_tag)
138
	{
139
		$sql = 'SELECT bbcode_id
140 2
			FROM ' . BBCODES_TABLE . "
141 2
			WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "'
142 2
			OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'";
143 2
		$result = $this->db->sql_query($sql);
144 2
		$row = $this->db->sql_fetchrow($result);
145 2
		$this->db->sql_freeresult($result);
146
147 2
		return $row;
148
	}
149
150
	/**
151
	 * Update existing bbcode
152
	 *
153
	 * @param array $old_bbcode Existing bbcode data
154
	 * @param array $new_bbcode New bbcode data
155
	 * @return null
156
	 * @access protected
157
	 */
158 1
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
159
	{
160 1
		$sql = 'UPDATE ' . BBCODES_TABLE . '
161 1
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
162 1
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
163 1
		$this->db->sql_query($sql);
164 1
	}
165
166
	/**
167
	 * Add new bbcode
168
	 *
169
	 * @param array $bbcode_data New bbcode data
170
	 * @return null
171
	 * @access protected
172
	 */
173 2
	protected function add_bbcode(array $bbcode_data)
174
	{
175 2
		$bbcode_id = $this->get_max_bbcode_id() + 1;
176
177 2
		if ($bbcode_id <= NUM_CORE_BBCODES)
178 2
		{
179 1
			$bbcode_id = NUM_CORE_BBCODES + 1;
180 1
		}
181
182 2
		if ($bbcode_id <= BBCODE_LIMIT)
183 2
		{
184 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
185
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
186 2
			$bbcode_data['display_on_posting'] = (int) !isset($bbcode_data['display_on_posting']);
187
188 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
189 2
		}
190 2
	}
191
}
192