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

VoteService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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
29
use OCP\IGroupManager;
30
use OCP\ILogger;
31
32
33
use OCA\Polls\Db\Vote;
34
use OCA\Polls\Db\VoteMapper;
35
use OCA\Polls\Db\OptionMapper;
36
use OCA\Polls\Service\AnonymizeService;
37
use OCA\Polls\Service\LogService;
38
use OCA\Polls\Model\Acl;
39
40
class VoteService  {
41
42
	private $userId;
43
	private $logger;
44
	private $vote;
45
	private $voteMapper;
46
	private $optionMapper;
47
	private $groupManager;
48
	private $anonymizer;
49
	private $logService;
50
	private $acl;
51
52
	/**
53
	 * VoteController constructor.
54
	 * @param string $appName
55
	 * @param $userId
56
	 * @param ILogger $logger
57
	 * @param Vote $vote
58
	 * @param VoteMapper $voteMapper
59
	 * @param OptionMapper $optionMapper
60
	 * @param IGroupManager $groupManager
61
	 * @param AnonymizeService $anonymizer
62
	 * @param LogService $logService
63
	 * @param Acl $acl
64
	 */
65
	public function __construct(
66
		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

66
		/** @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...
67
		$userId,
68
		ILogger $logger,
69
		VoteMapper $voteMapper,
70
		OptionMapper $optionMapper,
71
		Vote $vote,
72
		IGroupManager $groupManager,
73
		AnonymizeService $anonymizer,
74
		LogService $logService,
75
		Acl $acl
76
	) {
77
		$this->userId = $userId;
78
		$this->vote = $vote;
79
		$this->voteMapper = $voteMapper;
80
		$this->optionMapper = $optionMapper;
81
		$this->logger = $logger;
82
		$this->groupManager = $groupManager;
83
		$this->anonymizer = $anonymizer;
84
		$this->logService = $logService;
85
		$this->acl = $acl;
86
	}
87
88
	/**
89
	 * Get all votes of given poll
90
	 * Read all votes of a poll based on the poll id and return list as array
91
	 * @NoAdminRequired
92
	 * @param integer $pollId
93
	 * @param string $token
94
	 * @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...
95
	 */
96
	public function list($pollId = 0, $token = '') {
97
		if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) {
98
			throw new NotAuthorizedException;
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\NotAuthorizedException 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...
99
		}
100
101
		if (!$this->acl->getAllowSeeResults()) {
102
			return $this->voteMapper->findByPollAndUser($pollId, $this->acl->getUserId());
103
		} elseif (!$this->acl->getAllowSeeUsernames()) {
104
			$this->anonymizer->set($pollId, $this->acl->getUserId());
105
			return $this->anonymizer->getVotes();
106
		} else {
107
			return $this->voteMapper->findByPoll($pollId);
108
		}
109
	}
110
111
	/**
112
	 * set
113
	 * @NoAdminRequired
114
	 * @param integer $pollId
115
	 * @param Array $option
116
	 * @param string $setTo
117
	 * @param string $token
118
	 * @return DataResponse
119
	 */
120
	public function set($pollId = 0, $pollOptionText, $setTo, $token = '') {
121
122
		if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowVote()) {
123
			throw new NotAuthorizedException;
124
		}
125
126
		$option = $this->optionMapper->findByPollAndText($pollId, $pollOptionText);
127
128
		try {
129
			$this->vote = $this->voteMapper->findSingleVote($pollId, $option->getPollOptionText(), $this->acl->getUserId());
130
			$this->vote->setVoteAnswer($setTo);
131
			$this->voteMapper->update($this->vote);
132
133
		} catch (DoesNotExistException $e) {
134
			// Vote does not exist, insert as new Vote
135
			$this->vote = new Vote();
136
137
			$this->vote->setPollId($pollId);
138
			$this->vote->setUserId($this->acl->getUserId());
139
			$this->vote->setVoteOptionText($option->getPollOptionText());
140
			$this->vote->setVoteOptionId($option->getId());
141
			$this->vote->setVoteAnswer($setTo);
142
			$this->voteMapper->insert($this->vote);
143
144
		} finally {
145
			$this->logService->setLog($this->vote->getPollId(), 'setVote', $this->vote->getUserId());
146
			return $this->vote;
147
		}
148
	}
149
150
	/**
151
	 * delete
152
	 * @NoAdminRequired
153
	 * @NoCSRFRequired
154
	 * @param integer $voteId
155
	 * @param string $userId
156
	 * @param integer $pollId
157
	 * @return DataResponse
158
	 */
159
	public function delete($pollId, $userId) {
160
161
		if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowEdit()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $token seems to be never defined.
Loading history...
162
			throw new NotAuthorizedException;
163
		}
164
165
		$votes = $this->voteMapper->deleteByPollAndUser($pollId, $userId);
0 ignored issues
show
Unused Code introduced by
The assignment to $votes is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $votes is correct as $this->voteMapper->delet...dUser($pollId, $userId) targeting OCA\Polls\Db\VoteMapper::deleteByPollAndUser() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
166
		$this->logger->alert('Deleted votes from ' . $userId . ' in poll ' . $pollId);
167
	}
168
169
}
170