Completed
Pull Request — master (#966)
by René
04:37
created

PollController::list()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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