Completed
Push — master ( 397c2e...5232e6 )
by
unknown
02:26
created

CirclesController::join()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 1
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\Service\MiscService;
32
use \OCA\Circles\Service\ConfigService;
33
use \OCA\Circles\Service\CirclesService;
34
use \OCA\Circles\Model\Member;
35
36
37
use OC\AppFramework\Http;
38
use OCP\AppFramework\Controller;
39
use OCP\AppFramework\Http\DataResponse;
40
use OCP\IL10N;
41
use OCP\IRequest;
42
43
class CirclesController extends Controller {
44
45
	/** @var string */
46
	private $userId;
47
	/** @var IL10N */
48
	private $l10n;
49
	/** @var ConfigService */
50
	private $configService;
51
	/** @var CirclesService */
52
	private $circlesService;
53
	/** @var MiscService */
54
	private $miscService;
55
56 View Code Duplication
	public function __construct(
1 ignored issue
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...
57
		$appName,
58
		IRequest $request,
59
		$userId,
60
		IL10N $l10n,
61
		ConfigService $configService,
62
		CirclesService $circlesService,
63
		MiscService $miscService
64
	) {
65
		parent::__construct($appName, $request);
66
67
		$this->userId = $userId;
68
		$this->l10n = $l10n;
69
		$this->configService = $configService;
70
		$this->circlesService = $circlesService;
71
		$this->miscService = $miscService;
72
	}
73
74
75
	/**
76
	 * @NoAdminRequired
77
	 * @NoSubAdminRequired
78
	 *
79
	 * @param $type
80
	 * @param string $name
81
	 *
82
	 * @return DataResponse
83
	 */
84
	public function create($type, $name) {
85
86
		$data = null;
87
		if (substr($name, 0, 1) === '_') {
88
			return
89
				new DataResponse(
90
					[
91
						'name'   => $name,
92
						'type'   => $type,
93
						'status' => 0,
94
						'error'  => "The name of your circle cannot start with this character"
95
					],
96
					Http::STATUS_NON_AUTHORATIVE_INFORMATION
97
				);
98
		}
99
100
101
		try {
102
			$data = $this->circlesService->createCircle($type, $name);
103
		} catch (\Exception $e) {
104
			return
105
				new DataResponse(
106
					[
107
						'name'   => $name,
108
						'type'   => $type,
109
						'status' => 0,
110
						'error'  => $e->getMessage()
111
					],
112
					Http::STATUS_NON_AUTHORATIVE_INFORMATION
113
				);
114
		}
115
116
		return new DataResponse(
117
			[
118
				'name'   => $name,
119
				'circle' => $data,
120
				'type'   => $type,
121
				'status' => 1
122
			], Http::STATUS_CREATED
123
		);
124
125
	}
126
127
128
	/**
129
	 * @NoAdminRequired
130
	 * @NoSubAdminRequired
131
	 *
132
	 * @param $type
133
	 * @param string $name
134
	 *
135
	 * @return DataResponse
136
	 */
137 View Code Duplication
	public function list($type, $name = '') {
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...
138
139
		try {
140
			$data = $this->circlesService->listCircles($type, $name, Member::LEVEL_NONE);
141
		} catch (CircleTypeDisabledException $e) {
142
			return
143
				new DataResponse(
144
					[
145
						'type'   => $type,
146
						'status' => 0,
147
						'error'  => $e->getMessage()
148
					],
149
					Http::STATUS_NON_AUTHORATIVE_INFORMATION
150
				);
151
		}
152
153
		return new DataResponse(
154
			[
155
				'type'   => $type,
156
				'data'   => $data,
157
				'status' => 1
158
			], Http::STATUS_CREATED
159
		);
160
	}
161
162
163
	/**
164
	 * @NoAdminRequired
165
	 * @NoSubAdminRequired
166
	 *
167
	 * @param $id
168
	 *
169
	 * @return DataResponse
170
	 * @internal param string $name
171
	 *
172
	 */
173 View Code Duplication
	public function details($id) {
1 ignored issue
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...
174
175
		try {
176
			$data = $this->circlesService->detailsCircle($id);
177
		} catch (CircleDoesNotExistException $e) {
178
			return
179
				new DataResponse(
180
					[
181
						'circle_id' => $id,
182
						'status'    => 0,
183
						'error'     => $e->getMessage()
184
					],
185
					Http::STATUS_NON_AUTHORATIVE_INFORMATION
186
				);
187
		}
188
189
		return new DataResponse(
190
			[
191
				'circle_id' => $id,
192
				'details'   => $data,
193
				'status'    => 1
194
			], Http::STATUS_CREATED
195
		);
196
	}
197
198
199
	/**
200
	 * @NoAdminRequired
201
	 * @NoSubAdminRequired
202
	 *
203
	 * @param $id
204
	 *
205
	 * @return DataResponse
206
	 * @internal param string $name
207
	 *
208
	 */
209 View Code Duplication
	public function join($id) {
1 ignored issue
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...
210
211
		try {
212
			$data = $this->circlesService->joinCircle($id);
213
		} catch (\Exception $e) {
214
			return
215
				new DataResponse(
216
					[
217
						'circle_id' => $id,
218
						'status'    => 0,
219
						'error'     => $e->getMessage()
220
					],
221
					Http::STATUS_NON_AUTHORATIVE_INFORMATION
222
				);
223
		}
224
225
		return new DataResponse(
226
			[
227
				'circle_id' => $id,
228
				'member'    => $data,
229
				'status'    => 1
230
			], Http::STATUS_CREATED
231
		);
232
	}
233
234
235
	/**
236
	 * @NoAdminRequired
237
	 * @NoSubAdminRequired
238
	 *
239
	 * @param $id
240
	 *
241
	 * @return DataResponse
242
	 * @internal param string $name
243
	 *
244
	 */
245 View Code Duplication
	public function leave($id) {
1 ignored issue
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...
246
		try {
247
			$data = $this->circlesService->leaveCircle($id);
248
		} catch (\Exception $e) {
249
			return
250
				new DataResponse(
251
					[
252
						'circle_id' => $id,
253
						'status'    => 0,
254
						'error'     => $e->getMessage()
255
					],
256
					Http::STATUS_NON_AUTHORATIVE_INFORMATION
257
				);
258
		}
259
260
		return new DataResponse(
261
			[
262
				'circle_id' => $id,
263
				'member'    => $data,
264
				'status'    => 1
265
			], Http::STATUS_CREATED
266
		);
267
	}
268
269
270
271
272
273
274
275
276
	/**
277
	 * @NoAdminRequired
278
	 * @NoSubAdminRequired
279
	 *
280
	 * @param int $id
281
	 *
282
	 * @return DataResponse
283
	 */
284
//	public function delete($id) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
285
//		$affectedRows = $this->dbHandler->deleteTeam($id, $this->userId);
286
//
287
//		if ($affectedRows === 1) {
288
//			return new DataResponse(
289
//				[],
290
//				Http::STATUS_NO_CONTENT
291
//			);
292
//		}
293
//
294
//		return new DataResponse(
295
//			[
296
//				'message' => (string)$this->l10n->t('Unable to delete team.')
297
//			],
298
//			Http::STATUS_INTERNAL_SERVER_ERROR
299
//		);
300
//	}
301
302
303
}
304
305