helper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 130
rs 10
c 2
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A send_notifications() 0 11 2
A __construct() 0 6 1
A get_default_exempt_users() 0 28 2
A prepare_users_for_query() 0 9 2
A get_users_groups() 0 15 2
1
<?php
2
/**
3
*
4
* Auto Groups extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2015 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\type;
12
13
/**
14
 * Auto Groups conditions type helper class
15
 */
16
class helper
17
{
18
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
	protected $db;
20
21
	/** @var \phpbb\notification\manager */
0 ignored issues
show
Bug introduced by
The type phpbb\notification\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
	protected $notification_manager;
23
24
	/** @var string */
25
	protected $phpbb_root_path;
26
27
	/** @var string */
28
	protected $php_ext;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param \phpbb\db\driver\driver_interface $db                     Database object
34
	 * @param \phpbb\notification\manager       $notification_manager   Notification manager
35
	 * @param string                            $phpbb_root_path        phpBB root path
36
	 * @param string                            $php_ext                phpEx
37
	 *
38
	 * @access public
39
	 */
40
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\notification\manager $notification_manager, $phpbb_root_path, $php_ext)
41
	{
42
		$this->db = $db;
43
		$this->notification_manager = $notification_manager;
44
		$this->phpbb_root_path = $phpbb_root_path;
45
		$this->php_ext = $php_ext;
46
	}
47
48
	/**
49
	 * Get user's group ids
50
	 *
51
	 * @param array $user_id_ary An array of user ids to check
52
	 * @return array An array of usergroup ids each user belongs to
53
	 * @access public
54
	 */
55
	public function get_users_groups($user_id_ary)
56
	{
57
		$group_id_ary = array();
58
59
		$sql = 'SELECT user_id, group_id
60
			FROM ' . USER_GROUP_TABLE . '
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\USER_GROUP_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
			WHERE ' . $this->db->sql_in_set('user_id', $user_id_ary, false, true);
62
		$result = $this->db->sql_query($sql);
63
		while ($row = $this->db->sql_fetchrow($result))
64
		{
65
			$group_id_ary[$row['user_id']][] = $row['group_id'];
66
		}
67
		$this->db->sql_freeresult($result);
68
69
		return $group_id_ary;
70
	}
71
72
	/**
73
	 * Get users that should not have their default status changed
74
	 *
75
	 * @return array An array of user ids
76
	 * @access public
77
	 */
78
	public function get_default_exempt_users()
79
	{
80
		$user_id_ary = array();
81
82
		// Get users whose default group is autogroup_default_exempt
83
		$sql_array = array(
84
			'SELECT'	=> 'u.user_id',
85
			'FROM'		=> array(
86
				USERS_TABLE	=> 'u',
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
87
			),
88
			'LEFT_JOIN'	=> array(
89
				array(
90
					'FROM'	=> array(GROUPS_TABLE => 'g'),
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\GROUPS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
91
					'ON'	=> 'g.group_id = u.group_id',
92
				),
93
			),
94
			'WHERE'		=> 'g.autogroup_default_exempt = 1',
95
		);
96
97
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
98
		$result = $this->db->sql_query($sql);
99
		while ($row = $this->db->sql_fetchrow($result))
100
		{
101
			$user_id_ary[] = $row['user_id'];
102
		}
103
		$this->db->sql_freeresult($result);
104
105
		return $user_id_ary;
106
	}
107
108
	/**
109
	 * Prepare user ids for querying
110
	 *
111
	 * @param mixed $user_ids User id(s) expected as int or array
112
	 * @return array An array of user id(s)
113
	 * @access public
114
	 */
115
	public function prepare_users_for_query($user_ids)
116
	{
117
		if (!is_array($user_ids))
118
		{
119
			$user_ids = array($user_ids);
120
		}
121
122
		// Cast each array value to integer
123
		return array_map('intval', $user_ids);
124
	}
125
126
	/**
127
	 * Send notifications
128
	 *
129
	 * @param string $type       Type of notification to send (group_added|group_removed)
130
	 * @param array $user_id_ary Array of user(s) to notify
131
	 * @param int $group_id      The usergroup identifier
132
	 * @return void
133
	 * @access public
134
	 */
135
	public function send_notifications($type, $user_id_ary, $group_id)
136
	{
137
		if (!function_exists('get_group_name'))
138
		{
139
			include $this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext;
140
		}
141
142
		$this->notification_manager->add_notifications("phpbb.autogroups.notification.type.$type", array(
143
			'user_ids'		=> $user_id_ary,
144
			'group_id'		=> $group_id,
145
			'group_name'	=> get_group_name($group_id),
146
		));
147
	}
148
}
149