Completed
Push — master ( 9da91a...c60c2b )
by Maxence
02:22
created

CirclesController::listing()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 3
nop 3
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\CircleTypeDisabledException;
30
use OCA\Circles\Model\Member;
31
use OCP\AppFramework\Http\DataResponse;
32
33
class CirclesController extends BaseController {
34
35
	/**
36
	 * @NoAdminRequired
37
	 * @NoSubAdminRequired
38
	 *
39
	 * @param $type
40
	 * @param string $name
41
	 *
42
	 * @return DataResponse
43
	 */
44
	public function create($type, $name) {
45
46
		if (substr($name, 0, 1) === '_') {
47
			$error = $this->l10n->t("The name of your circle cannot start with this character");
48
		} else {
49
50
			try {
51
				$data = $this->circlesService->createCircle($type, $name);
52
53
				return $this->success(['name' => $name, 'circle' => $data, 'type' => $type]);
54
			} catch (\Exception $e) {
55
				$error = $e->getMessage();
56
			}
57
		}
58
59
		return $this->fail(['type' => $type, 'name' => $name, 'error' => $error]);
60
	}
61
62
63
	/**
64
	 * @NoAdminRequired
65
	 * @NoSubAdminRequired
66
	 *
67
	 * @param $type
68
	 * @param string $name
69
	 *
70
	 * @return DataResponse
71
	 */
72
	public function listing($type, $name = '', $level = 0) {
73
74
		try {
75
			$data = $this->circlesService->listCircles($type, $name, $level);
76
77
			return $this->success(['type' => $type, 'data' => $data]);
78
		} catch (CircleTypeDisabledException $e) {
79
80
			return $this->fail(['type' => $type, 'error' => $e->getMessage()]);
81
		}
82
	}
83
84
85
	/**
86
	 * @NoAdminRequired
87
	 * @NoSubAdminRequired
88
	 *
89
	 * @param $id
90
	 *
91
	 * @return DataResponse
92
	 * @internal param string $name
93
	 *
94
	 */
95 View Code Duplication
	public function details($id) {
96
		try {
97
			$data = $this->circlesService->detailsCircle($id);
98
99
			return $this->success(['circle_id' => $id, 'details' => $data]);
100
		} catch (\Exception $e) {
101
102
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
103
		}
104
105
	}
106
107
108
	/**
109
	 * @NoAdminRequired
110
	 * @NoSubAdminRequired
111
	 *
112
	 * @param $id
113
	 *
114
	 * @return DataResponse
115
	 * @internal param string $name
116
	 *
117
	 */
118 View Code Duplication
	public function join($id) {
119
		try {
120
			$data = $this->circlesService->joinCircle($id);
121
122
			return $this->success(['circle_id' => $id, 'member' => $data]);
123
		} catch (\Exception $e) {
124
125
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
126
		}
127
	}
128
129
130
	/**
131
	 * @NoAdminRequired
132
	 * @NoSubAdminRequired
133
	 *
134
	 * @param $id
135
	 *
136
	 * @return DataResponse
137
	 * @internal param string $name
138
	 *
139
	 */
140 View Code Duplication
	public function leave($id) {
141
		try {
142
			$data = $this->circlesService->leaveCircle($id);
143
144
			return $this->success(['circle_id' => $id, 'member' => $data]);
145
		} catch (\Exception $e) {
146
147
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
148
		}
149
150
	}
151
152
153
	/**
154
	 * @NoAdminRequired
155
	 * @NoSubAdminRequired
156
	 *
157
	 * @param $id
158
	 *
159
	 * @return DataResponse
160
	 * @internal param string $name
161
	 *
162
	 */
163 View Code Duplication
	public function destroy($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
		try {
165
			$this->circlesService->removeCircle($id);
166
167
			return $this->success(['circle_id' => $id]);
168
		} catch (\Exception $e) {
169
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
170
		}
171
172
	}
173
174
175
}
176
177