Completed
Push — master ( 90f02e...53fcb4 )
by Maxence
02:59
created

CirclesController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 229
Duplicated Lines 26.64 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 5
dl 61
loc 229
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 19 4
A listing() 0 11 2
A details() 0 12 2
A settings() 11 11 2
A join() 10 10 2
A leave() 11 11 2
A destroy() 9 9 2
A link() 0 15 2
A linkStatus() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
	public function details($id) {
98
		try {
99
			$circle = $this->circlesService->detailsCircle($id);
100
			$circle->shortenUniqueId();
101
102
			return $this->success(['circle_id' => $id, 'details' => $circle]);
103
		} catch (\Exception $e) {
104
105
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
106
		}
107
108
	}
109
110
111
	/**
112
	 * @NoAdminRequired
113
	 * @NoSubAdminRequired
114
	 *
115
	 * @param $id
116
	 *
117
	 * @return DataResponse
118
	 * @internal param string $name
119
	 *
120
	 */
121 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...
122
		try {
123
			$circle = $this->circlesService->settingsCircle($id, $settings);
124
125
			return $this->success(['circle_id' => $id, 'details' => $circle]);
126
		} catch (\Exception $e) {
127
128
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
129
		}
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 join($id) {
145
		try {
146
			$data = $this->circlesService->joinCircle($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
	 * @NoAdminRequired
158
	 * @NoSubAdminRequired
159
	 *
160
	 * @param $id
161
	 *
162
	 * @return DataResponse
163
	 * @internal param string $name
164
	 *
165
	 */
166 View Code Duplication
	public function leave($id) {
167
		try {
168
			$data = $this->circlesService->leaveCircle($id);
169
170
			return $this->success(['circle_id' => $id, 'member' => $data]);
171
		} catch (\Exception $e) {
172
173
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
174
		}
175
176
	}
177
178
179
	/**
180
	 * @NoAdminRequired
181
	 * @NoSubAdminRequired
182
	 *
183
	 * @param $id
184
	 *
185
	 * @return DataResponse
186
	 * @internal param string $name
187
	 *
188
	 */
189 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...
190
		try {
191
			$this->circlesService->removeCircle($id);
192
193
			return $this->success(['circle_id' => $id]);
194
		} catch (\Exception $e) {
195
			return $this->fail(['circle_id' => $id, 'error' => $e->getMessage()]);
196
		}
197
	}
198
199
200
	/**
201
	 * link()
202
	 *
203
	 * Called from the UI to create a initiate the process of linking 2 [remote] circles.
204
	 * $remote format: <circle_name>@<remote_host>
205
	 *
206
	 * @NoAdminRequired
207
	 * @NoSubAdminRequired
208
	 *
209
	 * @param int $circleId
210
	 * @param string $remote
211
	 *
212
	 * @return DataResponse
213
	 */
214
	public function link($circleId, $remote) {
215
		try {
216
			$link = $this->federatedService->linkCircle($circleId, $remote);
217
			$links = $this->circlesService->detailsCircle($circleId)
218
										  ->getLinks();
219
220
			return $this->success(
221
				['circle_id' => $circleId, 'remote' => $remote, 'link' => $link, 'links' => $links]
222
			);
223
		} catch (\Exception $e) {
224
			return $this->fail(
225
				['circle_id' => $id, 'remote' => $remote, 'error' => $e->getMessage()]
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
226
			);
227
		}
228
	}
229
230
231
	/**
232
	 * linkStatus();
233
	 *
234
	 * Modify a link status. Used to confirm/dismiss a request or putting down a link.
235
	 * The function will modify local status and broadcast the status to the remote.
236
	 *
237
	 * Note: should be moved to a LinkController
238
	 *
239
	 * @NoAdminRequired
240
	 * @NoSubAdminRequired
241
	 *
242
	 * @param int $linkId
243
	 * @param int $status
244
	 *
245
	 * @return DataResponse
246
	 */
247
	public function linkStatus($linkId, $status) {
248
		try {
249
			$links = $this->federatedService->linkStatus($linkId, $status);
250
251
			return $this->success(['link_id' => $linkId, 'links' => $links]);
252
		} catch (\Exception $e) {
253
			return $this->fail(
254
				['link_id' => $linkId, 'error' => $e->getMessage()]
255
			);
256
		}
257
	}
258
259
260
}
261
262