Completed
Push — master ( 13cf44...c90ced )
by Stanislav
22s queued 16s
created

helper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.8333
cc 1
nc 1
nop 13
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
*
4
* @package phpBB Gallery Extension
5
* @copyright (c) 2014 Lucifer
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
namespace phpbbgallery\core\notification;
10
11
use Symfony\Component\DependencyInjection\Container;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\DependencyInjection\Container 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...
12
13
class helper
14
{
15
	protected $config;
16
	protected $db;
17
	protected $request;
18
	protected $template;
19
	protected $user;
20
	protected $gallery_auth;
21
	protected $album_load;
22
	protected $helper;
23
	protected $url;
24
	protected $phpbb_container;
25
	protected $root_path;
26
	protected $php_ext;
27
	protected $watch_table;
28
	protected $image;
29
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user,
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...
Bug introduced by
The type phpbb\user 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...
Bug introduced by
The type phpbb\template\template 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...
Bug introduced by
The type phpbb\config\config 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...
Bug introduced by
The type phpbb\request\request 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...
30
	\phpbbgallery\core\auth\auth $gallery_auth, \phpbbgallery\core\album\loader $album_load, \phpbb\controller\helper $helper, \phpbbgallery\core\url $url,
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper 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...
31
	Container $phpbb_container, $root_path, $php_ext, $watch_table)
32
	{
33
		$this->config = $config;
34
		$this->db = $db;
35
		$this->request = $request;
36
		$this->template = $template;
37
		$this->user = $user;
38
		$this->gallery_auth = $gallery_auth;
39
		$this->album_load = $album_load;
40
		$this->helper = $helper;
41
		$this->url = $url;
42
		$this->phpbb_container = $phpbb_container;
43
		$this->root_path = $root_path;
44
		$this->php_ext = $php_ext;
45
		$this->watch_table = $watch_table;
46
	}
47
48
	/**
49
	 * Main notification function
50
	 *
51
	 * @param $type
52
	 * @param $target
53
	 * @throws \Exception
54
	 */
55
	public function notify($type, $target)
56
	{
57
		$phpbb_notifications = $this->phpbb_container->get('notification_manager');
58
		switch ($type)
59
		{
60
			case 'approval':
61
				$targets = $this->gallery_auth->acl_users_ids('m_status', $target['album_id']);
62
				$album_data = $this->album_load->get($target['album_id']);
63
				$notification_data = array(
64
					'user_ids' => $targets,
65
					'album_id' => $target['album_id'],
66
					'album_name' => $album_data['album_name'],
67
					'last_image_id'	=> $target['last_image'],
68
					'uploader'	=> $target['uploader'],
69
					'album_url'	=> $this->url->get_uri($this->helper->route('phpbbgallery_core_album', array('album_id' => $target['album_id']))),
70
				);
71
				$phpbb_notifications->add_notifications('phpbbgallery.core.notification.image_for_approval', $notification_data);
72
			break;
73
			case 'approved':
74
				$targets = $target['targets'];
75
				$album_data = $this->album_load->get($target['album_id']);
76
				$notification_data = array(
77
					'user_ids' => $targets,
78
					'album_id' => $target['album_id'],
79
					'album_name' => $album_data['album_name'],
80
					'last_image_id'	=> $target['last_image'],
81
					'album_url'	=> $this->url->get_uri($this->helper->route('phpbbgallery_core_album', array('album_id' => $target['album_id']))),
82
				);
83
				$phpbb_notifications->add_notifications('phpbbgallery.core.notification.image_approved', $notification_data);
84
			break;
85
			case 'not_approved':
86
				$targets = $target['targets'];
87
				$album_data = $this->album_load->get($target['album_id']);
88
				$notification_data = array(
89
					'user_ids' => $targets,
90
					'album_id' => $target['album_id'],
91
					'album_name' => $album_data['album_name'],
92
					'last_image_id'	=> $target['last_image'],
93
					'album_url'	=> $this->url->get_uri($this->helper->route('phpbbgallery_core_album', array('album_id' => $target['album_id']))),
94
				);
95
				$phpbb_notifications->add_notifications('phpbbgallery.core.notification.image_not_approved', $notification_data);
96
			break;
97
			case 'new_image':
98
				$targets = $target['targets'];
99
				$album_data = $this->album_load->get($target['album_id']);
100
				$notification_data = array(
101
					'user_ids' => $targets,
102
					'album_id' => $target['album_id'],
103
					'album_name' => $album_data['album_name'],
104
					'last_image_id'	=> $target['last_image'],
105
					'album_url'	=> $this->url->get_uri($this->helper->route('phpbbgallery_core_album', array('album_id' => $target['album_id']))),
106
				);
107
				$phpbb_notifications->add_notifications('phpbbgallery.core.notification.new_image', $notification_data);
108
			break;
109
			case 'new_comment':
110
				$notification_data = array(
111
					'user_ids'	=> array_diff($this->get_image_watchers($target['image_id']), array($target['poster_id'])),
112
					'image_id'	=> $target['image_id'],
113
					'comment_id'	=> $target['comment_id'],
114
					'poster'	=> $target['poster_id'],
115
					'url'		=> $this->url->get_uri($this->helper->route('phpbbgallery_core_image', array('image_id' => $target['image_id']))),
116
				);
117
				$phpbb_notifications->add_notifications('phpbbgallery.core.notification.new_comment', $notification_data);
118
			break;
119
			case 'new_report':
120
				if ($target['reported_album_id'] == 0)
121
				{
122
					$image_data = $this->image->get_image_data($target['reported_image_id']);
123
					$target['reported_album_id'] = $image_data['image_album_id'];
124
				}
125
				$notification_data = array(
126
					'user_ids'	=> array_diff($this->gallery_auth->acl_users_ids('m_report', $target['reported_album_id']), array($target['reporter_id'])),
127
					'item_id'	=> $target['report_id'],
128
					'reporter'	=> $target['reporter_id'],
129
					'url'		=> $this->url->get_uri($this->helper->route('phpbbgallery_core_moderate_image', array('image_id' => $target['reported_image_id']))),
130
				);
131
				$phpbb_notifications->add_notifications('phpbbgallery.core.notification.new_report', $notification_data);
132
			break;
133
			///case 'add':
134
			//	$phpbb_notifications->add_notifications('notification.type.zebraadd', $notification_data);
135
			//break;
136
			//case 'confirm':
137
			//	$phpbb_notifications->add_notifications('notification.type.zebraconfirm', $notification_data);
138
			//break;
139
		}
140
	}
141
	public function delete_notifications($type, $target)
142
	{
143
		$phpbb_notifications = $this->phpbb_container->get('notification_manager');
144
		switch ($type)
145
		{
146
			case 'report':
147
				$phpbb_notifications->delete_notifications('phpbbgallery.core.notification.new_report', $target);
148
			break;
149
		}
150
	}
151
152
	// Read notification (in some cases it is needed)
153
	public function read($type, $target)
154
	{
155
		$phpbb_notifications = $this->phpbb_container->get('notification_manager');
156
		switch ($type)
157
		{
158
			case 'approval':
159
				$phpbb_notifications->mark_notifications_read_by_parent('phpbbgallery.core.notification.image_for_approval', $target, false);
160
			break;
161
		}
162
	}
163
164
	/**
165
	 * Get watched for album
166
	 *
167
	 * @param (int) $album_id    Album we check
168
	 * @param bool $user_id
169
	 * @return
170
	 */
171
	public function get_watched_album($album_id, $user_id = false)
172
	{
173
		if (!$user_id)
174
		{
175
			$user_id = $this->user->data['user_id'];
176
		}
177
		$sql = 'SELECT COUNT(watch_id) as count FROM ' . $this->watch_table . ' WHERE album_id = ' . (int) $album_id . ' and user_id = ' . (int) $user_id;
178
		$result = $this->db->sql_query($sql);
179
		$row = $this->db->sql_fetchrow($result);
180
		$this->db->sql_freeresult($result);
181
		return $row['count'];
182
	}
183
184
	/**
185
	 * Get album watchers
186
	 * @param $album_id
187
	 * @return array
188
	 */
189
	public function get_album_watchers($album_id)
190
	{
191
		$sql = 'SELECT user_id FROM ' . $this->watch_table . ' WHERE album_id = ' . (int) $album_id;
192
		$result = $this->db->sql_query($sql);
193
		$watchers = array();
194
		while ($row = $this->db->sql_fetchrow($result))
195
		{
196
			$watchers[] = $row['user_id'];
197
		}
198
199
		return $watchers;
200
	}
201
202
	/**
203
	 * Get album watchers
204
	 * @param $image_id
205
	 * @return array
206
	 */
207
	public function get_image_watchers($image_id)
208
	{
209
		$sql = 'SELECT user_id FROM ' . $this->watch_table . ' WHERE image_id = ' . (int) $image_id;
210
		$result = $this->db->sql_query($sql);
211
		$watchers = array();
212
		while ($row = $this->db->sql_fetchrow($result))
213
		{
214
			$watchers[] = $row['user_id'];
215
		}
216
217
		return $watchers;
218
	}
219
220
	/**
221
	 * Add albums to watch-list
222
	 *
223
	 * @param    mixed $album_ids Array or integer with album_id where we delete from the watch-list.
224
	 * @param bool|int $user_id If not set, it uses the currents user_id
225
	 */
226
	public function add_albums($album_ids, $user_id = false)
227
	{
228
		$album_ids = $this->cast_mixed_int2array($album_ids);
229
		$user_id = (int) (($user_id) ? $user_id : $this->user->data['user_id']);
230
231
		// First check if we are not subscribed alredy for some
232
		$sql = 'SELECT * FROM ' . $this->watch_table . ' WHERE user_id = ' . $user_id . ' and ' . $this->db->sql_in_set('album_id', $album_ids);
233
		$result = $this->db->sql_query($sql);
234
		$exclude = array();
235
		while ($row = $this->db->sql_fetchrow($result))
236
		{
237
			$exclude[] = (int) $row['album_id'];
238
		}
239
		$album_ids = array_diff($album_ids, $exclude);
240
		foreach ($album_ids as $album_id)
241
		{
242
			$sql_ary = array(
243
				'album_id'		=> $album_id,
244
				'user_id'		=> $user_id,
245
			);
246
			$sql = 'INSERT INTO ' . $this->watch_table . $this->db->sql_build_array('INSERT', $sql_ary);
247
			$this->db->sql_query($sql);
248
		}
249
	}
250
251
	/**
252
	* Remove albums from watch-list
253
	*
254
	* @param	mixed	$album_ids		Array or integer with album_id where we delete from the watch-list.
255
	* @param	mixed	$user_ids		If not set, it uses the currents user_id
256
	*/
257
	public function remove_albums($album_ids, $user_ids = false)
258
	{
259
		$album_ids = $this->cast_mixed_int2array($album_ids);
260
		$user_ids = $this->cast_mixed_int2array((($user_ids) ? $user_ids : $this->user->data['user_id']));
261
262
		$sql = 'DELETE FROM ' . $this->watch_table . '
263
			WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . '
264
				AND ' . $this->db->sql_in_set('album_id', $album_ids);
265
		$this->db->sql_query($sql);
266
	}
267
268
	/**
269
	 *
270
	 * Cast int or array to array
271
	 *
272
	 * @param (mixed) $ids
273
	 * @return array
274
	 */
275
	static public function cast_mixed_int2array($ids)
276
	{
277
		if (is_array($ids))
278
		{
279
			return array_map('intval', $ids);
280
		}
281
		else
282
		{
283
			return array((int) $ids);
284
		}
285
	}
286
287
	/**
288
	 *
289
	 * New image in album
290
	 * @param $data
291
	 */
292
	public function new_image($data)
293
	{
294
		$get_watchers = $this->get_album_watchers($data['album_id']);
295
		// let's exclude all users that are uploadoing something and are approved
296
		$targets = array_diff($get_watchers, $data['targets']);
297
298
		$data['targets'] = $targets;
299
		$this->notify('new_image', $data);
300
	}
301
302
	public function set_image(\phpbbgallery\core\image\image $image)
303
	{
304
		$this->image = $image;
305
	}
306
}
307