Completed
Push — js-scrutinizing ( ccc639...ed62f5 )
by Maxence
02:15
created

CirclesController::list()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use OCA\Circles\Exceptions\CircleDoesNotExistException;
30
use OCA\Circles\Exceptions\CircleTypeDisabledException;
31
use OCA\Circles\Model\Circle;
32
use OCA\Circles\Model\Member;
33
use OCP\AppFramework\Http\DataResponse;
34
35
class CirclesController extends BaseController {
36
37
	/**
38
	 * @NoAdminRequired
39
	 * @NoSubAdminRequired
40
	 *
41
	 * @param $type
42
	 * @param string $name
43
	 *
44
	 * @return DataResponse
45
	 */
46
	public function create($type, $name) {
47
		$type = self::convertTypeStringToBitValue($type);
48
49
		if (substr($name, 0, 1) === '_') {
50
			$error = "The name of your circle cannot start with this character";
51
		} else {
52
53
			try {
54
				$data = $this->circlesService->createCircle($type, $name);
55
56
				return $this->success(['name' => $name, 'circle' => $data, 'type' => $type]);
57
			} catch (\Exception $e) {
58
				$error = $e->getMessage();
59
			}
60
		}
61
62
		return $this->fail(['type' => $type, 'name' => $name, 'error' => $error]);
63
	}
64
65
66
	/**
67
	 * @NoAdminRequired
68
	 * @NoSubAdminRequired
69
	 *
70
	 * @param $type
71
	 * @param string $name
72
	 *
73
	 * @return DataResponse
74
	 */
75
	public function listing($type, $name = '') {
76
		$type = self::convertTypeStringToBitValue($type);
77
78
		try {
79
			$data = $this->circlesService->listCircles($type, $name, Member::LEVEL_NONE);
80
81
			return $this->success(['type' => $type, 'data' => $data]);
82
		} catch (CircleTypeDisabledException $e) {
83
84
			return $this->fail(['type' => $type, 'error' => $e->getMessage()]);
85
		}
86
	}
87
88
89
	/**
90
	 * @NoAdminRequired
91
	 * @NoSubAdminRequired
92
	 *
93
	 * @param $id
94
	 *
95
	 * @return DataResponse
96
	 * @internal param string $name
97
	 *
98
	 */
99 View Code Duplication
	public function details($id) {
100
		try {
101
			$data = $this->circlesService->detailsCircle($id);
102
103
			return $this->success(['circle_id' => $id, 'details' => $data]);
104
		} catch (\Exception $e) {
105
106
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
107
		}
108
109
	}
110
111
112
	/**
113
	 * @NoAdminRequired
114
	 * @NoSubAdminRequired
115
	 *
116
	 * @param $id
117
	 *
118
	 * @return DataResponse
119
	 * @internal param string $name
120
	 *
121
	 */
122 View Code Duplication
	public function join($id) {
123
		try {
124
			$data = $this->circlesService->joinCircle($id);
125
126
			return $this->success(['circle_id' => $id, 'member' => $data]);
127
		} catch (\Exception $e) {
128
129
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
130
		}
131
	}
132
133
134
	/**
135
	 * @NoAdminRequired
136
	 * @NoSubAdminRequired
137
	 *
138
	 * @param $id
139
	 *
140
	 * @return DataResponse
141
	 * @internal param string $name
142
	 *
143
	 */
144 View Code Duplication
	public function leave($id) {
145
		try {
146
			$data = $this->circlesService->leaveCircle($id);
147
148
			return $this->success(['circle_id' => $id, 'member' => $data]);
149
		} catch (\Exception $e) {
150
151
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
152
		}
153
154
	}
155
156
157
	/**
158
	 * Convert a Type in String to its Bit Value
159
	 *
160
	 * @param $type
161
	 *
162
	 * @return int
163
	 */
164
	public static function convertTypeStringToBitValue($type) {
165
		if (strtolower($type) === 'personal') {
166
			return Circle::CIRCLES_PERSONAL;
167
		}
168
		if (strtolower($type) === 'hidden') {
169
			return Circle::CIRCLES_HIDDEN;
170
		}
171
		if (strtolower($type) === 'private') {
172
			return Circle::CIRCLES_PRIVATE;
173
		}
174
		if (strtolower($type) === 'public') {
175
			return Circle::CIRCLES_PUBLIC;
176
		}
177
		if (strtolower($type) === 'all') {
178
			return Circle::CIRCLES_ALL;
179
		}
180
	}
181
182
}
183
184