Completed
Push — master ( b9b537...6eecb7 )
by Matt
7s
created

manager::check_condition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 4
nc 1
nop 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 Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Auto Groups service manager class
17
 */
18
class manager
19
{
20
	/** @var array Array with auto group types */
21
	protected $autogroups_types;
22
23
	/** @var ContainerInterface */
24
	protected $phpbb_container;
25
26
	/** @var \phpbb\cache\driver\driver_interface */
27
	protected $cache;
28
29
	/** @var \phpbb\db\driver\driver_interface */
30
	protected $db;
31
32
	/** @var \phpbb\user */
33
	protected $user;
34
35
	/** @var string The database table the auto group rules are stored in */
36
	protected $autogroups_rules_table;
37
38
	/** @var string The database table the auto group types are stored in */
39
	protected $autogroups_types_table;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param array                                $autogroups_types       Array with auto group types
45
	 * @param ContainerInterface                   $phpbb_container        Service container interface
46
	 * @param \phpbb\cache\driver\driver_interface $cache                  Cache driver interface
47
	 * @param \phpbb\db\driver\driver_interface    $db                     Database object
48
	 * @param \phpbb\user                          $user                   User object
49
	 * @param string                               $autogroups_rules_table Name of the table used to store auto group rules data
50
	 * @param string                               $autogroups_types_table Name of the table used to store auto group types data
51
	 *
52
	 * @access public
53
	 */
54
	public function __construct($autogroups_types, ContainerInterface $phpbb_container, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $autogroups_rules_table, $autogroups_types_table)
55
	{
56
		$this->autogroups_types = $autogroups_types;
57
		$this->phpbb_container = $phpbb_container;
58
		$this->cache = $cache;
59
		$this->db = $db;
60
		$this->user = $user;
61
		$this->autogroups_rules_table = $autogroups_rules_table;
62
		$this->autogroups_types_table = $autogroups_types_table;
63
	}
64
65
	/**
66
	 * Check auto groups conditions and execute them
67
	 *
68
	 * @return null
69
	 * @access public
70
	 */
71
	public function check_conditions()
72
	{
73
		foreach ($this->autogroups_types as $autogroups_type => $data)
74
		{
75
			$this->check_condition($autogroups_type);
76
		}
77
	}
78
79
	/**
80
	 * Check auto groups condition and execute it
81
	 *
82
	 * @param string $type_name Name of the condition
83
	 * @param array  $options   Array of optional data
84
	 *
85
	 * @return null
86
	 * @access public
87
	 */
88
	public function check_condition($type_name, $options = array())
89
	{
90
		// Get an instance of the condition type to check
91
		$condition = $this->phpbb_container->get($type_name);
92
93
		// Get the user id array of users to check
94
		$check_users = $condition->get_users_for_condition($options);
95
96
		// Check the users and auto group them
97
		$condition->check($check_users, $options);
98
	}
99
100
	/**
101
	 * Add new condition type
102
	 *
103
	 * @param string $autogroups_type_name The name of the auto group type
104
	 *
105
	 * @return int The identifier of the new condition type
106
	 * @access public
107
	 */
108
	public function add_autogroups_type($autogroups_type_name)
109
	{
110
		// Insert the type name into the database
111
		$sql = 'INSERT INTO ' . $this->autogroups_types_table . ' ' .
112
			$this->db->sql_build_array('INSERT', array(
113
				'autogroups_type_name' => (string) $autogroups_type_name
114
			)
115
		);
116
		$this->db->sql_query($sql);
117
118
		// Return the id of the newly inserted condition type
119
		return (int) $this->db->sql_nextid();
120
	}
121
122
	/**
123
	 * Purge all conditions of a certain type
124
	 * (Note: This method is not used directly by Auto Groups, but is
125
	 * used in the purge step of extensions extending Auto Groups.)
126
	 *
127
	 * @param string $autogroups_type_name The name of the auto group type
128
	 *
129
	 * @return null
130
	 * @access public
131
	 */
132
	public function purge_autogroups_type($autogroups_type_name)
133
	{
134
		try
135
		{
136
			// Get the id of the condition
137
			$condition_type_id = $this->get_autogroups_type_id($autogroups_type_name);
138
139
			// Delete any rules associated with the condition id
140
			$sql = 'DELETE FROM ' . $this->autogroups_rules_table . '
141
				WHERE autogroups_type_id = ' . (int) $condition_type_id;
142
			$this->db->sql_query($sql);
143
144
			// Delete any types associated with the condition id
145
			$sql = 'DELETE FROM ' . $this->autogroups_types_table . '
146
				WHERE autogroups_type_id = ' . (int) $condition_type_id;
147
			$this->db->sql_query($sql);
148
149
			// Clear any cached autogroups data
150
			$this->cache->destroy('_autogroups_type_ids');
151
		}
152
		catch (\RuntimeException $e)
153
		{
154
			// Continue
155
		}
156
	}
157
158
	/**
159
	 * Purge all autogroups rules related to a certain group_id
160
	 *
161
	 * @param int $group_id Group identifier
162
	 * @return null
163
	 * @access public
164
	 */
165
	public function purge_autogroups_group($group_id)
166
	{
167
		// Delete any rules associated with the group id
168
		$sql = 'DELETE FROM ' . $this->autogroups_rules_table . '
169
			WHERE autogroups_group_id = ' . (int) $group_id;
170
		$this->db->sql_query($sql);
171
	}
172
173
	/**
174
	 * Get the condition type id from the name
175
	 *
176
	 * @param string $autogroups_type_name The name of the auto group type
177
	 *
178
	 * @return int The condition_type_id
179
	 * @throws \RuntimeException
180
	 */
181
	public function get_autogroups_type_id($autogroups_type_name)
182
	{
183
		// Get cached auto groups ids if they exist
184
		$autogroups_type_ids = $this->cache->get('_autogroups_type_ids');
185
186
		// Get auto groups ids from the db if no cache data exists, cache result
187
		if ($autogroups_type_ids === false)
188
		{
189
			$autogroups_type_ids = array();
190
191
			$sql = 'SELECT autogroups_type_id, autogroups_type_name
192
				FROM ' . $this->autogroups_types_table;
193
			$result = $this->db->sql_query($sql);
194
			while ($row = $this->db->sql_fetchrow($result))
195
			{
196
				$autogroups_type_ids[$row['autogroups_type_name']] = (int) $row['autogroups_type_id'];
197
			}
198
			$this->db->sql_freeresult($result);
199
200
			$this->cache->put('_autogroups_type_ids', $autogroups_type_ids);
201
		}
202
203
		// Add auto group type name to db if it exists as service but is not in db, cache result
204
		if (!isset($autogroups_type_ids[$autogroups_type_name]))
205
		{
206
			if (!isset($this->autogroups_types[$autogroups_type_name]))
207
			{
208
				throw new \RuntimeException($this->user->lang('AUTOGROUPS_TYPE_NOT_EXIST', $autogroups_type_name));
209
			}
210
211
			$autogroups_type_ids[$autogroups_type_name] = $this->add_autogroups_type($autogroups_type_name);
212
213
			$this->cache->put('_autogroups_type_ids', $autogroups_type_ids);
214
		}
215
216
		return $autogroups_type_ids[$autogroups_type_name];
217
	}
218
219
	/**
220
	 * Get condition type ids (as an array)
221
	 *
222
	 * @return array Array of condition type ids
223
	 * @access public
224
	 */
225
	public function get_autogroups_type_ids()
226
	{
227
		$autogroups_type_ids = array();
228
229
		foreach ($this->autogroups_types as $type_name => $data)
230
		{
231
			$autogroups_type_ids[$type_name] = $this->get_autogroups_type_id($type_name);
232
		}
233
234
		return $autogroups_type_ids;
235
	}
236
237
	/**
238
	 * Get the condition type name from the condition or rule id
239
	 *
240
	 * @param int     $type_id      The id of the auto group type
241
	 * @param int     $rule_id      The id of the auto group rule
242
	 *
243
	 * @return string|bool The condition type name, false on error
244
	 * @access public
245
	 */
246
	public function get_autogroups_type_name($type_id = 0, $rule_id = 0)
247
	{
248
		$sql_array = array(
249
			'SELECT'	=> 'agt.autogroups_type_name',
250
			'FROM'		=> array(
251
				$this->autogroups_types_table => 'agt',
252
			),
253
		);
254
255
		if ($type_id)
256
		{
257
			$sql_array['WHERE'] = 'agt.autogroups_type_id = ' . (int) $type_id;
258
		}
259
		else if ($rule_id)
260
		{
261
			$sql_array['LEFT_JOIN'] = array(
262
				array(
263
					'FROM'	=>	array($this->autogroups_rules_table	=> 'agr'),
264
					'ON'	=> 'agt.autogroups_type_id = agr.autogroups_type_id',
265
				),
266
			);
267
			$sql_array['WHERE'] = 'agr.autogroups_id = ' . (int) $rule_id;
268
		}
269
		else
270
		{
271
			return false;
272
		}
273
274
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
275
		$result = $this->db->sql_query($sql);
276
		$autogroups_type_name = $this->db->sql_fetchfield('autogroups_type_name');
277
		$this->db->sql_freeresult($result);
278
279
		return $autogroups_type_name;
280
	}
281
282
	/**
283
	 * Get the condition language var from the condition type class
284
	 *
285
	 * @param string     $autogroups_type_name      The name of the auto group type
286
	 *
287
	 * @return string The condition type name
288
	 * @access public
289
	 */
290
	public function get_condition_lang($autogroups_type_name)
291
	{
292
		try
293
		{
294
			$condition = $this->phpbb_container->get($autogroups_type_name);
295
		}
296
		catch (\InvalidArgumentException $e)
297
		{
298
			return $this->user->lang('AUTOGROUPS_TYPE_NOT_EXIST', $autogroups_type_name);
299
		}
300
301
		return $condition->get_condition_type_name();
302
	}
303
304
	/**
305
	 * Run auto groups check against users for a given condition/type
306
	 * Called in the ACP when adding/editing or via the Resync button
307
	 *
308
	 * @param int     $autogroups_rule_id      The id of the auto group rule
309
	 *
310
	 * @return null
311
	 * @access public
312
	 */
313
	public function sync_autogroups($autogroups_rule_id)
314
	{
315
		// Purge cached rules table queries
316
		$this->cache->destroy('sql', $this->autogroups_rules_table);
317
318
		// Get the auto group type name used by the specified auto group rule
319
		$autogroups_type_name = $this->get_autogroups_type_name(0, $autogroups_rule_id);
320
321
		// If auto group type exists, run it
322
		if ($autogroups_type_name !== false)
323
		{
324
			$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 319 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...
325
				'action'	=> 'sync',
326
			));
327
		}
328
	}
329
}
330