Completed
Pull Request — master (#120)
by Maxence
03:22
created

MembersController::searchGlobal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 9
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\Model\Member;
30
use OCA\Circles\Service\MiscService;
31
use OCP\AppFramework\Http\DataResponse;
32
33
class MembersController extends BaseController {
34
35
36
	/**
37
	 * @NoAdminRequired
38
	 * @NoSubAdminRequired
39
	 *
40
	 * @param string $uniqueId
41
	 * @param $ident
42
	 * @param $type
43
	 *
44
	 * @return DataResponse
45
	 */
46 View Code Duplication
	public function addMember($uniqueId, $ident, $type) {
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...
47
48
		try {
49
			$data = $this->membersService->addMember($uniqueId, $ident, (int)$type);
50
		} catch (\Exception $e) {
51
			return $this->fail(
52
				[
53
					'circle_id' => $uniqueId,
54
					'user_id'   => $ident,
55
					'user_type' => (int)$type,
56
					'display'   => MiscService::getDisplay($ident, (int)$type),
57
					'error'     => $e->getMessage()
58
				]
59
			);
60
		}
61
62
		return $this->success(
63
			[
64
				'circle_id' => $uniqueId,
65
				'user_id'   => $ident,
66
				'user_type' => (int)$type,
67
				'display'   => MiscService::getDisplay($ident, (int)$type),
68
				'members'   => $data
69
			]
70
		);
71
	}
72
73
74
	/**
75
	 * @NoAdminRequired
76
	 * @NoSubAdminRequired
77
	 *
78
	 * @param string $uniqueId
79
	 * @param string $member
80
	 * @param int $type
81
	 * @param int $level
82
	 *
83
	 * @return DataResponse
84
	 */
85
	public function levelMember($uniqueId, $member, $type, $level) {
86
87
		try {
88
			$data = $this->membersService->levelMember($uniqueId, $member, (int)$type, $level);
89
		} catch (\Exception $e) {
90
			return
91
				$this->fail(
92
					[
93
						'circle_id' => $uniqueId,
94
						'user_id'   => $member,
95
						'user_type' => (int)$type,
96
						'display'   => MiscService::getDisplay($member, (int)$type),
97
						'level'     => $level,
98
						'error'     => $e->getMessage()
99
					]
100
				);
101
		}
102
103
		return $this->success(
104
			[
105
				'circle_id' => $uniqueId,
106
				'user_id'   => $member,
107
				'user_type' => (int)$type,
108
				'display'   => MiscService::getDisplay($member, (int)$type),
109
				'level'     => $level,
110
				'members'   => $data,
111
			]
112
		);
113
	}
114
115
116
	/**
117
	 * @NoAdminRequired
118
	 * @NoSubAdminRequired
119
	 *
120
	 * @param string $uniqueId
121
	 * @param string $member
122
	 * @param int $type
123
	 *
124
	 * @return DataResponse
125
	 */
126 View Code Duplication
	public function removeMember($uniqueId, $member, $type) {
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...
127
128
		try {
129
			$data = $this->membersService->removeMember($uniqueId, $member, (int)$type);
130
		} catch (\Exception $e) {
131
			return
132
				$this->fail(
133
					[
134
						'circle_id' => $uniqueId,
135
						'user_id'   => $member,
136
						'user_type' => (int)$type,
137
						'display'   => MiscService::getDisplay($member, (int)$type),
138
						'error'     => $e->getMessage()
139
					]
140
				);
141
		}
142
143
		return $this->success(
144
			[
145
				'circle_id' => $uniqueId,
146
				'user_id'   => $member,
147
				'user_type' => (int)$type,
148
				'display'   => MiscService::getDisplay($member, (int)$type),
149
				'members'   => $data,
150
			]
151
		);
152
	}
153
154
155
	/**
156
	 * @NoAdminRequired
157
	 *
158
	 * @param string $search
159
	 *
160
	 * @return DataResponse
161
	 */
162
	public function searchGlobal($search) {
163
164
		try {
165
			$result = $this->searchService->searchGlobal($search);
166
		} catch (\Exception $e) {
167
			return
168
				$this->fail(
169
					[
170
						'search' => $search,
171
						'error'  => $e->getMessage()
172
					]
173
				);
174
		}
175
176
		return $this->success(['search' => $search, 'result' => $result]);
177
	}
178
179
}
180
181