Passed
Pull Request — master (#966)
by René
04:27
created

PollService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 20
cp 0
rs 9.9666
cc 1
nc 1
nop 9
crap 2

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\Service;
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\ILogger;
35
36
 use OCA\Polls\Db\Poll;
37
 use OCA\Polls\Db\PollMapper;
38
 use OCA\Polls\Service\CommentService;
39
 use OCA\Polls\Service\OptionService;
40
 use OCA\Polls\Service\ShareService;
41
 use OCA\Polls\Service\VoteService;
42
 use OCA\Polls\Service\LogService;
43
 use OCA\Polls\Model\Acl;
44
45
 class PollService {
46
47
	private $logger;
48
	private $pollMapper;
49
 	private $poll;
50
 	private $logService;
51
 	private $commentService;
52
 	private $optionService;
53
 	private $shareService;
54
 	private $voteService;
55
 	private $acl;
56
57
 	/**
58
 	 * PollController constructor.
59
 	 * @param PollMapper $pollMapper
60
 	 * @param LogService $logService
61
 	 * @param CommentService $commentService
62
 	 * @param OptionService $optionService
63
 	 * @param ShareService $shareService
64
 	 * @param VoteService $voteService
65
 	 * @param Acl $acl
66
 	 */
67
68
 	public function __construct(
69
		ILogger $logger,
70
 		PollMapper $pollMapper,
71
 		Poll $poll,
72
 		LogService $logService,
73
		CommentService $commentService,
74
		OptionService $optionService,
75
		ShareService $shareService,
76
		VoteService $voteService,
77
 		Acl $acl
78
 	) {
79
		$this->logger = $logger;
80
 		$this->pollMapper = $pollMapper;
81
 		$this->poll = $poll;
82
 		$this->logService = $logService;
83
 		$this->commentService = $commentService;
84
 		$this->optionService = $optionService;
85
 		$this->shareService = $shareService;
86
 		$this->voteService = $voteService;
87
 		$this->acl = $acl;
88
 	}
89
90
91
	/**
92
	 * list
93
	 * @NoAdminRequired
94
	 * @return array
95
	 */
96
97
	public function list() {
98
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
99
			throw new NotAuthorizedException;
100
		}
101
102
		$pollList = [];
103
104
		$polls = $this->pollMapper->findAll();
105
		// TODO: Not the elegant way. Improvement neccessary
106
		foreach ($polls as $poll) {
107
			$combinedPoll = (object) array_merge(
108
				(array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId()))));
109
			if ($combinedPoll->allowView) {
110
				$pollList[] = $combinedPoll;
111
			}
112
		}
113
114
		return $pollList;
115
	}
116
117
	/**
118
	 * get
119
	 * @NoAdminRequired
120
	 * @param integer $pollId
121
	 * @return array
122
	 */
123
 	public function get($pollId = 0, $token = '') {
124
125
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) {
126
			throw new NotAuthorizedException;
127
		}
128
129
		$this->poll = $this->pollMapper->find($this->acl->getPollId());
130
131
		try {
132
			$comments = $this->commentService->list($this->poll->getId(), $token);
133
		} catch (Exception $e) {
134
			$comments = [];
135
		}
136
137
		try {
138
			$options = $this->optionService->list($this->poll->getId(), $token);
139
		} catch (Exception $e) {
140
			$options = [];
141
142
		}
143
144
		try {
145
			$votes = $this->voteService->list($this->poll->getId(), $token);
146
		} catch (Exception $e) {
147
			$votes = [];
148
		}
149
150
		try {
151
			$shares = $this->shareService->list($this->poll->getId());
152
		} catch (Exception $e) {
153
			$shares = [];
154
		}
155
156
		return [
157
			'acl' => $this->acl,
158
			'poll' => $this->poll,
159
			'comments' => $comments,
160
			'options' => $options,
161
			'shares' => $shares,
162
			'votes' => $votes
163
		];
164
 	}
165
166
	/**
167
	 * delete
168
	 * @NoAdminRequired
169
	 * @NoCSRFRequired
170
	 * @param integer $pollId
171
	 * @return Poll
172
	 */
173
174
	public function delete($pollId) {
175
		$this->poll = $this->pollMapper->find($pollId);
176
177
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
178
			throw new NotAuthorizedException;
179
		}
180
181
		if ($this->poll->getDeleted()) {
182
			$this->poll->setDeleted(0);
183
		} else {
184
			$this->poll->setDeleted(time());
185
		}
186
187
		$this->poll = $this->pollMapper->update($this->poll);
188
		$this->logService->setLog($this->poll->getId(), 'deletePoll');
189
190
		return $this->poll;
191
	}
192
193
	/**
194
	 * deletePermanently
195
	 * @NoAdminRequired
196
	 * @NoCSRFRequired
197
	 * @param integer $pollId
198
	 * @return Poll
199
	 */
200
201
	public function deletePermanently($pollId) {
202
		$this->poll = $this->pollMapper->find($pollId);
203
204
		if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) {
205
			throw new NotAuthorizedException;
206
		}
207
208
		return $this->pollMapper->delete($this->poll);
209
	}
210
211
	/**
212
	 * write
213
	 * @NoAdminRequired
214
	 * @NoCSRFRequired
215
	 * @param Array $poll
216
	 * @return Poll
217
	 */
218
219
	public function add($type, $title) {
220
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
221
			throw new NotAuthorizedException;
222
		}
223
224
		// Validate valuess
225
		if (!in_array($type, $this->getValidPollType())) {
226
			throw new InvalidPollTypeException('Invalid poll type');
227
		}
228
229
		if (!$title) {
230
			throw new EmptyTitleException('Title must not be empty');
231
		}
232
233
		$this->poll = new Poll();
234
		$this->poll->setType($type);
235
		$this->poll->setCreated(time());
236
		$this->poll->setOwner($this->userId);
0 ignored issues
show
Bug Best Practice introduced by
The property userId does not exist on OCA\Polls\Service\PollService. Did you maybe forget to declare it?
Loading history...
237
		$this->poll->setTitle($title);
238
		$this->poll->setDescription('');
239
		$this->poll->setAccess('hidden');
240
		$this->poll->setExpire(0);
241
		$this->poll->setAnonymous(0);
242
		$this->poll->setFullAnonymous(0);
243
		$this->poll->setAllowMaybe(0);
244
		$this->poll->setVoteLimit(0);
245
		$this->poll->setSettings('');
246
		$this->poll->setOptions('');
247
		$this->poll->setShowResults('always');
248
		$this->poll->setDeleted(0);
249
		$this->poll->setAdminAccess(0);
250
		$this->poll = $this->pollMapper->insert($this->poll);
251
252
		$this->logService->setLog($this->poll->getId(), 'addPoll');
253
254
		return $this->poll;
255
	}
256
257
	/**
258
	 * update
259
	 * @NoAdminRequired
260
	 * @NoCSRFRequired
261
	 * @param Array $poll
262
	 * @return Poll
263
	 */
264
265
	public function update($pollId, $poll) {
266
267
		$this->poll = $this->pollMapper->find($pollId);
268
269
		if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) {
270
			throw new NotAuthorizedException;
271
		}
272
273
		// Validate valuess
274
		if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) {
275
			throw new InvalidShowResultsException('Invalid value for prop showResults');
276
		}
277
278
		if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) {
279
			throw new InvalidAccessException('Invalid value for prop access '. $poll['access']);
280
		}
281
282
		if (isset($poll['title']) && !$poll['title']) {
283
			throw new EmptyTitleException('Title must not be empty');
284
		}
285
286
		$this->poll->setTitle($poll['title'] ? $poll['title'] : $this->poll->getTitle());
287
		$this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription());
288
		$this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess());
289
		$this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire());
290
		$this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous());
291
		$this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe());
292
		$this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit());
293
		$this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults());
294
		$this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted());
295
		$this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess());
296
297
		$this->pollMapper->update($this->poll);
298
		$this->logService->setLog($this->poll->getId(), 'updatePoll');
299
300
		return $this->poll;
301
	}
302
303
	/**
304
	 * clone
305
	 * @NoAdminRequired
306
	 * @NoCSRFRequired
307
	 * @param integer $pollId
308
	 * @return Poll
309
	 */
310
	public function clone($pollId) {
311
312
		if (!$this->acl->setPollId($this->poll->getId())->getAllowView()) {
313
			throw new NotAuthorizedException;
314
		}
315
316
		$this->poll = $this->pollMapper->find($pollId);
317
318
		$this->poll->setCreated(time());
319
		$this->poll->setOwner($this->userId);
0 ignored issues
show
Bug Best Practice introduced by
The property userId does not exist on OCA\Polls\Service\PollService. Did you maybe forget to declare it?
Loading history...
320
		$this->poll->setTitle('Clone of ' . $this->poll->getTitle());
321
		$this->poll->setDeleted(0);
322
		$this->poll->setId(0);
323
324
		$this->poll = $this->pollMapper->insert($this->poll);
325
		$this->logService->setLog($this->poll->getId(), 'addPoll');
326
327
		$this->optionService->clone($pollId, $this->poll->getId());
328
329
		return $this->poll;
330
331
	}
332
333
	public function getValidEnum() {
334
		return [
335
			'pollType' => $this->getValidPollType(),
336
			'access' => $this->getValidAccess(),
337
			'showResults' => $this->getValidShowResults()
338
		];
339
	}
340
341
	private function getValidPollType() {
342
		return ['datePoll', 'textPoll'];
343
	}
344
345
	private function getValidAccess() {
346
		return ['hidden', 'public'];
347
	}
348
349
	private function getValidShowResults() {
350
		return ['always', 'expired', 'never'];
351
	}
352
}
353