Completed
Push — some-scrutinizing ( b2f84c...29b6ab )
by Maxence
23:41
created

CirclesController::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 11
nc 4
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
58
			} catch (\Exception $e) {
59
				$error = $e->getMessage();
60
			}
61
		}
62
63
		return $this->fail(['type' => $type, 'name' => $name, 'error' => $error]);
64
	}
65
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoSubAdminRequired
70
	 *
71
	 * @param $type
72
	 * @param string $name
73
	 *
74
	 * @return DataResponse
75
	 */
76
	public function list($type, $name = '') {
77
		$type = self::convertTypeStringToBitValue($type);
78
79
		try {
80
			$data = $this->circlesService->listCircles($type, $name, Member::LEVEL_NONE);
81
82
			return $this->success(['type' => $type, 'data' => $data]);
83
		} catch (CircleTypeDisabledException $e) {
84
85
			return $this->fail(['type' => $type, 'error' => $e->getMessage()]);
86
		}
87
	}
88
89
90
	/**
91
	 * @NoAdminRequired
92
	 * @NoSubAdminRequired
93
	 *
94
	 * @param $id
95
	 *
96
	 * @return DataResponse
97
	 * @internal param string $name
98
	 *
99
	 */
100 View Code Duplication
	public function details($id) {
101
		try {
102
			$data = $this->circlesService->detailsCircle($id);
103
104
			return $this->success(['circle_id' => $id, 'details' => $data]);
105
		} catch (\Exception $e) {
106
107
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
108
		}
109
110
	}
111
112
113
	/**
114
	 * @NoAdminRequired
115
	 * @NoSubAdminRequired
116
	 *
117
	 * @param $id
118
	 *
119
	 * @return DataResponse
120
	 * @internal param string $name
121
	 *
122
	 */
123 View Code Duplication
	public function join($id) {
124
		try {
125
			$data = $this->circlesService->joinCircle($id);
126
127
			return $this->success(['circle_id' => $id, 'member' => $data]);
128
		} catch (\Exception $e) {
129
130
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
131
		}
132
	}
133
134
135
	/**
136
	 * @NoAdminRequired
137
	 * @NoSubAdminRequired
138
	 *
139
	 * @param $id
140
	 *
141
	 * @return DataResponse
142
	 * @internal param string $name
143
	 *
144
	 */
145 View Code Duplication
	public function leave($id) {
146
		try {
147
			$data = $this->circlesService->leaveCircle($id);
148
149
			return $this->success(['circle_id' => $id, 'member' => $data]);
150
		} catch (\Exception $e) {
151
152
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
153
		}
154
155
	}
156
157
158
	/**
159
	 * Convert a Type in String to its Bit Value
160
	 *
161
	 * @param $type
162
	 *
163
	 * @return int
164
	 */
165
	public static function convertTypeStringToBitValue($type) {
166
		if (strtolower($type) === 'personal') {
167
			return Circle::CIRCLES_PERSONAL;
168
		}
169
		if (strtolower($type) === 'hidden') {
170
			return Circle::CIRCLES_HIDDEN;
171
		}
172
		if (strtolower($type) === 'private') {
173
			return Circle::CIRCLES_PRIVATE;
174
		}
175
		if (strtolower($type) === 'public') {
176
			return Circle::CIRCLES_PUBLIC;
177
		}
178
		if (strtolower($type) === 'all') {
179
			return Circle::CIRCLES_ALL;
180
		}
181
	}
182
183
}
184
185