Completed
Pull Request — master (#122)
by Matt
04:11 queued 37s
created

manager   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 312
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 22
lcom 1
cbo 0
dl 0
loc 312
rs 10
c 4
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A check_conditions() 0 7 2
A check_condition() 0 11 1
A add_autogroups_type() 0 13 1
B purge_autogroups_type() 0 25 2
A purge_autogroups_group() 0 7 1
B get_autogroups_type_id() 0 37 5
A get_autogroups_type_ids() 0 11 2
B get_autogroups_type_name() 0 35 3
A get_condition_lang() 0 13 2
A sync_autogroups() 0 16 2
1
<?php
2
/**
3
*
4
* Auto Groups extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace phpbb\autogroups\conditions;
12
13
use phpbb\exception\runtime_exception;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * Auto Groups service manager class
18
 */
19
class manager
20
{
21
	/** @var array Array with auto group types */
22
	protected $autogroups_types;
23
24
	/** @var ContainerInterface */
25
	protected $phpbb_container;
26
27
	/** @var \phpbb\cache\driver\driver_interface */
28
	protected $cache;
29
30
	/** @var \phpbb\db\driver\driver_interface */
31
	protected $db;
32
33
	/** @var \phpbb\language\language */
34
	protected $language;
35
36
	/** @var string The database table the auto group rules are stored in */
37
	protected $autogroups_rules_table;
38
39
	/** @var string The database table the auto group types are stored in */
40
	protected $autogroups_types_table;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param array                                $autogroups_types       Array with auto group types
46
	 * @param ContainerInterface                   $phpbb_container        Service container interface
47
	 * @param \phpbb\cache\driver\driver_interface $cache                  Cache driver interface
48
	 * @param \phpbb\db\driver\driver_interface    $db                     Database object
49
	 * @param \phpbb\language\language             $language               Language object
50
	 * @param string                               $autogroups_rules_table Name of the table used to store auto group rules data
51
	 * @param string                               $autogroups_types_table Name of the table used to store auto group types data
52
	 *
53
	 * @access public
54
	 */
55
	public function __construct($autogroups_types, ContainerInterface $phpbb_container, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $language, $autogroups_rules_table, $autogroups_types_table)
56
	{
57
		$this->autogroups_types = $autogroups_types;
58
		$this->phpbb_container = $phpbb_container;
59
		$this->cache = $cache;
60
		$this->db = $db;
61
		$this->language = $language;
62
		$this->autogroups_rules_table = $autogroups_rules_table;
63
		$this->autogroups_types_table = $autogroups_types_table;
64
	}
65
66
	/**
67
	 * Check auto groups conditions and execute them
68
	 *
69
	 * @return void
70
	 * @access public
71
	 */
72
	public function check_conditions()
73
	{
74
		foreach ($this->autogroups_types as $autogroups_type => $data)
75
		{
76
			$this->check_condition($autogroups_type);
77
		}
78
	}
79
80
	/**
81
	 * Check auto groups condition and execute it
82
	 *
83
	 * @param string $type_name Name of the condition
84
	 * @param array  $options   Array of optional data
85
	 *
86
	 * @return void
87
	 * @access public
88
	 */
89
	public function check_condition($type_name, $options = array())
90
	{
91
		// Get an instance of the condition type to check
92
		$condition = $this->phpbb_container->get($type_name);
93
94
		// Get the user id array of users to check
95
		$check_users = $condition->get_users_for_condition($options);
96
97
		// Check the users and auto group them
98
		$condition->check($check_users, $options);
99
	}
100
101
	/**
102
	 * Add new condition type
103
	 *
104
	 * @param string $autogroups_type_name The name of the auto group type
105
	 *
106
	 * @return int The identifier of the new condition type
107
	 * @access public
108
	 */
109
	public function add_autogroups_type($autogroups_type_name)
110
	{
111
		// Insert the type name into the database
112
		$sql = 'INSERT INTO ' . $this->autogroups_types_table . ' ' .
113
			$this->db->sql_build_array('INSERT', array(
114
				'autogroups_type_name' => (string) $autogroups_type_name
115
			)
116
		);
117
		$this->db->sql_query($sql);
118
119
		// Return the id of the newly inserted condition type
120
		return (int) $this->db->sql_nextid();
121
	}
122
123
	/**
124
	 * Purge all conditions of a certain type
125
	 * (Note: This method is not used directly by Auto Groups, but is
126
	 * used in the purge step of extensions extending Auto Groups.)
127
	 *
128
	 * @param string $autogroups_type_name The name of the auto group type
129
	 *
130
	 * @return void
131
	 * @access public
132
	 */
133
	public function purge_autogroups_type($autogroups_type_name)
134
	{
135
		try
136
		{
137
			// Get the id of the condition
138
			$condition_type_id = $this->get_autogroups_type_id($autogroups_type_name);
139
140
			// Delete any rules associated with the condition id
141
			$sql = 'DELETE FROM ' . $this->autogroups_rules_table . '
142
				WHERE autogroups_type_id = ' . (int) $condition_type_id;
143
			$this->db->sql_query($sql);
144
145
			// Delete any types associated with the condition id
146
			$sql = 'DELETE FROM ' . $this->autogroups_types_table . '
147
				WHERE autogroups_type_id = ' . (int) $condition_type_id;
148
			$this->db->sql_query($sql);
149
150
			// Clear any cached autogroups data
151
			$this->cache->destroy('_autogroups_type_ids');
152
		}
153
		catch (runtime_exception $e)
0 ignored issues
show
Bug introduced by
The class phpbb\exception\runtime_exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
154
		{
155
			// Continue
156
		}
157
	}
158
159
	/**
160
	 * Purge all autogroups rules related to a certain group_id
161
	 *
162
	 * @param int $group_id Group identifier
163
	 * @return void
164
	 * @access public
165
	 */
166
	public function purge_autogroups_group($group_id)
167
	{
168
		// Delete any rules associated with the group id
169
		$sql = 'DELETE FROM ' . $this->autogroups_rules_table . '
170
			WHERE autogroups_group_id = ' . (int) $group_id;
171
		$this->db->sql_query($sql);
172
	}
173
174
	/**
175
	 * Get the condition type id from the name
176
	 *
177
	 * @param string $autogroups_type_name The name of the auto group type
178
	 *
179
	 * @return int The condition_type_id
180
	 * @throws runtime_exception
181
	 */
182
	public function get_autogroups_type_id($autogroups_type_name)
183
	{
184
		// Get cached auto groups ids if they exist
185
		$autogroups_type_ids = $this->cache->get('_autogroups_type_ids');
186
187
		// Get auto groups ids from the db if no cache data exists, cache result
188
		if ($autogroups_type_ids === false)
189
		{
190
			$autogroups_type_ids = array();
191
192
			$sql = 'SELECT autogroups_type_id, autogroups_type_name
193
				FROM ' . $this->autogroups_types_table;
194
			$result = $this->db->sql_query($sql);
195
			while ($row = $this->db->sql_fetchrow($result))
196
			{
197
				$autogroups_type_ids[$row['autogroups_type_name']] = (int) $row['autogroups_type_id'];
198
			}
199
			$this->db->sql_freeresult($result);
200
201
			$this->cache->put('_autogroups_type_ids', $autogroups_type_ids);
202
		}
203
204
		// Add auto group type name to db if it exists as service but is not in db, cache result
205
		if (!isset($autogroups_type_ids[$autogroups_type_name]))
206
		{
207
			if (!isset($this->autogroups_types[$autogroups_type_name]))
208
			{
209
				throw new runtime_exception('AUTOGROUPS_TYPE_NOT_EXIST', array($autogroups_type_name));
210
			}
211
212
			$autogroups_type_ids[$autogroups_type_name] = $this->add_autogroups_type($autogroups_type_name);
213
214
			$this->cache->put('_autogroups_type_ids', $autogroups_type_ids);
215
		}
216
217
		return $autogroups_type_ids[$autogroups_type_name];
218
	}
219
220
	/**
221
	 * Get condition type ids (as an array)
222
	 *
223
	 * @return array Array of condition type ids
224
	 * @access public
225
	 */
226
	public function get_autogroups_type_ids()
227
	{
228
		$autogroups_type_ids = array();
229
230
		foreach ($this->autogroups_types as $type_name => $data)
231
		{
232
			$autogroups_type_ids[$type_name] = $this->get_autogroups_type_id($type_name);
233
		}
234
235
		return $autogroups_type_ids;
236
	}
237
238
	/**
239
	 * Get the condition type name from the condition or rule id
240
	 *
241
	 * @param int     $type_id      The id of the auto group type
242
	 * @param int     $rule_id      The id of the auto group rule
243
	 *
244
	 * @return string|bool The condition type name, false on error
245
	 * @access public
246
	 */
247
	public function get_autogroups_type_name($type_id = 0, $rule_id = 0)
248
	{
249
		$sql_array = array(
250
			'SELECT'	=> 'agt.autogroups_type_name',
251
			'FROM'		=> array(
252
				$this->autogroups_types_table => 'agt',
253
			),
254
		);
255
256
		if ($type_id)
257
		{
258
			$sql_array['WHERE'] = 'agt.autogroups_type_id = ' . (int) $type_id;
259
		}
260
		else if ($rule_id)
261
		{
262
			$sql_array['LEFT_JOIN'] = array(
263
				array(
264
					'FROM'	=>	array($this->autogroups_rules_table	=> 'agr'),
265
					'ON'	=> 'agt.autogroups_type_id = agr.autogroups_type_id',
266
				),
267
			);
268
			$sql_array['WHERE'] = 'agr.autogroups_id = ' . (int) $rule_id;
269
		}
270
		else
271
		{
272
			return false;
273
		}
274
275
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
276
		$result = $this->db->sql_query($sql);
277
		$autogroups_type_name = $this->db->sql_fetchfield('autogroups_type_name');
278
		$this->db->sql_freeresult($result);
279
280
		return $autogroups_type_name;
281
	}
282
283
	/**
284
	 * Get the condition language var from the condition type class
285
	 *
286
	 * @param string     $autogroups_type_name      The name of the auto group type
287
	 *
288
	 * @return string The condition type name
289
	 * @access public
290
	 */
291
	public function get_condition_lang($autogroups_type_name)
292
	{
293
		try
294
		{
295
			$condition = $this->phpbb_container->get($autogroups_type_name);
296
		}
297
		catch (\InvalidArgumentException $e)
298
		{
299
			return $this->language->lang('AUTOGROUPS_TYPE_NOT_EXIST', $autogroups_type_name);
300
		}
301
302
		return $condition->get_condition_type_name();
303
	}
304
305
	/**
306
	 * Run auto groups check against users for a given condition/type
307
	 * Called in the ACP when adding/editing or via the Resync button
308
	 *
309
	 * @param int     $autogroups_rule_id      The id of the auto group rule
310
	 *
311
	 * @return void
312
	 * @access public
313
	 */
314
	public function sync_autogroups($autogroups_rule_id)
315
	{
316
		// Purge cached rules table queries
317
		$this->cache->destroy('sql', $this->autogroups_rules_table);
318
319
		// Get the auto group type name used by the specified auto group rule
320
		$autogroups_type_name = $this->get_autogroups_type_name(0, $autogroups_rule_id);
321
322
		// If auto group type exists, run it
323
		if ($autogroups_type_name !== false)
324
		{
325
			$this->check_condition($autogroups_type_name, array(
0 ignored issues
show
Bug introduced by
It seems like $autogroups_type_name defined by $this->get_autogroups_ty...0, $autogroups_rule_id) on line 320 can also be of type boolean; however, phpbb\autogroups\conditi...ager::check_condition() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
326
				'action'	=> 'sync',
327
			));
328
		}
329
	}
330
}
331