Completed
Push — master ( 701c1d...a14c1c )
by Nazar
04:37
created

Group   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 122
ccs 36
cts 54
cp 0.6667
rs 10
c 1
b 0
f 0
wmc 21
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B add_groups() 0 11 6
B get_groups() 0 19 5
A del_groups() 0 11 4
B set_groups() 0 48 6
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\User;
9
use cs\User;
10
11
/**
12
 * Trait that contains all methods from <i>>cs\User</i> for working with user groups
13
 *
14
 * @property int              $id
15
 * @property \cs\Cache\Prefix $cache
16
 *
17
 * @method \cs\DB\_Abstract db()
18
 * @method \cs\DB\_Abstract db_prime()
19
 */
20
trait Group {
21
	/**
22
	 * Add user's groups
23
	 *
24
	 * @param int|int[] $group Group id
25
	 * @param false|int $user  If not specified - current user assumed
26
	 *
27
	 * @return bool
28
	 */
29
	function add_groups ($group, $user = false) {
30
		$user = (int)$user ?: $this->id;
31
		if (!$user || $user == User::GUEST_ID) {
32
			return false;
33
		}
34
		$groups = $this->get_groups($user) ?: [];
35
		foreach ((array)_int($group) as $g) {
36
			$groups[] = $g;
37
		};
38
		return $this->set_groups($groups, $user);
39
	}
40
	/**
41
	 * Get user's groups
42
	 *
43
	 * @param false|int $user If not specified - current user assumed
44
	 *
45
	 * @return false|int[]
46
	 */
47 4
	function get_groups ($user = false) {
48 4
		$user = (int)$user ?: $this->id;
49 4
		if (!$user || $user == User::GUEST_ID) {
50
			return false;
51
		}
52 4
		return $this->cache->get(
53 4
			"groups/$user",
54 4
			function () use ($user) {
55 4
				return _int(
56 4
					$this->db()->qfas(
57
						"SELECT `group`
58
						FROM `[prefix]users_groups`
59 4
						WHERE `id` = '$user'
60 4
						ORDER BY `priority` DESC"
61 4
					) ?: []
62
				);
63 4
			}
64
		);
65
	}
66
	/**
67
	 * Set user's groups
68
	 *
69
	 * @param int[]     $groups
70
	 * @param false|int $user
71
	 *
72
	 * @return bool
73
	 */
74 2
	function set_groups ($groups, $user = false) {
75 2
		$user = (int)$user ?: $this->id;
76 2
		if (!$user) {
77
			return false;
78
		}
79 2
		if (!$groups) {
80 2
			return (bool)$this->db_prime()->q(
81
				"DELETE FROM `[prefix]users_groups`
82
				WHERE
83 2
					`id`	='$user'"
84
			);
85
		}
86 2
		$groups          = _int($groups);
87 2
		$groups_imploded = implode(', ', $groups);
88 2
		$return          = $this->db_prime()->q(
89
			"DELETE FROM `[prefix]users_groups`
90
			WHERE
91 2
				`id`	= '$user' AND
92 2
				`group`	NOT IN ($groups_imploded)"
93
		);
94 2
		unset($groups_imploded);
95 2
		$insert_update = [];
96 2
		foreach ($groups as $priority => $group) {
97 2
			$insert_update[] = [$group, $priority];
98
		}
99
		$return =
100 2
			$return &&
101 2
			$this->db_prime()->insert(
102
				"REPLACE INTO `[prefix]users_groups`
103
					(
104
						`id`,
105
						`group`,
106
						`priority`
107
					) VALUES (
108 2
						'$user',
109
						'%d',
110
						'%d'
111 2
					)",
112
				$insert_update
113
			);
114 2
		unset($insert_update);
115 2
		$Cache = $this->cache;
116
		unset(
117 2
			$Cache->{"groups/$user"},
118 2
			$Cache->{"permissions/$user"}
119
		);
120 2
		return $return;
121
	}
122
	/**
123
	 * Delete user's groups
124
	 *
125
	 * @param int|int[] $group Group id
126
	 * @param false|int $user  If not specified - current user assumed
127
	 *
128
	 * @return bool
129
	 */
130
	function del_groups ($group, $user = false) {
131
		$user = (int)$user ?: $this->id;
132
		if (!$user || $user == User::GUEST_ID) {
133
			return false;
134
		}
135
		$groups = array_diff(
136
			$this->get_groups($user),
137
			(array)_int($group)
138
		);
139
		return $this->set_groups($groups, $user);
140
	}
141
}
142