Completed
Push — master ( 88f2dc...58f7d6 )
by René
26s queued 12s
created

PublicController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 31
rs 9.7998
cc 1
nc 1
nop 15

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\IRequest;
27
use OCP\IURLGenerator;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\TemplateResponse;
32
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
33
34
use OCA\Polls\Exceptions\NoUpdatesException;
35
use OCA\Polls\Db\Share;
36
use OCA\Polls\Db\Poll;
37
use OCA\Polls\Model\Acl;
38
use OCA\Polls\Service\CommentService;
39
use OCA\Polls\Service\MailService;
40
use OCA\Polls\Service\OptionService;
41
use OCA\Polls\Service\PollService;
42
use OCA\Polls\Service\ShareService;
43
use OCA\Polls\Service\SubscriptionService;
44
use OCA\Polls\Service\VoteService;
45
use OCA\Polls\Service\SystemService;
46
use OCA\Polls\Service\WatchService;
47
48
class PublicController extends Controller {
49
50
	/** @var IURLGenerator */
51
	private $urlGenerator;
52
53
	/** @var Acl */
54
	private $acl;
55
56
	/** @var CommentService */
57
	private $commentService;
58
59
	/** @var OptionService */
60
	private $optionService;
61
62
	/** @var MailService */
63
	private $mailService;
64
65
	/** @var PollService */
66
	private $pollService;
67
68
	/** @var Poll */
69
	private $poll;
70
71
	/** @var ShareService */
72
	private $shareService;
73
74
	/** @var Share */
75
	private $share;
76
77
	/** @var SubscriptionService */
78
	private $subscriptionService;
79
80
	/** @var SystemService */
81
	private $systemService;
82
83
	/** @var VoteService */
84
	private $voteService;
85
86
	/** @var WatchService */
87
	private $watchService;
88
89
	use ResponseHandle;
90
91
	public function __construct(
92
		string $appName,
93
		IRequest $request,
94
		IURLGenerator $urlGenerator,
95
		Acl $acl,
96
		CommentService $commentService,
97
		MailService $mailService,
98
		OptionService $optionService,
99
		PollService $pollService,
100
		Poll $poll,
101
		ShareService $shareService,
102
		Share $share,
103
		SubscriptionService $subscriptionService,
104
		SystemService $systemService,
105
		VoteService $voteService,
106
		WatchService $watchService
107
	) {
108
		parent::__construct($appName, $request);
109
		$this->urlGenerator = $urlGenerator;
110
		$this->acl = $acl;
111
		$this->commentService = $commentService;
112
		$this->mailService = $mailService;
113
		$this->optionService = $optionService;
114
		$this->pollService = $pollService;
115
		$this->poll = $poll;
116
		$this->shareService = $shareService;
117
		$this->share = $share;
118
		$this->subscriptionService = $subscriptionService;
119
		$this->systemService = $systemService;
120
		$this->voteService = $voteService;
121
		$this->watchService = $watchService;
122
	}
123
124
	/**
125
	 * @PublicPage
126
	 * @NoAdminRequired
127
	 * @NoCSRFRequired
128
	 * @return TemplateResponse|PublicTemplateResponse
129
	 */
130
	public function votePage() {
131
		if (\OC::$server->getUserSession()->isLoggedIn()) {
132
			return new TemplateResponse('polls', 'polls.tmpl', [
133
				'urlGenerator' => $this->urlGenerator]);
134
		} else {
135
			return new PublicTemplateResponse('polls', 'polls.tmpl', [
136
				'urlGenerator' => $this->urlGenerator]);
137
		}
138
	}
139
140
	/**
141
	 * get complete poll via token
142
	 * @PublicPage
143
	 * @NoAdminRequired
144
	 */
145
	public function getPoll(string $token): DataResponse {
146
		return $this->response(function () use ($token) {
147
			$this->share = $this->shareService->get($token, true);
148
			$this->acl->setShare($this->share);
149
			$this->poll = $this->pollService->get($this->share->getPollId());
150
			return [
151
				'acl' => $this->acl,
152
				'poll' => $this->poll,
153
			];
154
		});
155
	}
156
157
	/**
158
	 * Watch poll for updates
159
	 * @PublicPage
160
	 * @NoAdminRequired
161
	 */
162
	public function watchPoll(string $token, ?int $offset): DataResponse {
163
		$pollId = $this->acl->setToken($token)->getPollId();
164
165
		return $this->responseLong(function () use ($pollId, $offset) {
166
			$start = time();
167
			$timeout = 30;
168
			$offset = $offset ?? $start;
169
170
			while (empty($updates) && time() <= $start + $timeout) {
171
				sleep(1);
172
				$updates = $this->watchService->getUpdates($pollId, $offset);
173
			}
174
			if (empty($updates)) {
175
				throw new NoUpdatesException;
176
			}
177
			return ['updates' => $updates];
178
		});
179
	}
180
181
	/**
182
	 * Get share
183
	 * @PublicPage
184
	 * @NoAdminRequired
185
	 */
186
	public function getShare(string $token): DataResponse {
187
		return $this->response(function () use ($token) {
188
			return ['share' => $this->shareService->get($token, true)];
189
		});
190
	}
191
192
	/**
193
	 * Get Comments
194
	 * @NoAdminRequired
195
	 * @PublicPage
196
	 */
197
	public function getComments(string $token): DataResponse {
198
		return $this->response(function () use ($token) {
199
			return ['comments' => $this->commentService->list(null, $token)];
200
		});
201
	}
202
203
	/**
204
	 * Get votes
205
	 * @NoAdminRequired
206
	 * @PublicPage
207
	 */
208
	public function getVotes(string $token): DataResponse {
209
		return $this->response(function () use ($token) {
210
			return ['votes' => $this->voteService->list(null, $token)];
211
		});
212
	}
213
214
	/**
215
	 * Get options
216
	 * @NoAdminRequired
217
	 * @PublicPage
218
	 */
219
	public function getOptions(string $token): DataResponse {
220
		return $this->response(function () use ($token) {
221
			return ['options' => $this->optionService->list(null, $token)];
222
		});
223
	}
224
225
	/**
226
	 * Get subscription status
227
	 * @PublicPage
228
	 * @NoAdminRequired
229
	 */
230
	public function getSubscription(string $token): DataResponse {
231
		return $this->response(function () use ($token) {
232
			return ['subscribed' => $this->subscriptionService->get(0, $token)];
233
		});
234
	}
235
236
	/**
237
	 * Set Vote
238
	 * @PublicPage
239
	 * @NoAdminRequired
240
	 */
241
	public function setVote(int $optionId, string $setTo, string $token): DataResponse {
242
		return $this->response(function () use ($optionId, $setTo, $token) {
243
			return ['vote' => $this->voteService->set($optionId, $setTo, $token)];
244
		});
245
	}
246
247
	/**
248
	 * Write a new comment to the db and returns the new comment as array
249
	 * @NoAdminRequired
250
	 * @PublicPage
251
	 */
252
	public function addComment(string $token, string $message): DataResponse {
253
		return $this->response(function () use ($token, $message) {
254
			return ['comment' => $this->commentService->add(null, $token, $message)];
255
		});
256
	}
257
258
	/**
259
	 * Delete Comment
260
	 * @NoAdminRequired
261
	 * @PublicPage
262
	 */
263
	public function deleteComment(int $commentId, string $token): DataResponse {
264
		return $this->responseDeleteTolerant(function () use ($commentId, $token) {
265
			return ['comment' => $this->commentService->delete($commentId, $token)];
266
		});
267
	}
268
269
	/**
270
	 * subscribe
271
	 * @PublicPage
272
	 * @NoAdminRequired
273
	 */
274
	public function subscribe(string $token): DataResponse {
275
		return $this->response(function () use ($token) {
276
			return ['subscribed' => $this->subscriptionService->set(0, $token, true)];
277
		});
278
	}
279
280
	/**
281
	 * Unsubscribe
282
	 * @PublicPage
283
	 * @NoAdminRequired
284
	 */
285
	public function unsubscribe(string $token): DataResponse {
286
		return $this->response(function () use ($token) {
287
			return ['subscribed' => $this->subscriptionService->set(0, $token, false)];
288
		});
289
	}
290
291
	/**
292
	 * Validate it the user name is reservrd
293
	 * return false, if this username already exists as a user or as
294
	 * a participant of the poll
295
	 * @NoAdminRequired
296
	 * @PublicPage
297
	 */
298
	public function validatePublicUsername(string $userName, string $token): DataResponse {
299
		try {
300
			return new DataResponse(['result' => $this->systemService->validatePublicUsername($userName, $token), 'name' => $userName], Http::STATUS_OK);
301
		} catch (\Exception $e) {
302
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
303
		}
304
	}
305
306
	/**
307
	 * Validate email address (simple validation)
308
	 * @NoAdminRequired
309
	 * @PublicPage
310
	 */
311
	public function validateEmailAddress(string $emailAddress): DataResponse {
312
		try {
313
			return new DataResponse(['result' => $this->systemService->validateEmailAddress($emailAddress), 'emailAddress' => $emailAddress], Http::STATUS_OK);
314
		} catch (\Exception $e) {
315
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
316
		}
317
	}
318
319
	/**
320
	 * Create a personal share from a public share
321
	 * or update an email share with the username
322
	 * @NoAdminRequired
323
	 * @PublicPage
324
	 */
325
	public function register(string $token, string $userName, string $emailAddress = ''): DataResponse {
326
		return $this->responseCreate(function () use ($token, $userName, $emailAddress) {
327
			return ['share' => $this->shareService->personal($token, $userName, $emailAddress)];
328
		});
329
	}
330
331
	/**
332
	 * Sent invitation mails for a share
333
	 * Additionally send notification via notifications
334
	 * @NoAdminRequired
335
	 * @PublicPage
336
	 */
337
	public function resendInvitation(string $token): DataResponse {
338
		return $this->response(function () use ($token) {
339
			return ['share' => $this->mailService->resendInvitation($token)];
340
		});
341
	}
342
}
343