permissions::admin_permissions_for_item_post()   C
last analyzed

Complexity

Conditions 11
Paths 39

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 19
nc 39
nop 1
dl 0
loc 25
rs 5.2653
c 0
b 0
f 0
ccs 0
cts 20
cp 0
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package    CleverStyle Framework
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @license    0BSD
8
 */
9
namespace cs\modules\System\api\Controller\admin;
10
use
11
	cs\ExitException,
12
	cs\Group,
13
	cs\Permission,
14
	cs\User;
15
16
trait permissions {
17
	/**
18
	 * Get array of permissions data or data of specific permission if id specified
19
	 *
20
	 * @param \cs\Request $Request
21
	 *
22
	 * @return array
23
	 *
24
	 * @throws ExitException
25
	 */
26
	public static function admin_permissions___get ($Request) {
27
		$Permission = Permission::instance();
28
		if (isset($Request->route_ids[0])) {
29
			$result = $Permission->get($Request->route_ids[0]);
30
			if (!$result) {
31
				throw new ExitException(404);
32
			}
33
		} else {
34
			$result = $Permission->get_all();
35
		}
36
		return $result;
37
	}
38
	/**
39
	 * Add new permission
40
	 *
41
	 * @param \cs\Request  $Request
42
	 * @param \cs\Response $Response
43
	 *
44
	 * @throws ExitException
45
	 */
46
	public static function admin_permissions___post ($Request, $Response) {
47
		$data = $Request->data('group', 'label');
0 ignored issues
show
Bug introduced by
'group' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

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

47
		$data = $Request->data(/** @scrutinizer ignore-type */ 'group', 'label');
Loading history...
48
		if (!$data) {
49
			throw new ExitException(400);
50
		}
51
		if (Permission::instance()->add(...array_values($data))) {
0 ignored issues
show
Bug introduced by
The call to cs\Permission::add() has too few arguments starting with label. ( Ignorable by Annotation )

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

51
		if (Permission::instance()->/** @scrutinizer ignore-call */ add(...array_values($data))) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
			$Response->code = 201;
53
		} else {
54
			throw new ExitException(500);
55
		}
56
	}
57
	/**
58
	 * Update permission's data
59
	 *
60
	 * @param \cs\Request $Request
61
	 *
62
	 * @throws ExitException
63
	 */
64
	public static function admin_permissions___put ($Request) {
65
		$id   = $Request->route_ids(0);
66
		$data = $Request->data('group', 'label');
0 ignored issues
show
Bug introduced by
'group' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

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

66
		$data = $Request->data(/** @scrutinizer ignore-type */ 'group', 'label');
Loading history...
67
		if (!$id || !$data) {
68
			throw new ExitException(400);
69
		}
70
		if (!Permission::instance()->set($id, ...array_values($data))) {
0 ignored issues
show
Bug introduced by
The call to cs\Permission::set() has too few arguments starting with label. ( Ignorable by Annotation )

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

70
		if (!Permission::instance()->/** @scrutinizer ignore-call */ set($id, ...array_values($data))) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
71
			throw new ExitException(500);
72
		}
73
	}
74
	/**
75
	 * Delete permission
76
	 *
77
	 * @param \cs\Request $Request
78
	 *
79
	 * @throws ExitException
80
	 */
81
	public static function admin_permissions___delete ($Request) {
82
		if (!isset($Request->route_ids[0])) {
83
			throw new ExitException(400);
84
		}
85
		if (!Permission::instance()->del($Request->route_ids[0])) {
86
			throw new ExitException(500);
87
		}
88
	}
89
	/**
90
	 * Get permissions for specific item
91
	 *
92
	 * @param \cs\Request $Request
93
	 *
94
	 * @return array
95
	 *
96
	 * @throws ExitException
97
	 */
98
	public static function admin_permissions_for_item_get ($Request) {
99
		$data = $Request->query('group', 'label');
0 ignored issues
show
Bug introduced by
'group' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::query(). ( Ignorable by Annotation )

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

99
		$data = $Request->query(/** @scrutinizer ignore-type */ 'group', 'label');
Loading history...
100
		if (!$data) {
101
			throw new ExitException(400);
102
		}
103
		$User       = User::instance();
104
		$Permission = Permission::instance();
105
		$permission = $Permission->get(null, ...array_values($data));
0 ignored issues
show
Bug introduced by
array_values($data) is expanded, but the parameter $group of cs\Permission::get() does not expect variable arguments. ( Ignorable by Annotation )

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

105
		$permission = $Permission->get(null, /** @scrutinizer ignore-type */ ...array_values($data));
Loading history...
106
		$data       = [
107
			'groups' => [],
108
			'users'  => []
109
		];
110
		if ($permission) {
111
			$data['groups'] = array_column(
112
				$User->db()->qfa(
113
					"SELECT
0 ignored issues
show
Bug introduced by
'SELECT `id`, ... `permission` = '%s'' of type string is incompatible with the type string[] expected by parameter $query of cs\DB\_Abstract::qfa(). ( Ignorable by Annotation )

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

113
					/** @scrutinizer ignore-type */ "SELECT
Loading history...
114
						`id`,
115
						`value`
116
					FROM `[prefix]groups_permissions`
117
					WHERE
118
						`permission`	= '%s'",
119
					$permission[0]['id']
120
				) ?: [],
121
				'value',
122
				'id'
123
			);
124
			$data['users']  = array_column(
125
				$User->db()->qfa(
126
					"SELECT
127
						`id`,
128
						`value`
129
					FROM `[prefix]users_permissions`
130
					WHERE
131
						`permission`	= '%s'",
132
					$permission[0]['id']
133
				) ?: [],
134
				'value',
135
				'id'
136
			);
137
		}
138
		return [
139
			'groups' => (object)$data['groups'],
140
			'users'  => (object)$data['users']
141
		];
142
	}
143
	/**
144
	 * Get permissions for specific item
145
	 *
146
	 * @param \cs\Request $Request
147
	 *
148
	 * @throws ExitException
149
	 */
150
	public static function admin_permissions_for_item_post ($Request) {
151
		$data = $Request->data('group', 'label');
0 ignored issues
show
Bug introduced by
'group' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

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

151
		$data = $Request->data(/** @scrutinizer ignore-type */ 'group', 'label');
Loading history...
152
		if (!$data) {
153
			throw new ExitException(400);
154
		}
155
		$Group      = Group::instance();
156
		$Permission = Permission::instance();
157
		$User       = User::instance();
158
		$permission = $Permission->get(null, ...array_values($data));
0 ignored issues
show
Bug introduced by
array_values($data) is expanded, but the parameter $group of cs\Permission::get() does not expect variable arguments. ( Ignorable by Annotation )

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

158
		$permission = $Permission->get(null, /** @scrutinizer ignore-type */ ...array_values($data));
Loading history...
159
		// We'll create permission if needed
160
		$permission = $permission
161
			? $permission[0]['id']
162
			: $Permission->add(...array_values($data));
0 ignored issues
show
Bug introduced by
The call to cs\Permission::add() has too few arguments starting with label. ( Ignorable by Annotation )

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

162
			: $Permission->/** @scrutinizer ignore-call */ add(...array_values($data));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
163
		if (!$permission) {
164
			throw new ExitException(500);
165
		}
166
		$result = true;
167
		foreach ($Request->data('groups') ?: [] as $group => $value) {
168
			$result = $result && $Group->set_permissions([$permission => $value], $group);
169
		}
170
		foreach ($Request->data('users') ?: [] as $user => $value) {
171
			$result = $result && $User->set_permissions([$permission => $value], $user);
172
		}
173
		if (!$result) {
174
			throw new ExitException(500);
175
		}
176
	}
177
}
178