Completed
Push — master ( fa4c5d...fa4c5d )
by Maxence
05:16 queued 02:40
created

CirclesController::settings()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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\CircleTypeDisabledException;
30
use OCP\AppFramework\Http\DataResponse;
31
32
class CirclesController extends BaseController {
33
34
	/**
35
	 * @NoAdminRequired
36
	 * @NoSubAdminRequired
37
	 *
38
	 * @param $type
39
	 * @param string $name
40
	 *
41
	 * @return DataResponse
42
	 */
43
	public function create($type, $name) {
44
45
		if (strlen($name) < 3) {
46
			$error = $this->l10n->t("The name of your circle must contain at least 3 characters");
47
		} elseif (substr($name, 0, 1) === '_') {
48
			$error = $this->l10n->t("The name of your circle cannot start with this character");
49
		} else {
50
51
			try {
52
				$data = $this->circlesService->createCircle($type, $name);
53
54
				return $this->success(['name' => $name, 'circle' => $data, 'type' => $type]);
55
			} catch (\Exception $e) {
56
				$error = $e->getMessage();
57
			}
58
		}
59
60
		return $this->fail(['type' => $type, 'name' => $name, 'error' => $error]);
61
	}
62
63
64
	/**
65
	 * @NoAdminRequired
66
	 * @NoSubAdminRequired
67
	 *
68
	 * @param $type
69
	 * @param string $name
70
	 * @param int $level
71
	 *
72
	 * @return DataResponse
73
	 */
74
	public function listing($type, $name = '', $level = 0) {
75
76
		try {
77
			$data = $this->circlesService->listCircles($type, $name, $level);
78
79
			return $this->success(['type' => $type, 'data' => $data]);
80
		} catch (CircleTypeDisabledException $e) {
81
82
			return $this->fail(['type' => $type, 'error' => $e->getMessage()]);
83
		}
84
	}
85
86
87
	/**
88
	 * @NoAdminRequired
89
	 * @NoSubAdminRequired
90
	 *
91
	 * @param $id
92
	 *
93
	 * @return DataResponse
94
	 * @internal param string $name
95
	 *
96
	 */
97 View Code Duplication
	public function details($id) {
98
		try {
99
			$data = $this->circlesService->detailsCircle($id);
100
101
			return $this->success(['circle_id' => $id, 'details' => $data]);
102
		} catch (\Exception $e) {
103
104
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
105
		}
106
107
	}
108
109
110
	/**
111
	 * @NoAdminRequired
112
	 * @NoSubAdminRequired
113
	 *
114
	 * @param $id
115
	 *
116
	 * @return DataResponse
117
	 * @internal param string $name
118
	 *
119
	 */
120 View Code Duplication
	public function settings($id, $settings) {
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...
121
		try {
122
			$data = $this->circlesService->settingsCircle($id, $settings);
123
124
			return $this->success(['circle_id' => $id, 'details' => $data]);
125
		} catch (\Exception $e) {
126
127
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
128
		}
129
130
	}
131
132
133
	/**
134
	 * @NoAdminRequired
135
	 * @NoSubAdminRequired
136
	 *
137
	 * @param $id
138
	 *
139
	 * @return DataResponse
140
	 * @internal param string $name
141
	 *
142
	 */
143 View Code Duplication
	public function join($id) {
144
		try {
145
			$data = $this->circlesService->joinCircle($id);
146
147
			return $this->success(['circle_id' => $id, 'member' => $data]);
148
		} catch (\Exception $e) {
149
150
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
151
		}
152
	}
153
154
155
	/**
156
	 * @NoAdminRequired
157
	 * @NoSubAdminRequired
158
	 *
159
	 * @param $id
160
	 *
161
	 * @return DataResponse
162
	 * @internal param string $name
163
	 *
164
	 */
165 View Code Duplication
	public function leave($id) {
166
		try {
167
			$data = $this->circlesService->leaveCircle($id);
168
169
			return $this->success(['circle_id' => $id, 'member' => $data]);
170
		} catch (\Exception $e) {
171
172
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
173
		}
174
175
	}
176
177
178
	/**
179
	 * @NoAdminRequired
180
	 * @NoSubAdminRequired
181
	 *
182
	 * @param $id
183
	 *
184
	 * @return DataResponse
185
	 * @internal param string $name
186
	 *
187
	 */
188 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...
189
		try {
190
			$this->circlesService->removeCircle($id);
191
192
			return $this->success(['circle_id' => $id]);
193
		} catch (\Exception $e) {
194
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
195
		}
196
	}
197
198
199
	/**
200
	 * link()
201
	 *
202
	 * Called from the UI to create a initiate the process of linking 2 [remote] circles.
203
	 * $remote format: <circle_name>@<remote_host>
204
	 *
205
	 * @NoAdminRequired
206
	 * @NoSubAdminRequired
207
	 *
208
	 * @param int $id
209
	 * @param string $remote
210
	 *
211
	 * @return DataResponse
212
	 */
213 View Code Duplication
	public function link($id, $remote) {
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...
214
		try {
215
			$link = $this->federatedService->linkCircle($id, $remote);
216
217
			return $this->success(['circle_id' => $id, 'remote' => $remote, 'link' => $link]);
218
		} catch (\Exception $e) {
219
			return $this->fail(
220
				['circle_id' => $id, 'remote' => $remote, 'error' => $e->getMessage()]
221
			);
222
		}
223
	}
224
225
}
226
227