Passed
Pull Request — master (#1016)
by René
04:17
created

PollApiController::enum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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 Exception;
27
 use OCP\AppFramework\Db\DoesNotExistException;
28
 use OCA\Polls\Exceptions\EmptyTitleException;
29
 use OCA\Polls\Exceptions\InvalidAccessException;
30
 use OCA\Polls\Exceptions\InvalidShowResultsException;
31
 use OCA\Polls\Exceptions\InvalidPollTypeException;
32
 use OCA\Polls\Exceptions\NotAuthorizedException;
33
34
 use OCP\IRequest;
35
 use OCP\AppFramework\ApiController;
36
 use OCP\AppFramework\Http;
37
 use OCP\AppFramework\Http\DataResponse;
38
39
 use OCA\Polls\Service\PollService;
40
41
 class PollApiController extends ApiController {
42
43
44
 	 /** @var PollService */
45
 	private $pollService;
46
47
 	/**
48
 	 * PollApiController constructor
49
 	 * @param string $appName
50
 	 * @param IRequest $request
51
 	 * @param PollService $pollService
52
 	 */
53
54
 	public function __construct(
55
 		string $appName,
56
 		IRequest $request,
57
		PollService $pollService
58
 	) {
59
 		parent::__construct($appName, $request);
60
 		$this->pollService = $pollService;
61
 	}
62
63
64
	/**
65
	 * Get list of polls
66
	 * @NoAdminRequired
67
	 * @CORS
68
	 * @NoCSRFRequired
69
	 * @return DataResponse
70
	 */
71
72
	public function list() {
73
		try {
74
			return new DataResponse(['polls' => $this->pollService->list()], Http::STATUS_OK);
75
		} catch (DoesNotExistException $e) {
76
			return new DataResponse([], Http::STATUS_NOT_FOUND);
77
		} catch (NotAuthorizedException $e) {
78
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
79
		}
80
	}
81
82
83
	/**
84
	 * get poll configuration
85
	 * @NoAdminRequired
86
	 * @CORS
87
	 * @NoCSRFRequired
88
	 * @param int $pollId
89
	 * @return DataResponse
90
	 */
91
 	public function get($pollId) {
92
		try {
93
			return new DataResponse(['poll' => $this->pollService->get($pollId)], Http::STATUS_OK);
94
		} catch (DoesNotExistException $e) {
95
			return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND);
96
		} catch (NotAuthorizedException $e) {
97
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
98
		}
99
 	}
100
101
	/**
102
	 * Add poll
103
	 * @NoAdminRequired
104
	 * @NoCSRFRequired
105
	 * @CORS
106
	 * @param Array $poll
107
	 * @return DataResponse
108
	 */
109
110
	public function add($type, $title) {
111
		try {
112
			return new DataResponse(['poll' => $this->pollService->add($type, $title)], Http::STATUS_CREATED);
113
		} catch (NotAuthorizedException $e) {
114
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
115
		} catch (InvalidPollTypeException $e) {
116
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
117
		} catch (EmptyTitleException $e) {
118
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
119
		}
120
	}
121
122
	/**
123
	 * Update poll configuration
124
	 * @NoAdminRequired
125
	 * @CORS
126
	 * @NoCSRFRequired
127
	 * @param int $pollId
128
	 * @param array $poll
129
	 * @return DataResponse
130
	 */
131
132
	public function update($pollId, $poll) {
133
		try {
134
			return new DataResponse(['poll' => $this->pollService->update($pollId, $poll)], Http::STATUS_OK);
135
		} catch (DoesNotExistException $e) {
136
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
137
		} catch (NotAuthorizedException $e) {
138
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
139
		} catch (InvalidAccessException $e) {
140
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
141
		} catch (InvalidShowResultsException $e) {
142
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
143
		} catch (EmptyTitleException $e) {
144
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
145
		}
146
	}
147
148
	/**
149
	 * Switch deleted status (move to deleted polls)
150
	 * @NoAdminRequired
151
	 * @CORS
152
	 * @NoCSRFRequired
153
	 * @param int $pollId
154
	 * @return DataResponse
155
	 */
156
157
	public function trash($pollId) {
158
		try {
159
			return new DataResponse(['poll' => $this->pollService->delete($pollId)], Http::STATUS_OK);
160
		} catch (DoesNotExistException $e) {
161
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
162
		} catch (NotAuthorizedException $e) {
163
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
164
		}
165
	}
166
167
	/**
168
	 * Delete poll
169
	 * @NoAdminRequired
170
	 * @CORS
171
	 * @NoCSRFRequired
172
	 * @param int $pollId
173
	 * @return DataResponse
174
	 */
175
176
	public function delete($pollId) {
177
		try {
178
			return new DataResponse(['poll' => $this->pollService->deletePermanently($pollId)], Http::STATUS_OK);
179
		} catch (DoesNotExistException $e) {
180
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
181
		} catch (NotAuthorizedException $e) {
182
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
183
		}
184
185
	}
186
187
	/**
188
	 * Clone poll
189
	 * @NoAdminRequired
190
	 * @CORS
191
	 * @NoCSRFRequired
192
	 * @param int $pollId
193
	 * @return DataResponse
194
	 */
195
	public function clone($pollId) {
196
		try {
197
			return new DataResponse(['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED);
198
		} catch (DoesNotExistException $e) {
199
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
200
		} catch (NotAuthorizedException $e) {
201
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
202
		}
203
	}
204
205
	/**
206
	 * Get valid values for configuration options
207
	 * @NoAdminRequired
208
	 * @CORS
209
	 * @NoCSRFRequired
210
	 * @param Array $poll
211
	 * @return DataResponse
212
	 */
213
214
	public function enum() {
215
		return new DataResponse($this->pollService->getValidEnum(), Http::STATUS_OK);
216
	}
217
218
219
}
220