Completed
Push — master ( d4ce93...77c8bc )
by Maxence
02:50
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 Exception;
30
use OCA\Circles\Exceptions\CircleNameFirstCharException;
31
use OCA\Circles\Exceptions\CircleNameTooShortException;
32
use OCA\Circles\Exceptions\CircleTypeDisabledException;
33
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException;
34
use OCP\AppFramework\Http\DataResponse;
35
36
class CirclesController extends BaseController {
37
38
	/**
39
	 * Create a circle.
40
	 *
41
	 * @NoAdminRequired
42
	 * @NoSubAdminRequired
43
	 *
44
	 * @param $type
45
	 * @param string $name
46
	 *
47
	 * @return DataResponse
48
	 */
49
	public function create($type, $name) {
50
51
		try {
52
			$this->verifyCreationName($name);
53
			$data = $this->circlesService->createCircle($type, $name);
54
55
			return $this->success(['name' => $name, 'circle' => $data, 'type' => $type]);
56
		} catch (Exception $e) {
57
			return $this->fail(['type' => $type, 'name' => $name, 'error' => $e->getMessage()]);
58
		}
59
60
	}
61
62
63
	/**
64
	 * @NoAdminRequired
65
	 * @NoSubAdminRequired
66
	 *
67
	 * @param $type
68
	 * @param string $name
69
	 * @param int $level
70
	 *
71
	 * @return DataResponse
72
	 */
73
	public function listing($type, $name = '', $level = 0) {
74
75
		try {
76
			$data = $this->circlesService->listCircles($type, $name, $level);
77
78
			return $this->success(['type' => $type, 'data' => $data]);
79
		} catch (CircleTypeDisabledException $e) {
80
81
			return $this->fail(['type' => $type, 'error' => $e->getMessage()]);
82
		}
83
	}
84
85
86
	/**
87
	 * @NoAdminRequired
88
	 * @NoSubAdminRequired
89
	 *
90
	 * @param string $uniqueId
91
	 *
92
	 * @return DataResponse
93
	 */
94 View Code Duplication
	public function details($uniqueId) {
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...
95
		try {
96
			$circle = $this->circlesService->detailsCircle($uniqueId);
97
98
			return $this->success(['circle_id' => $uniqueId, 'details' => $circle]);
99
		} catch (\Exception $e) {
100
101
			return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]);
102
		}
103
104
	}
105
106
107
	/**
108
	 * @NoAdminRequired
109
	 * @NoSubAdminRequired
110
	 *
111
	 * @param string $uniqueId
112
	 * @param array $settings
113
	 *
114
	 * @return DataResponse
115
	 */
116 View Code Duplication
	public function settings($uniqueId, $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...
117
		try {
118
			$this->verifyCreationName($settings['circle_name']);
119
			$circle = $this->circlesService->settingsCircle($uniqueId, $settings);
120
121
			return $this->success(['circle_id' => $uniqueId, 'details' => $circle]);
122
		} catch (\Exception $e) {
123
124
			return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]);
125
		}
126
127
	}
128
129
130
	/**
131
	 * @NoAdminRequired
132
	 * @NoSubAdminRequired
133
	 *
134
	 * @param string $uniqueId
135
	 *
136
	 * @return DataResponse
137
	 */
138 View Code Duplication
	public function join($uniqueId) {
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...
139
		try {
140
			$data = $this->circlesService->joinCircle($uniqueId);
141
142
			return $this->success(['circle_id' => $uniqueId, 'member' => $data]);
143
		} catch (\Exception $e) {
144
145
			return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]);
146
		}
147
	}
148
149
150
	/**
151
	 * @NoAdminRequired
152
	 * @NoSubAdminRequired
153
	 *
154
	 * @param string $uniqueId
155
	 *
156
	 * @return DataResponse
157
	 */
158 View Code Duplication
	public function leave($uniqueId) {
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...
159
		try {
160
			$data = $this->circlesService->leaveCircle($uniqueId);
161
162
			return $this->success(['circle_id' => $uniqueId, 'member' => $data]);
163
		} catch (\Exception $e) {
164
165
			return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]);
166
		}
167
168
	}
169
170
171
	/**
172
	 * @NoAdminRequired
173
	 * @NoSubAdminRequired
174
	 *
175
	 * @param string $uniqueId
176
	 *
177
	 * @return DataResponse
178
	 */
179
	public function destroy($uniqueId) {
180
		try {
181
			$this->circlesService->removeCircle($uniqueId);
182
183
			return $this->success(['circle_id' => $uniqueId]);
184
		} catch (\Exception $e) {
185
			return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]);
186
		}
187
	}
188
189
190
	/**
191
	 * verifyCreationName();
192
	 *
193
	 * Verify the name at the creation of a circle:
194
	 * Name must contain at least 3 chars.
195
	 * First char must be alpha-numeric.
196
	 *
197
	 * @param $name
198
	 *
199
	 * @throws CircleNameFirstCharException
200
	 * @throws CircleNameTooShortException
201
	 */
202
	private function verifyCreationName($name) {
203
		if (strlen($name) < 3) {
204
			throw new CircleNameTooShortException(
205
				$this->l10n->t('The name of your circle must contain at least 3 characters')
206
			);
207
		}
208
209
		$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
210
		if (strpos($chars, strtolower(substr($name, 0, 1))) === false) {
211
			throw new CircleNameFirstCharException(
212
				$this->l10n->t(
213
					"The name of your circle must start with an alpha-numerical character"
214
				)
215
			);
216
		}
217
	}
218
219
}
220
221