Completed
Push — develop ( b18c7a...273208 )
by Daniel
11:54 queued 09:05
created

userlist   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 86
ccs 37
cts 37
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_user_id() 0 14 3
A update() 0 19 3
A _get_userlist() 0 5 1
A _get_next_user() 0 10 3
A _get_next_key() 0 7 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\users;
11
12
class userlist
13
{
14
	/**
15
	 * @param array $settings
16
	 * @param bool $change_user
17
	 * @return int
18
	 */
19 8
	public static function get_user_id(array &$settings, $change_user)
20
	{
21 8
		$current_user = $settings['current_user'];
22 8
		$userlist = self::_get_userlist($settings['userlist']);
23
24 8
		if ($change_user && sizeof($userlist))
25 8
		{
26 6
			$current_user = self::_get_next_user($settings['current_user'], $userlist);
27
28 6
			$settings['current_user'] = $current_user;
29 6
		}
30
31 8
		return (int) $current_user;
32
	}
33
34
	/**
35
	 * if we're selecting from a list and there is no result, we remove the culprit and update the list
36
	 *
37
	 * @param array $settings
38
	 */
39 4
	public static function update(array &$settings)
40
	{
41 4
		$userlist = self::_get_userlist($settings['userlist']);
42
43 4
		if ($settings['qtype'] === 'featured' && sizeof($userlist))
44 4
		{
45 3
			$curr_key = (int) array_search($settings['current_user'], $userlist);
46
47
			// Remove the invalid user from the list
48 3
			$new_list = str_replace($settings['current_user'] . ',', '', $settings['userlist'] . ',');
49 3
			$new_list = trim($new_list, ',');
50
51 3
			$new_userlist = self::_get_userlist($new_list);
52 3
			$current_user =& $new_userlist[$curr_key - 1];
53 3
			$settings['current_user'] = (int) $current_user;
54 3
			$settings['userlist'] = trim($new_list);
55 3
			$settings['last_changed'] = 0;
56 3
		}
57 4
	}
58
59
	/**
60
	 * @param string $list
61
	 * @return array
62
	 */
63 8
	private static function _get_userlist($list)
64
	{
65 8
		$userlist = preg_replace('/\s+/', '', $list);
66 8
		return array_map('intval', array_filter(explode(',', $userlist)));
67
	}
68
69
	/**
70
	 * @param $current_user
71
	 * @param $userlist
72
	 * @return int
73
	 */
74 6
	private static function _get_next_user($current_user, array $userlist)
75
	{
76 6
		$key = 0;
77 6
		if ($current_user && sizeof($userlist) > 1)
78 6
		{
79 3
			$key = self::_get_next_key($current_user, $userlist);
80 3
		}
81
82 6
		return $userlist[$key];
83
	}
84
85
	/**
86
	 * @param $current_user
87
	 * @param $userlist
88
	 * @return int
89
	 */
90 3
	private static function _get_next_key($current_user, array $userlist)
91
	{
92 3
		$end_key = sizeof($userlist) - 1;
93 3
		$curr_key = (int) array_search($current_user, $userlist);
94
95 3
		return ($curr_key >= 0 && $curr_key < $end_key) ? ($curr_key + 1) : 0;
96
	}
97
}
98