Passed
Pull Request — master (#1038)
by René
04:02
created

PollApiController::deletePermanently()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
 namespace OCA\Polls\Controller;
25
26
 use OCP\AppFramework\Db\DoesNotExistException;
27
 use OCA\Polls\Exceptions\EmptyTitleException;
28
 use OCA\Polls\Exceptions\InvalidAccessException;
29
 use OCA\Polls\Exceptions\InvalidShowResultsException;
30
 use OCA\Polls\Exceptions\InvalidPollTypeException;
31
 use OCA\Polls\Exceptions\NotAuthorizedException;
32
33
 use OCP\IRequest;
34
 use OCP\AppFramework\ApiController;
35
 use OCP\AppFramework\Http;
36
 use OCP\AppFramework\Http\DataResponse;
37
38
 use OCA\Polls\Service\PollService;
39
40
 class PollApiController extends ApiController {
41
42
43
	 /** @var PollService */
44
 	private $pollService;
45
46
 	/**
47
 	 * PollApiController constructor
48
 	 * @param string $appName
49
 	 * @param IRequest $request
50
 	 * @param PollService $pollService
51
 	 */
52
53
 	public function __construct(
54
		string $appName,
55
		IRequest $request,
56
		PollService $pollService
57
	) {
58
 		parent::__construct($appName, $request);
59
 		$this->pollService = $pollService;
60
 	}
61
62
63
 	/**
64
 	 * Get list of polls
65
 	 * @NoAdminRequired
66
 	 * @CORS
67
 	 * @NoCSRFRequired
68
 	 * @return DataResponse
69
 	 */
70
71
 	public function list() {
72
 		try {
73
 			return new DataResponse(['polls' => $this->pollService->list()], Http::STATUS_OK);
74
 		} catch (DoesNotExistException $e) {
75
 			return new DataResponse([], Http::STATUS_NOT_FOUND);
76
 		} catch (NotAuthorizedException $e) {
77
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
78
 		}
79
 	}
80
81
82
 	/**
83
 	 * get poll configuration
84
 	 * @NoAdminRequired
85
 	 * @CORS
86
 	 * @NoCSRFRequired
87
 	 * @param int $pollId
88
 	 * @return DataResponse
89
 	 */
90
 	public function get($pollId) {
91
 		try {
92
 			return new DataResponse(['poll' => $this->pollService->get($pollId, '')], Http::STATUS_OK);
93
 		} catch (DoesNotExistException $e) {
94
 			return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND);
95
 		} catch (NotAuthorizedException $e) {
96
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
97
 		}
98
 	}
99
100
 	/**
101
 	 * Add poll
102
 	 * @NoAdminRequired
103
 	 * @NoCSRFRequired
104
 	 * @CORS
105
 	 * @param Array $poll
106
 	 * @return DataResponse
107
 	 */
108
109
 	public function add($type, $title) {
110
 		try {
111
 			return new DataResponse(['poll' => $this->pollService->add($type, $title)], Http::STATUS_CREATED);
112
 		} catch (NotAuthorizedException $e) {
113
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
114
 		} catch (InvalidPollTypeException $e) {
115
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
116
 		} catch (EmptyTitleException $e) {
117
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
118
 		}
119
 	}
120
121
 	/**
122
 	 * Update poll configuration
123
 	 * @NoAdminRequired
124
 	 * @CORS
125
 	 * @NoCSRFRequired
126
 	 * @param int $pollId
127
 	 * @param array $poll
128
 	 * @return DataResponse
129
 	 */
130
131
 	public function update($pollId, $poll) {
132
 		try {
133
 			return new DataResponse(['poll' => $this->pollService->update($pollId, $poll)], Http::STATUS_OK);
134
 		} catch (DoesNotExistException $e) {
135
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
136
 		} catch (NotAuthorizedException $e) {
137
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
138
 		} catch (InvalidAccessException $e) {
139
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
140
 		} catch (InvalidShowResultsException $e) {
141
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
142
 		} catch (EmptyTitleException $e) {
143
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
144
 		}
145
 	}
146
147
 	/**
148
 	 * Switch deleted status (move to deleted polls)
149
 	 * @NoAdminRequired
150
 	 * @CORS
151
 	 * @NoCSRFRequired
152
 	 * @param int $pollId
153
 	 * @return DataResponse
154
 	 */
155
156
 	public function trash($pollId) {
157
 		try {
158
 			return new DataResponse(['poll' => $this->pollService->delete($pollId)], Http::STATUS_OK);
159
 		} catch (DoesNotExistException $e) {
160
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
161
 		} catch (NotAuthorizedException $e) {
162
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
163
 		}
164
 	}
165
166
 	/**
167
 	 * Delete poll
168
 	 * @NoAdminRequired
169
 	 * @CORS
170
 	 * @NoCSRFRequired
171
 	 * @param int $pollId
172
 	 * @return DataResponse
173
 	 */
174
175
 	public function delete($pollId) {
176
 		try {
177
 			return new DataResponse(['poll' => $this->pollService->deletePermanently($pollId)], Http::STATUS_OK);
178
 		} catch (DoesNotExistException $e) {
179
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
180
 		} catch (NotAuthorizedException $e) {
181
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
182
 		}
183
 	}
184
185
 	/**
186
 	 * Clone poll
187
 	 * @NoAdminRequired
188
 	 * @CORS
189
 	 * @NoCSRFRequired
190
 	 * @param int $pollId
191
 	 * @return DataResponse
192
 	 */
193
 	public function clone($pollId) {
194
 		try {
195
 			return new DataResponse(['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED);
196
 		} catch (DoesNotExistException $e) {
197
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
198
 		} catch (NotAuthorizedException $e) {
199
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
200
 		}
201
 	}
202
203
 	/**
204
 	 * Collect email addresses from particitipants
205
 	 * @NoAdminRequired
206
 	 * @CORS
207
 	 * @NoCSRFRequired
208
 	 * @param Array $poll
209
 	 * @return DataResponse
210
 	 */
211
212
 	public function getParticipantsEmailAddresses($pollId) {
213
 		try {
214
 			return new DataResponse($this->pollService->getParticipantsEmailAddresses($pollId), Http::STATUS_OK);
215
 		} catch (DoesNotExistException $e) {
216
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
217
 		} catch (NotAuthorizedException $e) {
218
 			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
219
 		}
220
 	}
221
222
 	/**
223
 	 * Get valid values for configuration options
224
 	 * @NoAdminRequired
225
 	 * @CORS
226
 	 * @NoCSRFRequired
227
 	 * @param Array $poll
228
 	 * @return DataResponse
229
 	 */
230
231
 	public function enum() {
232
 		return new DataResponse($this->pollService->getValidEnum(), Http::STATUS_OK);
233
 	}
234
 }
235