Passed
Push — master ( 04720b...e4e141 )
by René
05:37 queued 16s
created

PublicController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
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(0, $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(0, $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(0, $token)];
222
		});
223
	}
224
225
	/**
226
	 * Add options
227
	 * @NoAdminRequired
228
	 * @PublicPage
229
	 */
230
	public function addOption(string $token, $timestamp = 0, $pollOptionText = '', $duration = 0): DataResponse {
231
		return $this->responseCreate(function () use ($token, $timestamp, $pollOptionText, $duration) {
232
			return ['option' => $this->optionService->add(0, $timestamp, $pollOptionText, $duration, $token)];
233
		});
234
	}
235
236
	/**
237
	 * Delete option
238
	 * @NoAdminRequired
239
	 * @PublicPage
240
	 */
241
	public function deleteOption(string $token, int $optionId): DataResponse {
242
		return $this->responseDeleteTolerant(function () use ($token, $optionId) {
243
			return ['option' => $this->optionService->delete($optionId, $token)];
244
		});
245
	}
246
247
	/**
248
	 * Get subscription status
249
	 * @PublicPage
250
	 * @NoAdminRequired
251
	 */
252
	public function getSubscription(string $token): DataResponse {
253
		return $this->response(function () use ($token) {
254
			return ['subscribed' => $this->subscriptionService->get(0, $token)];
255
		});
256
	}
257
258
	/**
259
	 * Set Vote
260
	 * @PublicPage
261
	 * @NoAdminRequired
262
	 */
263
	public function setVote(int $optionId, string $setTo, string $token): DataResponse {
264
		return $this->response(function () use ($optionId, $setTo, $token) {
265
			return ['vote' => $this->voteService->set($optionId, $setTo, $token)];
266
		});
267
	}
268
269
	/**
270
	 * Write a new comment to the db and returns the new comment as array
271
	 * @NoAdminRequired
272
	 * @PublicPage
273
	 */
274
	public function addComment(string $token, string $message): DataResponse {
275
		return $this->response(function () use ($token, $message) {
276
			return ['comment' => $this->commentService->add(0, $token, $message)];
277
		});
278
	}
279
280
	/**
281
	 * Delete Comment
282
	 * @NoAdminRequired
283
	 * @PublicPage
284
	 */
285
	public function deleteComment(int $commentId, string $token): DataResponse {
286
		return $this->responseDeleteTolerant(function () use ($commentId, $token) {
287
			return ['comment' => $this->commentService->delete($commentId, $token)];
288
		});
289
	}
290
291
	/**
292
	 * subscribe
293
	 * @PublicPage
294
	 * @NoAdminRequired
295
	 */
296
	public function subscribe(string $token): DataResponse {
297
		return $this->response(function () use ($token) {
298
			return ['subscribed' => $this->subscriptionService->set(0, $token, true)];
299
		});
300
	}
301
302
	/**
303
	 * Unsubscribe
304
	 * @PublicPage
305
	 * @NoAdminRequired
306
	 */
307
	public function unsubscribe(string $token): DataResponse {
308
		return $this->response(function () use ($token) {
309
			return ['subscribed' => $this->subscriptionService->set(0, $token, false)];
310
		});
311
	}
312
313
	/**
314
	 * Validate it the user name is reservrd
315
	 * return false, if this username already exists as a user or as
316
	 * a participant of the poll
317
	 * @NoAdminRequired
318
	 * @PublicPage
319
	 */
320
	public function validatePublicUsername(string $userName, string $token): DataResponse {
321
		try {
322
			return new DataResponse(['result' => $this->systemService->validatePublicUsername($userName, $token), 'name' => $userName], Http::STATUS_OK);
323
		} catch (\Exception $e) {
324
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
325
		}
326
	}
327
328
	/**
329
	 * Validate email address (simple validation)
330
	 * @NoAdminRequired
331
	 * @PublicPage
332
	 */
333
	public function validateEmailAddress(string $emailAddress): DataResponse {
334
		try {
335
			return new DataResponse(['result' => $this->systemService->validateEmailAddress($emailAddress), 'emailAddress' => $emailAddress], Http::STATUS_OK);
336
		} catch (\Exception $e) {
337
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
338
		}
339
	}
340
341
	/**
342
	 * Set EmailAddress
343
	 * @PublicPage
344
	 * @NoAdminRequired
345
	 */
346
	public function setEmailAddress(string $token, string $emailAddress = ''): DataResponse {
347
		return $this->response(function () use ($token, $emailAddress) {
348
			return ['share' => $this->shareService->setEmailAddress($token, $emailAddress, true)];
349
		});
350
	}
351
352
	/**
353
	 * Set EmailAddress
354
	 * @PublicPage
355
	 * @NoAdminRequired
356
	 */
357
	public function deleteEmailAddress(string $token): DataResponse {
358
		return $this->response(function () use ($token) {
359
			return ['share' => $this->shareService->deleteEmailAddress($token)];
360
		});
361
	}
362
363
364
	/**
365
	 * Create a personal share from a public share
366
	 * or update an email share with the username
367
	 * @NoAdminRequired
368
	 * @PublicPage
369
	 */
370
	public function register(string $token, string $userName, string $emailAddress = ''): DataResponse {
371
		return $this->responseCreate(function () use ($token, $userName, $emailAddress) {
372
			return ['share' => $this->shareService->personal($token, $userName, $emailAddress)];
373
		});
374
	}
375
376
	/**
377
	 * Sent invitation mails for a share
378
	 * Additionally send notification via notifications
379
	 * @NoAdminRequired
380
	 * @PublicPage
381
	 */
382
	public function resendInvitation(string $token): DataResponse {
383
		return $this->response(function () use ($token) {
384
			return ['share' => $this->mailService->resendInvitation($token)];
385
		});
386
	}
387
}
388