Passed
Pull Request — master (#1254)
by René
03:50
created

PollController::list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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