album   F
last analyzed

Complexity

Total Complexity 60

Size/Duplication

Total Lines 472
Duplicated Lines 0 %

Test Coverage

Coverage 29.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 60
eloc 226
c 1
b 0
f 0
dl 0
loc 472
ccs 60
cts 201
cp 0.2985
rs 3.6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A check_user() 0 22 3
F get_albumbox() 0 136 45
A __construct() 0 18 1
A get_info() 0 46 5
B update_info() 0 77 3
A get_public_albums() 0 13 2
A generate_personal_album() 0 40 1

How to fix   Complexity   

Complex Class

Complex classes like album often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use album, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * phpBB Gallery - Core Extension
4
 *
5
 * @package   phpbbgallery/core
6
 * @author    satanasov
7
 * @author    Leinad4Mind
8
 * @copyright 2014- satanasov, 2018- Leinad4Mind
9
 * @license   GPL-2.0-only
10
 */
11
12
namespace phpbbgallery\core\album;
13
14
class album
15
{
16
	/** @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...
17
	protected $db;
18
19
	/** @var \phpbb\user */
0 ignored issues
show
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...
20
	protected $user;
21
22
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language 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...
23
	protected $language;
24
25
	/** @var \phpbb\profilefields\manager */
0 ignored issues
show
Bug introduced by
The type phpbb\profilefields\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...
26
	protected $user_cpf;
27
28
	/** @var \phpbbgallery\core\auth\auth */
29
	protected $gallery_auth;
30
31
	/** @var \phpbbgallery\core\cache */
32
	protected $gallery_cache;
33
34
	/** @var \phpbbgallery\core\block */
35
	protected $block;
36
37
	/** @var \phpbbgallery\core\config */
38
	protected $gallery_config;
39
40
	/** @var */
41
	protected $images_table;
42
43
	/** @var */
44
	protected $watch_table;
45
46
	/** @var */
47
	protected $contests_table;
48
49
	/** @var */
50
	protected $albums_table;
51
52
	/**
53
	 * album constructor.
54
	 *
55
	 * @param \phpbb\db\driver\driver_interface $db
56
	 * @param \phpbb\user                       $user
57
	 * @param language                          $language
0 ignored issues
show
Bug introduced by
The type phpbbgallery\core\album\language 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...
58
	 * @param \phpbb\profilefields\manager      $user_cpf
59
	 * @param \phpbbgallery\core\auth\auth      $gallery_auth
60
	 * @param \phpbbgallery\core\cache          $gallery_cache
61
	 * @param \phpbbgallery\core\block          $block
62
	 * @param \phpbbgallery\core\config         $gallery_config
63
	 * @param                                   $albums_table
64
	 * @param                                   $images_table
65
	 * @param                                   $watch_table
66
	 * @param                                   $contest_table
67
	 */
68 99
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user,
69
		\phpbb\language\language $language, \phpbb\profilefields\manager $user_cpf,
70
		\phpbbgallery\core\auth\auth $gallery_auth, \phpbbgallery\core\cache $gallery_cache, \phpbbgallery\core\block $block,
71
		\phpbbgallery\core\config $gallery_config,
72
		$albums_table, $images_table, $watch_table, $contest_table)
73
	{
74 99
		$this->db = $db;
75 99
		$this->user = $user;
76 99
		$this->language = $language;
77 99
		$this->user_cpf = $user_cpf;
78 99
		$this->gallery_auth = $gallery_auth;
79 99
		$this->gallery_cache = $gallery_cache;
80 99
		$this->block = $block;
81 99
		$this->gallery_config = $gallery_config;
82 99
		$this->albums_table = $albums_table;
83 99
		$this->images_table = $images_table;
84 99
		$this->watch_table = $watch_table;
85 99
		$this->contests_table = $contest_table;
86 99
	}
87
88
	/**
89
	 * Get album information
90
	 *
91
	 * @param      $album_id
92
	 * @param bool $extended_info
93
	 * @return mixed
94
	 */
95 6
	public function get_info($album_id, $extended_info = true)
96
	{
97
		$sql_array = array(
98 6
			'SELECT' => 'a.*',
99 6
			'FROM'   => array($this->albums_table => 'a'),
100
101 6
			'WHERE' => 'a.album_id = ' . (int) $album_id,
102
		);
103
104 6
		if ($extended_info)
105
		{
106 6
			$sql_array['SELECT'] .= ', c.*, w.watch_id';
107 6
			$sql_array['LEFT_JOIN'] = array(
108
				array(
109 6
					'FROM' => array($this->watch_table => 'w'),
110 6
					'ON'   => 'a.album_id = w.album_id AND w.user_id = ' . (int) $this->user->data['user_id'],
111
				),
112
				array(
113 6
					'FROM' => array($this->contests_table => 'c'),
114 6
					'ON'   => 'a.album_id = c.contest_album_id',
115
				),
116
			);
117
		}
118 6
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
119
120 6
		$result = $this->db->sql_query($sql);
121 6
		$row = $this->db->sql_fetchrow($result);
122 6
		$this->db->sql_freeresult($result);
123
124 6
		if (!$row)
125
		{
126
			throw new \phpbb\exception\http_exception(404, 'ALBUM_NOT_EXIST');
0 ignored issues
show
Bug introduced by
The type phpbb\exception\http_exception 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...
127
		}
128
129 6
		if ($extended_info && !isset($row['contest_id']))
130
		{
131 6
			$row['contest_id'] = 0;
132 6
			$row['contest_rates_start'] = 0;
133 6
			$row['contest_end'] = 0;
134 6
			$row['contest_marked'] = 0;
135 6
			$row['contest_first'] = 0;
136 6
			$row['contest_second'] = 0;
137 6
			$row['contest_third'] = 0;
138
		}
139
140 6
		return $row;
141
	}
142
143
	/**
144
	 * Check whether the album_user is the user who wants to do something
145
	 *
146
	 * @param      $album_id
147
	 * @param bool $user_id
148
	 * @return bool
149
	 */
150 3
	public function check_user($album_id, $user_id = false)
151
	{
152 3
		if ($user_id === false)
153
		{
154 2
			$user_id = (int) $this->user->data['user_id'];
155
		}
156
157
		$sql = 'SELECT album_id
158 3
			FROM ' . $this->albums_table . '
159 3
			WHERE album_id = ' . (int) $album_id . '
160 3
				AND album_user_id = ' . (int) $user_id;
161 3
		$result = $this->db->sql_query($sql);
162 3
		$row = $this->db->sql_fetchrow($result);
163 3
		$this->db->sql_freeresult($result);
164
165 3
		if ($row === false)
166
		{
167
			// return false;
168 2
			throw new \phpbb\exception\http_exception(403, 'NO_ALBUM_STEALING');
169
		}
170
171 1
		return true;
172
	}
173
174
	/**
175
	 * Generate gallery-albumbox
176
	 *
177
	 * @param bool        $ignore_personals     list personal albums
178
	 * @param string      $select_name          request_var() for the select-box
179
	 * @param bool|int    $select_id            selected album
180
	 * @param bool|string $requested_permission Exp: for moving a image you need i_upload permissions or a_moderate
181
	 * @param bool        $ignore_id
182
	 * @param int         $album_user_id        for the select-boxes of the ucp so you only can attach to your own
183
	 *                                          albums
184
	 * @param int         $requested_album_type only albums of the album_type are allowed
185
	 * @return string $gallery_albumbox        if ($select_name) {full select-box} else {list with options}
186
	 *                                          else {list with options}
187
	 *
188
	 * comparable to make_forum_select (includes/functions_admin.php)
189
	 * @internal param $ (string || array)    $ignore_id                disabled albums, Exp: on moving: the album
190
	 *           where the image is now
191
	 */
192
	public function get_albumbox($ignore_personals, $select_name, $select_id = false, $requested_permission = false, $ignore_id = false, $album_user_id = \phpbbgallery\core\block::PUBLIC_ALBUM, $requested_album_type = -1)
193
	{
194
		// Instead of the query we use the cache
195
		$album_data = $this->gallery_cache->get('albums');
196
197
		$right = $last_a_u_id = 0;
198
		$access_own = $access_personal = $requested_own = $requested_personal = false;
199
		$c_access_own = $c_access_personal = false;
200
		$padding_store = array('0' => '');
201
		$padding = $album_list = '';
202
		$check_album_type = ($requested_album_type >= 0) ? true : false;
203
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
204
205
		// Sometimes it could happen that albums will be displayed here not be displayed within the index page
206
		// This is the result of albums not displayed at index and a parent of a album with no permissions.
207
		// If this happens, the padding could be "broken", see includes/functions_admin.php > make_forum_select
208
209
		foreach ($album_data as $row)
210
		{
211
			$list = false;
212
			if ($row['album_user_id'] != $last_a_u_id)
213
			{
214
				if (!$last_a_u_id && $this->gallery_auth->acl_check('a_list', $this->gallery_auth->get_personal_album()) && !$ignore_personals)
215
				{
216
					$album_list .= '<option disabled="disabled" class="disabled-option">' . $this->language->lang('PERSONAL_ALBUMS') . '</option>';
217
				}
218
				$padding = '';
219
				$padding_store[$row['parent_id']] = '';
220
			}
221
			if ($row['left_id'] < $right)
222
			{
223
				$padding .= '&nbsp; &nbsp;';
224
				$padding_store[$row['parent_id']] = $padding;
225
			}
226
			else if ($row['left_id'] > $right + 1)
227
			{
228
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
229
			}
230
231
			$right = $row['right_id'];
232
			$last_a_u_id = $row['album_user_id'];
233
			$disabled = false;
234
235
			if (
236
				// Is in the ignore_id
237
				((is_array($ignore_id) && in_array($row['album_id'], $ignore_id)) || $row['album_id'] == $ignore_id)
238
				||
239
				// Need upload permissions (for moving)
240
				(($requested_permission == 'm_move') && (($row['album_type'] == (int) \phpbbgallery\core\block::TYPE_CAT) || (!$this->gallery_auth->acl_check('i_upload', $row['album_id'], $row['album_user_id']) && !$this->gallery_auth->acl_check('m_move', $row['album_id'], $row['album_user_id']))))
241
				||
242
				// album_type does not fit
243
				($check_album_type && ($row['album_type'] != $requested_album_type))
244
			)
245
			{
246
				$disabled = true;
247
			}
248
249
			if (($select_id == $this->gallery_auth->get_setting_permissions()) && !$row['album_user_id'])
250
			{
251
				$list = true;
252
			}
253
			else if (!$row['album_user_id'])
254
			{
255
				if ($this->gallery_auth->acl_check('a_list', $row['album_id'], $row['album_user_id']) || defined('IN_ADMIN'))
256
				{
257
					$list = true;
258
				}
259
			}
260
			else if (!$ignore_personals)
261
			{
262
				if ($row['album_user_id'] == $this->user->data['user_id'])
263
				{
264
					if (!$c_access_own)
265
					{
266
						$c_access_own = true;
267
						$access_own = $this->gallery_auth->acl_check('a_list', $this->gallery_auth->get_own_album());
268
						if ($requested_permission)
269
						{
270
							$requested_own = !$this->gallery_auth->acl_check($requested_permission, $this->gallery_auth->get_own_album());
0 ignored issues
show
Bug introduced by
It seems like $requested_permission can also be of type true; however, parameter $acl of phpbbgallery\core\auth\auth::acl_check() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

270
							$requested_own = !$this->gallery_auth->acl_check(/** @scrutinizer ignore-type */ $requested_permission, $this->gallery_auth->get_own_album());
Loading history...
271
						}
272
						else
273
						{
274
							$requested_own = false; // We need the negated version of true here
275
						}
276
					}
277
					$list = (!$list) ? $access_own : $list;
278
					$disabled = (!$disabled) ? $requested_own : $disabled;
279
				}
280
				else if ($row['album_user_id'])
281
				{
282
					if (!$c_access_personal)
283
					{
284
						$c_access_personal = true;
285
						$access_personal = $this->gallery_auth->acl_check('a_list', $this->gallery_auth->get_personal_album());
286
						if ($requested_permission)
287
						{
288
							$requested_personal = !$this->gallery_auth->acl_check($requested_permission, $this->gallery_auth->get_personal_album());
289
						}
290
						else
291
						{
292
							$requested_personal = false; // We need the negated version of true here
293
						}
294
					}
295
					$list = (!$list) ? $access_personal : $list;
296
					$disabled = (!$disabled) ? $requested_personal : $disabled;
297
				}
298
			}
299
			if (($album_user_id != (int) \phpbbgallery\core\block::PUBLIC_ALBUM) && ($album_user_id != $row['album_user_id']))
300
			{
301
				$list = false;
302
			}
303
			else if (($album_user_id != (int) \phpbbgallery\core\block::PUBLIC_ALBUM) && ($row['parent_id'] == 0))
304
			{
305
				$disabled = true;
306
			}
307
308
			if ($list)
309
			{
310
				$selected = (is_array($select_id)) ? ((in_array($row['album_id'], $select_id)) ? ' selected="selected"' : '') : (($row['album_id'] == $select_id) ? ' selected="selected"' : '');
311
				$album_list .= '<option value="' . $row['album_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['album_name'] . ' (ID: ' . $row['album_id'] . ')</option>';
312
			}
313
		}
314
		unset($padding_store);
315
316
		if ($select_name)
317
		{
318
			$gallery_albumbox = "<select name='$select_name' id='$select_name'>";
319
			$gallery_albumbox .= $album_list;
320
			$gallery_albumbox .= '</select>';
321
		}
322
		else
323
		{
324
			$gallery_albumbox = $album_list;
325
		}
326
327
		return $gallery_albumbox;
328
	}
329
330
	/**
331
	 * Update album information
332
	 * Resets the following columns with the correct value:
333
	 * - album_images, _real
334
	 * - album_last_image_id, _time, _name
335
	 * - album_last_username, _user_colour, _user_id
336
	 *
337
	 * @param $album_id
338
	 * @return mixed
339
	 */
340
	public function update_info($album_id)
341
	{
342
		$images_real = $images = $album_user_id = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $images_real is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $album_user_id is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $images is dead and can be removed.
Loading history...
343
344
		// Get the album_user_id, so we can keep the user_colour
345
		$sql = 'SELECT album_user_id
346
			FROM ' . $this->albums_table . '
347
			WHERE album_id = ' . (int) $album_id;
348
		$result = $this->db->sql_query($sql);
349
		$album_user_id = $this->db->sql_fetchfield('album_user_id');
350
		$this->db->sql_freeresult($result);
351
352
		// Number of not unapproved images
353
		$sql = 'SELECT COUNT(image_id) images
354
			FROM ' . $this->images_table . ' 
355
			WHERE image_status <> ' . (int) $this->block->get_image_status_unapproved() . '
356
				AND image_status <> ' . (int) $this->block->get_image_status_orphan() . '
357
				AND image_album_id = ' . (int) $album_id;
358
		$result = $this->db->sql_query($sql);
359
		$images = $this->db->sql_fetchfield('images');
360
		$this->db->sql_freeresult($result);
361
362
		// Number of total images
363
		$sql = 'SELECT COUNT(image_id) images_real
364
			FROM ' . $this->images_table . '
365
			WHERE image_status <> ' . (int) $this->block->get_image_status_orphan() . '
366
				AND image_album_id = ' . (int) $album_id;
367
		$result = $this->db->sql_query($sql);
368
		$images_real = $this->db->sql_fetchfield('images_real');
369
		$this->db->sql_freeresult($result);
370
371
		// Data of the last not unapproved image
372
		$sql = 'SELECT image_id, image_time, image_name, image_username, image_user_colour, image_user_id
373
			FROM ' . $this->images_table . '
374
			WHERE image_status <> ' . (int) $this->block->get_image_status_unapproved() . '
375
				AND image_status <> ' . (int) $this->block->get_image_status_orphan() . '
376
				AND image_album_id = ' . (int) $album_id . '
377
			ORDER BY image_time DESC';
378
		$result = $this->db->sql_query($sql);
379
		if ($row = $this->db->sql_fetchrow($result))
380
		{
381
			$sql_ary = array(
382
				'album_images_real'      => $images_real,
383
				'album_images'           => $images,
384
				'album_last_image_id'    => $row['image_id'],
385
				'album_last_image_time'  => $row['image_time'],
386
				'album_last_image_name'  => $row['image_name'],
387
				'album_last_username'    => $row['image_username'],
388
				'album_last_user_colour' => $row['image_user_colour'],
389
				'album_last_user_id'     => $row['image_user_id'],
390
			);
391
		}
392
		else
393
		{
394
			// No approved image, so we clear the columns
395
			$sql_ary = array(
396
				'album_images_real'      => $images_real,
397
				'album_images'           => $images,
398
				'album_last_image_id'    => 0,
399
				'album_last_image_time'  => 0,
400
				'album_last_image_name'  => '',
401
				'album_last_username'    => '',
402
				'album_last_user_colour' => '',
403
				'album_last_user_id'     => 0,
404
			);
405
			if ($album_user_id)
406
			{
407
				unset($sql_ary['album_last_user_colour']);
408
			}
409
		}
410
		$this->db->sql_freeresult($result);
411
412
		$sql = 'UPDATE ' . $this->albums_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
413
			WHERE album_id = ' . (int) $album_id;
414
		$this->db->sql_query($sql);
415
416
		return $row;
417
	}
418
419
	/**
420
	 * Generate personal album for user, when moving image into it
421
	 *
422
	 * @param $album_name
423
	 * @param $user_id
424
	 * @param $user_colour
425
	 * @param $gallery_user
426
	 * @return string
427
	 */
428
	public function generate_personal_album($album_name, $user_id, $user_colour, $gallery_user)
429
	{
430
		$album_data = array(
431
			'album_name'             => $this->db->sql_escape($album_name),
432
			'parent_id'              => 0,
433
			//left_id and right_id default by db
434
			'album_desc_options'     => 7,
435
			'album_desc'             => '',
436
			'album_parents'          => '',
437
			'album_type'             => (int) \phpbbgallery\core\block::TYPE_UPLOAD,
438
			'album_status'           => (int) \phpbbgallery\core\block::ALBUM_OPEN,
439
			'album_user_id'          => (int) $user_id,
440
			'album_last_username'    => '',
441
			'album_last_user_colour' => $user_colour,
442
		);
443
		$this->db->sql_query('INSERT INTO ' . $this->albums_table . ' ' . $this->db->sql_build_array('INSERT', $album_data));
444
		$personal_album_id = $this->db->sql_nextid();
445
446
		$gallery_user->update_data(array(
447
			'personal_album_id' => $personal_album_id,
448
		));
449
450
		// Fill album CPF.
451
		$cpf_vars = array(
452
			'pf_gallery_palbum' => (int) $personal_album_id,
453
		);
454
		$this->user_cpf->update_profile_field_data((int) $user_id, $cpf_vars);
455
456
		$this->gallery_config->inc('num_pegas', 1);
457
458
		// Update the config for the statistic on the index
459
		$this->gallery_config->set('newest_pega_user_id', $user_id);
460
		$this->gallery_config->set('newest_pega_username', $album_name);
461
		$this->gallery_config->set('newest_pega_user_colour', $user_colour);
462
		$this->gallery_config->set('newest_pega_album_id', $personal_album_id);
463
464
		$this->gallery_cache->destroy('_albums');
465
		$this->gallery_cache->destroy('sql', $this->albums_table);
466
467
		return $personal_album_id;
468
	}
469
470
	/**
471
	 * Create array of album IDs that are public
472
	 */
473 3
	public function get_public_albums()
474
	{
475
		$sql = 'SELECT album_id
476 3
				FROM ' . $this->albums_table . '
477 3
				WHERE album_user_id = ' . (int) \phpbbgallery\core\block::PUBLIC_ALBUM;
478 3
		$result = $this->db->sql_query($sql);
479 3
		$id_ary = array();
480 3
		while ($row = $this->db->sql_fetchrow($result))
481
		{
482 3
			$id_ary[] = (int) $row['album_id'];
483
		}
484 3
		$this->db->sql_freeresult($result);
485 3
		return $id_ary;
486
	}
487
}
488