Passed
Pull Request — master (#1278)
by René
05:31 queued 01:45
created

PublicController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 29
rs 9.8333
cc 1
nc 1
nop 14

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