Passed
Pull Request — master (#948)
by René
04:03
created

VoteController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 8
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\Controller;
25
26
use Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
30
use OCP\IRequest;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Db\Vote;
36
use OCA\Polls\Db\VoteMapper;
37
use OCA\Polls\Service\AnonymizeService;
38
use OCA\Polls\Service\LogService;
39
use OCA\Polls\Model\Acl;
40
41
class VoteController extends Controller {
42
43
	private $userId;
44
	private $voteMapper;
45
	private $vote;
46
	private $anonymizer;
47
	private $logService;
48
	private $acl;
49
50
	/**
51
	 * VoteController constructor.
52
	 * @param string $appName
53
	 * @param $userId
54
	 * @param IRequest $request
55
	 * @param VoteMapper $voteMapper
56
	 * @param Vote $vote
57
	 * @param AnonymizeService $anonymizer
58
	 * @param LogService $logService
59
	 * @param Acl $acl
60
	 */
61
	public function __construct(
62
		string $appName,
63
		$UserId,
64
		IRequest $request,
65
		VoteMapper $voteMapper,
66
		Vote $vote,
67
		AnonymizeService $anonymizer,
68
		LogService $logService,
69
		Acl $acl
70
	) {
71
		parent::__construct($appName, $request);
72
		$this->userId = $UserId;
73
		$this->voteMapper = $voteMapper;
74
		$this->vote = $vote;
75
		$this->anonymizer = $anonymizer;
76
		$this->logService = $logService;
77
		$this->acl = $acl;
78
	}
79
80
	/**
81
	 * list
82
	 * Get all votes baased on $pollId
83
	 * @NoAdminRequired
84
	 * @NoCSRFRequired
85
	 * @PublicPage
86
	 * @param integer $pollId
87
	 * @param string $token
88
	 * @return DataResponse
89
	 */
90
	public function list($pollId, $token = '') {
91
92
		if (\OC::$server->getUserSession()->isLoggedIn()) {
93
			$this->acl->setPollId($pollId);
94
		} elseif (!$this->acl->setToken($token)->getTokenIsValid()) {
95
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
96
		}
97
98
		try {
99
100
			if (!$this->acl->getFoundByToken()) {
101
				$this->acl->setPollId($pollId);
102
			}
103
104
			if (!$this->acl->getAllowSeeResults()) {
105
				return new DataResponse((array) $this->voteMapper->findByPollAndUser($pollId, $this->acl->getUserId()), Http::STATUS_OK);
106
			} elseif (!$this->acl->getAllowSeeUsernames()) {
107
				$this->anonymizer->set($pollId, $this->acl->getUserId());
108
				return new DataResponse((array) $this->anonymizer->getVotes(), Http::STATUS_OK);
109
			} else {
110
				return new DataResponse((array) $this->voteMapper->findByPoll($pollId), Http::STATUS_OK);
111
			}
112
113
		} catch (DoesNotExistException $e) {
114
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
115
		}
116
117
	}
118
119
	/**
120
	 * set
121
	 * change vote
122
	 * @NoAdminRequired
123
	 * @NoCSRFRequired
124
	 * @PublicPage
125
	 * @param integer $pollId - id of poll
126
	 * @param Array $option - the option to vote on
127
	 * @param string $setTo - change to state
128
	 * @param string $token
129
	 * @return DataResponse
130
	 */
131
	public function set($pollId, $option, $setTo, $token = '') {
132
133
		if (\OC::$server->getUserSession()->isLoggedIn()) {
134
			$this->acl->setPollId($pollId);
135
		} elseif (!$this->acl->setToken($token)->getTokenIsValid()) {
136
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
137
		}
138
139
		try {
140
			$this->vote = $this->voteMapper->findSingleVote(
141
				$this->acl->getPollId(),
142
				$option['pollOptionText'],
143
				$this->acl->getUserId());
144
145
			$this->vote->setVoteAnswer($setTo);
146
			$this->voteMapper->update($this->vote);
147
148
		} catch (DoesNotExistException $e) {
149
			// Vote does not exist, insert as new Vote
150
			$this->vote = new Vote();
151
152
			$this->vote->setPollId($this->acl->getPollId());
153
			$this->vote->setUserId($this->acl->getUserId());
154
			$this->vote->setVoteOptionText($option['pollOptionText']);
155
			$this->vote->setVoteOptionId($option['id']);
156
			$this->vote->setVoteAnswer($setTo);
157
158
			$this->voteMapper->insert($this->vote);
159
160
		} finally {
161
			$this->logService->setLog($this->vote->getPollId(), 'setVote', $this->vote->getUserId());
162
			return new DataResponse($this->vote, Http::STATUS_OK);
163
		}
164
	}
165
166
167
	/**
168
	 * delete
169
	 * delete a vote or remove all votes of a poll or a user in a poll
170
	 * @NoAdminRequired
171
	 * @NoCSRFRequired
172
	 * @param integer $voteId
173
	 * @param string $userId
174
	 * @param integer $pollId
175
	 * @return DataResponse
176
	 */
177
	public function delete($voteId = 0, $userId = '', $pollId = 0) {
178
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
179
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
180
		}
181
182
		try {
183
			if ($voteId) {
184
				$this->vote = $this->voteMapper->find($voteId);
185
186
				if ($this->acl->setPollId($this->vote->getPollId())->getAllowEdit()) {
187
					$this->vote = $this->voteMapper->delete($voteId);
0 ignored issues
show
Bug introduced by
$voteId of type integer is incompatible with the type OCP\AppFramework\Db\Entity expected by parameter $entity of OCP\AppFramework\Db\QBMapper::delete(). ( Ignorable by Annotation )

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

187
					$this->vote = $this->voteMapper->delete(/** @scrutinizer ignore-type */ $voteId);
Loading history...
188
					return $this->list($this->vote->getPollId());
189
				} else {
190
					return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
191
				}
192
193
			} elseif ($pollId && $userId) {
194
				if ($this->acl->setPollId($pollId)->getAllowEdit()) {
195
					$this->votes = $this->voteMapper->deleteByPollAndUser($pollId, $userId);
0 ignored issues
show
Bug Best Practice introduced by
The property votes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
Are you sure the assignment to $this->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...
196
					return $this->list($pollId);
197
				} else {
198
					return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
199
				}
200
201
			} elseif ($pollId) {
202
				if ($this->acl->setPollId($pollId)->getAllowEdit()) {
203
					$this->vote = $this->voteMapper->deleteByPoll($pollId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->vote is correct as $this->voteMapper->deleteByPoll($pollId) targeting OCA\Polls\Db\VoteMapper::deleteByPoll() 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...
204
					return $this->list($pollId);
205
				} else {
206
					return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
207
				}
208
209
			} else {
210
				return new DataResponse($e, Http::STATUS_METHOD_NOT_ALLOWED);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e seems to be never defined.
Loading history...
211
			}
212
		} catch (DoesNotExistException $e) {
213
			return new DataResponse(null, Http::STATUS_NOT_FOUND);
214
		}
215
	}
216
217
}
218