Passed
Pull Request — master (#1272)
by René
03:46
created

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