Completed
Pull Request — master (#1030)
by René
04:05
created

PollController::get()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 35
c 1
b 1
f 0
dl 0
loc 48
ccs 0
cts 42
cp 0
rs 8.1155
cc 8
nc 40
nop 2
crap 72
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\Controller;
36
use OCP\AppFramework\Http;
37
use OCP\AppFramework\Http\DataResponse;
38
39
use OCA\Polls\Service\PollService;
40
use OCA\Polls\Service\CommentService;
41
use OCA\Polls\Service\OptionService;
42
use OCA\Polls\Service\ShareService;
43
use OCA\Polls\Service\VoteService;
44
use OCA\Polls\Model\Acl;
45
46
class PollController extends Controller {
47
48
	/** @var PollService */
49
	private $pollService;
50
51
	/** @var CommentService */
52
	private $commentService;
53
54
	/** @var OptionService */
55
	private $optionService;
56
57
	/** @var ShareService */
58
	private $shareService;
59
60
	/** @var VoteService */
61
	private $voteService;
62
63
	/** @var Acl */
64
	private $acl;
65
66
 	/**
67
 	 * PollController constructor.
68
 	 * @param string $appName
69
 	 * @param IRequest $request
70
 	 * @param PollService $pollService
71
 	 * @param CommentService $commentService
72
 	 * @param OptionService $optionService
73
 	 * @param ShareService $shareService
74
 	 * @param VoteService $voteService
75
 	 * @param Acl $acl
76
 	 */
77
78
 	public function __construct(
79
		string $appName,
80
 		IRequest $request,
81
 		PollService $pollService,
82
		CommentService $commentService,
83
 		OptionService $optionService,
84
 		ShareService $shareService,
85
 		VoteService $voteService,
86
  		Acl $acl
87
	) {
88
 		parent::__construct($appName, $request);
89
		$this->pollService = $pollService;
90
		$this->commentService = $commentService;
91
  		$this->optionService = $optionService;
92
  		$this->shareService = $shareService;
93
  		$this->voteService = $voteService;
94
  		$this->acl = $acl;
95
	}
96
97
98
	/**
99
	 * Get list of polls
100
	 * @NoAdminRequired
101
	 * @return DataResponse
102
	 */
103
104
	public function list() {
105
		try {
106
			return new DataResponse($this->pollService->list(), Http::STATUS_OK);
107
		} catch (DoesNotExistException $e) {
108
			return new DataResponse([], Http::STATUS_NOT_FOUND);
109
		} catch (NotAuthorizedException $e) {
110
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
111
		}
112
	}
113
114
115
	/**
116
	 * get complete poll
117
	 * @NoAdminRequired
118
	 * @PublicPage
119
	 * @param int $pollId
120
	 * @param string $token
121
	 * @return DataResponse
122
	 */
123
 	public function get($pollId, $token) {
124
		try {
125
			if ($token) {
126
				$poll = $this->pollService->getByToken($token);
127
				$acl = $this->acl->setToken($token);
128
			} else {
129
				$poll = $this->pollService->get($pollId);
130
				$acl = $this->acl->setPollId($pollId);
131
			}
132
133
		} catch (DoesNotExistException $e) {
134
			return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND);
135
		} catch (NotAuthorizedException $e) {
136
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
137
		}
138
139
		try {
140
			$comments = $this->commentService->list($poll->getId(), $token);
141
		} catch (Exception $e) {
142
			$comments = [];
143
		}
144
145
		try {
146
			$options = $this->optionService->list($poll->getId(), $token);
147
		} catch (Exception $e) {
148
			$options = [];
149
		}
150
151
		try {
152
			$votes = $this->voteService->list($poll->getId(), $token);
153
		} catch (Exception $e) {
154
			$votes = [];
155
		}
156
157
		try {
158
			$shares = $this->shareService->list($poll->getId());
159
		} catch (Exception $e) {
160
			$shares = [];
161
		}
162
163
		return new DataResponse([
164
			'acl' => $acl,
165
			'poll' => $poll,
166
			'comments' => $comments,
167
			'options' => $options,
168
			'shares' => $shares,
169
			'votes' => $votes
170
		], Http::STATUS_OK);
171
 	}
172
173
	/**
174
	 * Add poll
175
	 * @NoAdminRequired
176
	 * @param string $type
177
	 * @param string $title
178
	 * @return DataResponse
179
	 */
180
181
	public function add($type, $title) {
182
		try {
183
			return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK);
184
		} catch (NotAuthorizedException $e) {
185
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
186
		} catch (InvalidPollTypeException $e) {
187
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
188
		} catch (EmptyTitleException $e) {
189
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
190
		}
191
	}
192
193
	/**
194
	 * Update poll configuration
195
	 * @NoAdminRequired
196
	 * @param int $pollId
197
	 * @param array $poll
198
	 * @return DataResponse
199
	 */
200
201
	public function update($pollId, $poll) {
202
		try {
203
			return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK);
204
		} catch (DoesNotExistException $e) {
205
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
206
		} catch (NotAuthorizedException $e) {
207
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
208
		} catch (InvalidAccessException $e) {
209
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
210
		} catch (InvalidShowResultsException $e) {
211
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
212
		} catch (EmptyTitleException $e) {
213
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
214
		}
215
	}
216
217
	/**
218
	 * Switch deleted status (move to deleted polls)
219
	 * @NoAdminRequired
220
	 * @param int $pollId
221
	 * @return DataResponse
222
	 */
223
224
	public function delete($pollId) {
225
		try {
226
			return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK);
227
		} catch (DoesNotExistException $e) {
228
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
229
		} catch (NotAuthorizedException $e) {
230
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
231
		}
232
	}
233
234
	/**
235
	 * Delete poll
236
	 * @NoAdminRequired
237
	 * @param Array $poll
238
	 * @return DataResponse
239
	 */
240
241
	public function deletePermanently($pollId) {
242
		try {
243
			return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK);
244
		} catch (DoesNotExistException $e) {
245
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
246
		} catch (NotAuthorizedException $e) {
247
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
248
		}
249
	}
250
251
	/**
252
	 * Clone poll
253
	 * @NoAdminRequired
254
	 * @param int $pollId
255
	 * @return DataResponse
256
	 */
257
	public function clone($pollId) {
258
		try {
259
			$poll = $this->pollService->clone($pollId);
260
			$this->optionService->clone($pollId, $poll->getId());
261
262
			return new DataResponse($poll, Http::STATUS_OK);
263
		} catch (DoesNotExistException $e) {
264
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
265
		} catch (NotAuthorizedException $e) {
266
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
267
		}
268
	}
269
270
	/**
271
	 * Collect email addresses from particitipants
272
	 * @NoAdminRequired
273
	 * @param Array $poll
274
	 * @return DataResponse
275
	 */
276
277
	public function getParticipantsEmailAddresses($pollId) {
278
		try {
279
			return new DataResponse($this->pollService->getParticipantsEmailAddresses($pollId), Http::STATUS_OK);
280
		} catch (DoesNotExistException $e) {
281
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
282
		} catch (NotAuthorizedException $e) {
283
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
284
		}
285
	}
286
287
}
288