Completed
Push — master ( a8898a...5c0dbc )
by Nazar
04:15
created

Group   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 185
ccs 0
cts 105
cp 0
rs 10
wmc 19
lcom 1
cbo 9

10 Methods

Rating   Name   Duplication   Size   Complexity  
A cdb() 0 3 1
A construct() 0 3 1
A get() 0 18 4
A get_all() 0 8 1
A add() 0 13 2
A set() 0 12 2
B del() 0 40 5
A get_permissions() 0 3 1
A set_permissions() 0 3 1
A del_permissions_all() 0 3 1
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
/**
9
 * Provides next events:<br>
10
 *
11
 *  System/User/Group/add
12
 *  ['id' => <i>group_id</i>]
13
 *
14
 *  System/User/Group/del/before
15
 *  ['id' => <i>group_id</i>]
16
 *
17
 *  System/User/Group/del/after
18
 *  ['id' => <i>group_id</i>]
19
 *
20
 */
21
namespace cs;
22
use
23
	cs\Permission\Any;
24
25
/**
26
 * Class for groups manipulating
27
 *
28
 * @method static $this instance($check = false)
29
 */
30
class Group {
31
	use
32
		CRUD_helpers,
33
		Singleton,
34
		Any;
35
	protected $data_model = [
36
		'id'          => 'int:0',
37
		'title'       => 'html',
38
		'description' => 'html'
39
	];
40
	protected $table      = '[prefix]groups';
41
	/**
42
	 * @var Cache\Prefix
43
	 */
44
	protected $cache;
45
	/**
46
	 * Returns database index
47
	 *
48
	 * @return int
49
	 */
50
	protected function cdb () {
51
		return Config::instance()->module('System')->db('users');
52
	}
53
	protected function construct () {
54
		$this->cache = Cache::prefix('groups');
55
	}
56
	/**
57
	 * Get group data
58
	 *
59
	 * @param int|int[] $id
60
	 *
61
	 * @return array|array[]|false
62
	 */
63
	function get ($id) {
64
		if (is_array($id)) {
65
			foreach ($id as &$i) {
66
				$i = $this->get($i);
67
			}
68
			return $id;
69
		}
70
		$id = (int)$id;
71
		if (!$id) {
72
			return false;
73
		}
74
		return $this->cache->get(
75
			$id,
76
			function () use ($id) {
77
				return $this->read($id);
78
			}
79
		);
80
	}
81
	/**
82
	 * Get array of all groups
83
	 *
84
	 * @return int[]
85
	 */
86
	function get_all () {
87
		return $this->cache->get(
88
			'all',
89
			function () {
90
				return $this->search([], 1, PHP_INT_MAX, 'id', true);
91
			}
92
		);
93
	}
94
	/**
95
	 * Add new group
96
	 *
97
	 * @param string $title
98
	 * @param string $description
99
	 *
100
	 * @return false|int
1 ignored issue
show
Documentation introduced by
Should the return type not be integer|string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
101
	 */
102
	function add ($title, $description) {
103
		$id = $this->create($title, $description);
104
		if ($id) {
105
			unset($this->cache->all);
106
			Event::instance()->fire(
107
				'System/User/Group/add',
108
				[
109
					'id' => $id
110
				]
111
			);
112
		}
113
		return $id;
114
	}
115
	/**
116
	 * Set group data
117
	 *
118
	 * @param int    $id
119
	 * @param string $title
120
	 * @param string $description
121
	 *
122
	 * @return bool
123
	 */
124
	function set ($id, $title, $description) {
125
		$id     = (int)$id;
126
		$result = $this->update($id, $title, $description);
127
		if ($result) {
128
			$Cache = $this->cache;
129
			unset(
130
				$Cache->$id,
131
				$Cache->all
132
			);
133
		}
134
		return (bool)$result;
135
	}
136
	/**
137
	 * Delete group
138
	 *
139
	 * @param int|int[] $id
140
	 *
141
	 * @return bool
142
	 */
143
	function del ($id) {
144
		if (is_array($id)) {
145
			foreach ($id as &$i) {
146
				$i = (int)$this->del($i);
147
			}
148
			return (bool)array_product($id);
149
		}
150
		$id = (int)$id;
151
		if (in_array($id, [User::ADMIN_GROUP_ID, User::USER_GROUP_ID])) {
152
			return false;
153
		}
154
		Event::instance()->fire(
155
			'System/User/Group/del/before',
156
			[
157
				'id' => $id
158
			]
159
		);
160
		$result = $this->db_prime()->q(
161
			[
162
				"DELETE FROM `[prefix]groups` WHERE `id` = $id",
163
				"DELETE FROM `[prefix]users_groups` WHERE `group` = $id"
164
			]
165
		);
166
		if ($result) {
167
			$this->del_permissions_all($id);
168
			$Cache = $this->cache;
169
			unset(
170
				Cache::instance()->{'users/groups'},
171
				$Cache->$id,
172
				$Cache->all
173
			);
174
			Event::instance()->fire(
175
				'System/User/Group/del/after',
176
				[
177
					'id' => $id
178
				]
179
			);
180
		}
181
		return (bool)$result;
182
	}
183
	/**
184
	 * Get group permissions
185
	 *
186
	 * @param int $group
187
	 *
188
	 * @return int[]|false
189
	 */
190
	function get_permissions ($group) {
191
		return $this->get_any_permissions($group, 'group');
192
	}
193
	/**
194
	 * Set group permissions
195
	 *
196
	 * @param array $data
197
	 * @param int   $group
198
	 *
199
	 * @return bool
200
	 */
201
	function set_permissions ($data, $group) {
202
		return $this->set_any_permissions($data, (int)$group, 'group');
203
	}
204
	/**
205
	 * Delete all permissions of specified group
206
	 *
207
	 * @param int $group
208
	 *
209
	 * @return bool
210
	 */
211
	function del_permissions_all ($group) {
212
		return $this->del_any_permissions_all((int)$group, 'group');
213
	}
214
}
215