Completed
Pull Request — master (#966)
by René
29:43 queued 25:49
created

PollService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 23
ccs 0
cts 23
cp 0
crap 2
rs 9.9332

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 $userid;
0 ignored issues
show
introduced by
The private property $userid is not used, and could be removed.
Loading history...
49
	private $pollMapper;
50
 	private $poll;
51
 	private $logService;
52
 	private $commentService;
53
 	private $optionService;
54
 	private $shareService;
55
 	private $voteService;
56
 	private $acl;
57
58
 	/**
59
 	 * PollController constructor.
60
 	 * @param string $appName
61
 	 * @param $userId
62
 	 * @param PollMapper $pollMapper
63
 	 * @param LogService $logService
64
 	 * @param CommentService $commentService
65
 	 * @param OptionService $optionService
66
 	 * @param ShareService $shareService
67
 	 * @param VoteService $voteService
68
 	 * @param Acl $acl
69
 	 */
70
71
 	public function __construct(
72
 		string $appName,
0 ignored issues
show
Unused Code introduced by
The parameter $appName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

72
 		/** @scrutinizer ignore-unused */ string $appName,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
		$userId,
74
		ILogger $logger,
75
 		PollMapper $pollMapper,
76
 		Poll $poll,
77
 		LogService $logService,
78
		CommentService $commentService,
79
		OptionService $optionService,
80
		ShareService $shareService,
81
		VoteService $voteService,
82
 		Acl $acl
83
 	) {
84
		$this->userId = $userId;
0 ignored issues
show
Bug Best Practice introduced by
The property userId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
		$this->logger = $logger;
86
 		$this->pollMapper = $pollMapper;
87
 		$this->poll = $poll;
88
 		$this->logService = $logService;
89
 		$this->commentService = $commentService;
90
 		$this->optionService = $optionService;
91
 		$this->shareService = $shareService;
92
 		$this->voteService = $voteService;
93
 		$this->acl = $acl;
94
 	}
95
96
97
	/**
98
	 * list
99
	 * @NoAdminRequired
100
	 * @return DataResponse
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
	 */
102
103
	public function list() {
104
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
105
			throw new NotAuthorizedException;
106
		}
107
108
		$polls = $this->pollMapper->findAll();
109
		// TODO: Not the elegant way. Improvement neccessary
110
		foreach ($polls as $poll) {
111
			$combinedPoll = (object) array_merge(
112
				(array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId()))));
113
			if ($combinedPoll->allowView) {
114
				$pollList[] = $combinedPoll;
115
			}
116
		}
117
118
		return $pollList;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pollList does not seem to be defined for all execution paths leading up to this point.
Loading history...
119
	}
120
121
	/**
122
	 * get
123
	 * @NoAdminRequired
124
	 * @param integer $pollId
125
	 * @return array
126
	 */
127
 	public function get($pollId = 0, $token = '') {
128
		$this->poll = $this->pollMapper->find($pollId);
129
		if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) {
130
			throw new NotAuthorizedException;
131
		}
132
133
		return [
134
			'acl' => $this->acl,
135
			'poll' => $this->poll,
136
			'comments' => $this->commentService->list($pollId, $token),
137
			'options' => $this->optionService->list($pollId, $token),
138
			'shares' => $this->shareService->list($pollId, $token),
0 ignored issues
show
Unused Code introduced by
The call to OCA\Polls\Service\ShareService::list() has too many arguments starting with $token. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
			'shares' => $this->shareService->/** @scrutinizer ignore-call */ list($pollId, $token),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
139
			'votes' => $this->voteService->list($pollId, $token)
140
		];
141
142
 	}
143
144
	/**
145
	 * delete
146
	 * @NoAdminRequired
147
	 * @NoCSRFRequired
148
	 * @param integer $pollId
149
	 * @return Poll
150
	 */
151
152
	public function delete($pollId) {
153
		$this->poll = $this->pollMapper->find($pollId);
154
155
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
156
			throw new NotAuthorizedException;
157
		}
158
		if ($this->poll->getDeleted()) {
159
			$this->poll->setDeleted(0);
160
		} else {
161
			$this->poll->setDeleted(time());
162
		}
163
164
		$this->poll = $this->pollMapper->update($this->poll);
165
		$this->logService->setLog($this->poll->getId(), 'deletePoll');
166
		return $this->poll;
167
	}
168
169
	/**
170
	 * deletePermanently
171
	 * @NoAdminRequired
172
	 * @NoCSRFRequired
173
	 * @param integer $pollId
174
	 * @return Poll
175
	 */
176
177
	public function deletePermanently($pollId) {
178
		$this->poll = $this->pollMapper->find($pollId);
179
180
		if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) {
181
			throw new NotAuthorizedException;
182
		}
183
		return $this->pollMapper->delete($this->poll);
184
		// return $this->poll;
185
	}
186
187
	/**
188
	 * write
189
	 * @NoAdminRequired
190
	 * @NoCSRFRequired
191
	 * @param Array $poll
192
	 * @return DataResponse
193
	 */
194
195
	public function add($type, $title) {
196
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
197
			throw new NotAuthorizedException;
198
		}
199
		$this->logger->alert(json_encode($type));
200
		$this->logger->alert(json_encode($title));
201
202
		// Validate valuess
203
		if (!in_array($type, $this->getValidPollType())) {
204
			throw new InvalidPollTypeException('Invalid poll type');
205
		}
206
207
		if (!$title) {
208
			throw new EmptyTitleException('Title must not be empty');
209
		}
210
211
		$this->poll = new Poll();
212
		$this->poll->setType($type);
213
		$this->poll->setCreated(time());
214
		$this->poll->setOwner($this->userId);
215
		$this->poll->setTitle($title);
216
		$this->poll->setDescription('');
217
		$this->poll->setAccess('hidden');
218
		$this->poll->setExpire(0);
219
		$this->poll->setAnonymous(0);
220
		$this->poll->setFullAnonymous(0);
221
		$this->poll->setAllowMaybe(0);
222
		$this->poll->setVoteLimit(0);
223
		$this->poll->setSettings('');
224
		$this->poll->setOptions('');
225
		$this->poll->setShowResults('always');
226
		$this->poll->setDeleted(0);
227
		$this->poll->setAdminAccess(0);
228
		$this->poll = $this->pollMapper->insert($this->poll);
229
230
		$this->logService->setLog($this->poll->getId(), 'addPoll');
231
		$this->logger->alert(json_encode($this->poll));
232
233
		return $this->poll;
234
	}
235
236
	/**
237
	 * write
238
	 * @NoAdminRequired
239
	 * @NoCSRFRequired
240
	 * @depricated
241
	 * @param Array $poll
242
	 * @return DataResponse
243
	 */
244
245
	public function write($poll, $pollId = null) {
246
247
		if (!$pollId) {
248
			$pollId = $poll['id'];
249
		}
250
251
		// Validate valuess
252
		if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) {
253
			throw new InvalidShowResultsException('Invalid value for prop showResults');
254
		}
255
256
		if (isset($poll['access']) && !in_array($poll['access'], $this->getValidShowResults())) {
257
			throw new InvalidAccessException('Invalid value for prop access');
258
		}
259
260
		if (isset($poll['title']) && !$poll['title']) {
261
			throw new EmptyTitleException('Title must not be empty');
262
		}
263
264
		try {
265
			// find pollId
266
			$this->poll = $this->pollMapper->find($pollId);
267
			$this->logService->setLog($this->poll->getId(), 'updatePoll');
268
269
270
		} catch (DoesNotExistException $e) {
271
			// if not found create a new poll
272
273
			// Validate valuess
274
			if (!in_array($poll['type'], $this->getValidPollType())) {
275
				throw new InvalidPollTypeException('Invalid poll type');
276
			}
277
278
			if (!$poll['title']) {
279
				throw new EmptyTitleException('Title must not be empty');
280
			}
281
282
283
			$this->poll = new Poll();
284
			$this->poll->setType($poll['type']);
285
			$this->poll->setOwner($this->userId);
286
			$this->poll->setTitle($poll['title']);
287
			$this->poll->setCreated(time());
288
			$this->poll = $this->pollMapper->insert($this->poll);
289
290
			$this->logService->setLog($this->poll->getId(), 'addPoll');
291
		}
292
293
		if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) {
294
			throw new NotAuthorizedException;
295
		}
296
297
		$this->poll->setTitle(isset($poll['title']) ? $poll['title'] : $this->poll->getTitle());
298
		$this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription());
299
		$this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess());
300
		$this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire());
301
		$this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous());
302
		$this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe());
303
		$this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit());
304
		$this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults());
305
		$this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted());
306
		$this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess());
307
308
		$this->poll->setFullAnonymous(0);
309
		$this->poll->setVoteLimit(0);
310
		$this->poll->setSettings('');
311
		$this->poll->setOptions('');
312
313
		$this->pollMapper->update($this->poll);
314
315
		return $this->poll;
316
	}
317
318
	/**
319
	 * write
320
	 * @NoAdminRequired
321
	 * @NoCSRFRequired
322
	 * @param Array $poll
323
	 * @return DataResponse
324
	 */
325
326
	public function update($pollId, $poll) {
327
328
		$this->poll = $this->pollMapper->find($pollId);
329
330
		if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) {
331
			throw new NotAuthorizedException;
332
		}
333
334
		// Validate valuess
335
		if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) {
336
			throw new InvalidShowResultsException('Invalid value for prop showResults');
337
		}
338
339
		if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) {
340
			throw new InvalidAccessException('Invalid value for prop access '. $poll['access']);
341
		}
342
343
		if (isset($poll['title']) && !$poll['title']) {
344
			throw new EmptyTitleException('Title must not be empty');
345
		}
346
347
		$this->poll->setTitle($poll['title'] ? $poll['title'] : $this->poll->getTitle());
348
		$this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription());
349
		$this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess());
350
		$this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire());
351
		$this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous());
352
		$this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe());
353
		$this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit());
354
		$this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults());
355
		$this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted());
356
		$this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess());
357
358
		$this->pollMapper->update($this->poll);
359
		$this->logService->setLog($this->poll->getId(), 'updatePoll');
360
361
		return $this->poll;
362
	}
363
364
	/**
365
	 * clone
366
	 * @NoAdminRequired
367
	 * @NoCSRFRequired
368
	 * @param integer $pollId
369
	 * @return DataResponse
370
	 */
371
	public function clone($pollId) {
372
		$this->poll = $this->pollMapper->find($pollId);
373
374
		$this->poll->setCreated(time());
375
		$this->poll->setOwner($this->userId);
376
		$this->poll->setTitle('Clone of ' . $this->poll->getTitle());
377
		$this->poll->setDeleted(0);
378
		$this->poll->setId(0);
379
380
		$this->poll = $this->pollMapper->insert($this->poll);
381
		$this->logService->setLog($clonePoll->getId(), 'addPoll');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $clonePoll seems to be never defined.
Loading history...
382
383
		$this->optionService->clone($pollId, $this->poll->getId());
384
385
		return $this->poll;
386
387
	}
388
389
	public function getValidEnum() {
390
		return [
391
			'pollType' => $this->getValidPollType(),
392
			'access' => $this->getValidAccess(),
393
			'showResults' => $this->getValidShowResults()
394
		];
395
	}
396
397
	private function getValidPollType() {
398
		return ['datePoll', 'textPoll'];
399
	}
400
401
	private function getValidAccess() {
402
		return ['hidden', 'public'];
403
	}
404
405
	private function getValidShowResults() {
406
		return ['always', 'expired', 'never'];
407
	}
408
}
409