groups::admin_groups___get()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
ccs 0
cts 12
cp 0
crap 20
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\Response;
14
15
trait groups {
16
	/**
17
	 * Get array of groups data or data of specific group if id specified or data of several specified groups if specified in ids query parameter
18
	 *
19
	 * @param \cs\Request $Request
20
	 *
21
	 * @return array|array[]
22
	 *
23
	 * @throws ExitException
24
	 */
25
	public static function admin_groups___get ($Request) {
26
		$Group = Group::instance();
27
		$id    = $Request->route_ids(0);
28
		$ids   = $Request->query('ids');
0 ignored issues
show
Bug introduced by
'ids' 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

28
		$ids   = $Request->query(/** @scrutinizer ignore-type */ 'ids');
Loading history...
29
		if ($id) {
30
			$result = $Group->get($id);
31
		} elseif ($ids) {
32
			$result = $Group->get(explode(',', $ids));
0 ignored issues
show
Bug introduced by
It seems like $ids can also be of type array and array; however, parameter $string of explode() 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

32
			$result = $Group->get(explode(',', /** @scrutinizer ignore-type */ $ids));
Loading history...
33
		} else {
34
			$result = $Group->get($Group->get_all());
35
		}
36
		if (!$result) {
37
			throw new ExitException(404);
38
		}
39
		return $result;
40
	}
41
	/**
42
	 * Add new group
43
	 *
44
	 * @param \cs\Request $Request
45
	 *
46
	 * @throws ExitException
47
	 */
48
	public static function admin_groups___post ($Request) {
49
		$data = $Request->data('title', 'description');
0 ignored issues
show
Bug introduced by
'title' 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

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

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

53
		if (Group::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...
54
			Response::instance()->code = 201;
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on cs\False_class.
Loading history...
55
		} else {
56
			throw new ExitException(500);
57
		}
58
	}
59
	/**
60
	 * Update group's data
61
	 *
62
	 * @param \cs\Request $Request
63
	 *
64
	 * @throws ExitException
65
	 */
66
	public static function admin_groups___put ($Request) {
67
		$id   = $Request->route_ids(0);
68
		$data = $Request->data('title', 'description');
0 ignored issues
show
Bug introduced by
'title' 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

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

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

72
		if (!Group::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...
73
			throw new ExitException(500);
74
		}
75
	}
76
	/**
77
	 * Delete group
78
	 *
79
	 * @param \cs\Request $Request
80
	 *
81
	 * @throws ExitException
82
	 */
83
	public static function admin_groups___delete ($Request) {
84
		$id = $Request->route_ids(0);
85
		if (!$id) {
86
			throw new ExitException(400);
87
		}
88
		if (!Group::instance()->del($id)) {
89
			throw new ExitException(500);
90
		}
91
	}
92
	/**
93
	 * Get group's permissions
94
	 *
95
	 * @param \cs\Request $Request
96
	 *
97
	 * @return array
98
	 *
99
	 * @throws ExitException
100
	 */
101
	public static function admin_groups_permissions_get ($Request) {
102
		$id = $Request->route_ids(0);
103
		if (!$id) {
104
			throw new ExitException(400);
105
		}
106
		return Group::instance()->get_permissions($id) ?: [];
107
	}
108
	/**
109
	 * Update group's permissions
110
	 *
111
	 * @param \cs\Request $Request
112
	 *
113
	 * @throws ExitException
114
	 */
115
	public static function admin_groups_permissions_put ($Request) {
116
		$id          = $Request->route_ids(0);
117
		$permissions = $Request->data('permissions');
0 ignored issues
show
Bug introduced by
'permissions' 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

117
		$permissions = $Request->data(/** @scrutinizer ignore-type */ 'permissions');
Loading history...
118
		if (!$id || !$permissions) {
119
			throw new ExitException(400);
120
		}
121
		if (!Group::instance()->set_permissions($permissions, $id)) {
122
			throw new ExitException(500);
123
		}
124
	}
125
}
126