Completed
Push — master ( df8ec4...96358d )
by Nazar
04:25
created

groups   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
lcom 0
cbo 3
dl 0
loc 103
rs 10
c 1
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A admin_groups___get() 0 19 4
A admin_groups___post() 0 10 3
A admin_groups___put() 0 8 3
A admin_groups___delete() 0 8 3
A admin_groups_permissions_get() 0 8 3
A admin_groups_permissions_put() 0 8 3
1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller\admin;
11
use
12
	cs\ExitException,
13
	cs\Group,
14
	cs\Page;
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 int[] $route_ids
20
	 *
21
	 * @throws ExitException
22
	 */
23
	static function admin_groups___get ($route_ids) {
24
		$Group = Group::instance();
25
		$Page  = Page::instance();
26
		if (isset($route_ids[0])) {
27
			$result = $Group->get($route_ids[0]);
28
		} elseif (isset($_GET['ids'])) {
29
			$result = $Group->get(
30
				explode(',', $_GET['ids'])
31
			);
32
		} else {
33
			$result = $Group->get(
34
				$Group->get_all()
35
			);
36
		}
37
		if (!$result) {
38
			throw new ExitException(404);
39
		}
40
		$Page->json($result);
41
	}
42
	/**
43
	 * Add new group
44
	 *
45
	 * @throws ExitException
46
	 */
47
	static function admin_groups___post () {
48
		if (!isset($_POST['title'], $_POST['description'])) {
49
			throw new ExitException(400);
50
		}
51
		if (Group::instance()->add($_POST['title'], $_POST['description'])) {
52
			status_code(201);
53
		} else {
54
			throw new ExitException(500);
55
		}
56
	}
57
	/**
58
	 * Update group's data
59
	 *
60
	 * @param int[] $route_ids
61
	 *
62
	 * @throws ExitException
63
	 */
64
	static function admin_groups___put ($route_ids) {
65
		if (!isset($route_ids[0], $_POST['title'], $_POST['description'])) {
66
			throw new ExitException(400);
67
		}
68
		if (!Group::instance()->set($route_ids[0], $_POST['title'], $_POST['description'])) {
69
			throw new ExitException(500);
70
		}
71
	}
72
	/**
73
	 * Delete group
74
	 *
75
	 * @param int[] $route_ids
76
	 *
77
	 * @throws ExitException
78
	 */
79
	static function admin_groups___delete ($route_ids) {
80
		if (!isset($route_ids[0])) {
81
			throw new ExitException(400);
82
		}
83
		if (!Group::instance()->del($route_ids[0])) {
84
			throw new ExitException(500);
85
		}
86
	}
87
	/**
88
	 * Get group's permissions
89
	 *
90
	 * @param int[] $route_ids
91
	 *
92
	 * @throws ExitException
93
	 */
94
	static function admin_groups_permissions_get ($route_ids) {
95
		if (!isset($route_ids[0])) {
96
			throw new ExitException(400);
97
		}
98
		Page::instance()->json(
99
			Group::instance()->get_permissions($route_ids[0]) ?: []
100
		);
101
	}
102
	/**
103
	 * Update group's permissions
104
	 *
105
	 * @param int[] $route_ids
106
	 *
107
	 * @throws ExitException
108
	 */
109
	static function admin_groups_permissions_put ($route_ids) {
110
		if (!isset($route_ids[0], $_POST['permissions'])) {
111
			throw new ExitException(400);
112
		}
113
		if (!Group::instance()->set_permissions($_POST['permissions'], $route_ids[0])) {
114
			throw new ExitException(500);
115
		}
116
	}
117
}
118