Completed
Pull Request — master (#854)
by René
04:20
created

VoteController::delete()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 14
nop 3
dl 0
loc 21
ccs 0
cts 20
cp 0
crap 42
rs 9.0444
c 0
b 0
f 0
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\ILogger;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
use OCP\IGroupManager;
37
38
use OCA\Polls\Db\Poll;
39
use OCA\Polls\Db\PollMapper;
40
use OCA\Polls\Db\Vote;
41
use OCA\Polls\Db\VoteMapper;
42
use OCA\Polls\Db\Share;
43
use OCA\Polls\Db\ShareMapper;
44
use OCA\Polls\Service\AnonymizeService;
45
use OCA\Polls\Service\LogService;
46
use OCA\Polls\Model\Acl;
47
48
class VoteController extends Controller {
49
50
	private $userId;
51
	private $logger;
52
	private $mapper;
53
	private $groupManager;
54
	private $pollMapper;
55
	private $shareMapper;
56
	private $anonymizer;
57
	private $logService;
58
	private $acl;
59
60
	/**
61
	 * VoteController constructor.
62
	 * @param string $appName
63
	 * @param $userId
64
	 * @param IRequest $request
65
	 * @param ILogger $logger
66
	 * @param VoteMapper $mapper
67
	 * @param IGroupManager $groupManager
68
	 * @param PollMapper $pollMapper
69
	 * @param ShareMapper $shareMapper
70
	 * @param AnonymizeService $anonymizer
71
	 * @param LogService $logService
72
	 * @param Acl $acl
73
	 */
74
	public function __construct(
75
		string $appName,
76
		$UserId,
77
		IRequest $request,
78
		ILogger $logger,
79
		VoteMapper $mapper,
80
		IGroupManager $groupManager,
81
		PollMapper $pollMapper,
82
		ShareMapper $shareMapper,
83
		AnonymizeService $anonymizer,
84
		LogService $logService,
85
		Acl $acl
86
	) {
87
		parent::__construct($appName, $request);
88
		$this->userId = $UserId;
89
		$this->mapper = $mapper;
90
		$this->logger = $logger;
91
		$this->groupManager = $groupManager;
92
		$this->pollMapper = $pollMapper;
93
		$this->shareMapper = $shareMapper;
94
		$this->anonymizer = $anonymizer;
95
		$this->logService = $logService;
96
		$this->acl = $acl;
97
	}
98
99
	/**
100
	 * Get all votes of given poll
101
	 * Read all votes of a poll based on the poll id and return list as array
102
	 * @NoAdminRequired
103
	 * @param integer $pollId
104
	 * @return DataResponse
105
	 */
106
	public function get($pollId) {
107
108
		try {
109
110
			if (!$this->acl->getFoundByToken()) {
111
				$this->acl->setPollId($pollId);
112
			}
113
114
			if (!$this->acl->getAllowSeeUsernames()) {
115
				$this->anonymizer->set($pollId, $this->acl->getUserId());
116
				return new DataResponse((array) $this->anonymizer->getVotes(), Http::STATUS_OK);
117
			} else {
118
				return new DataResponse((array) $this->mapper->findByPoll($pollId), Http::STATUS_OK);
119
			}
120
121
		} catch (DoesNotExistException $e) {
122
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
123
		}
124
125
	}
126
127
	/**
128
	 * set
129
	 * @NoAdminRequired
130
	 * @param integer $pollId
131
	 * @param Array $option
132
	 * @param string $userId
133
	 * @param string $setTo
134
	 * @return DataResponse
135
	 */
136
	public function set($pollId, $option, $userId, $setTo) {
137
138
		$this->logger->alert('Deleting vote no. ' . $option);
0 ignored issues
show
Bug introduced by
Are you sure $option of type array can be used in concatenation? ( Ignorable by Annotation )

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

138
		$this->logger->alert('Deleting vote no. ' . /** @scrutinizer ignore-type */ $option);
Loading history...
139
		try {
140
			$vote = $this->mapper->findSingleVote($pollId, $option['pollOptionText'], $userId);
141
			$vote->setVoteAnswer($setTo);
142
			$this->mapper->update($vote);
143
144
		} catch (DoesNotExistException $e) {
145
			// Vote does not exist, insert as new Vote
146
			$vote = new Vote();
147
148
			$vote->setPollId($pollId);
149
			$vote->setUserId($userId);
150
			$vote->setVoteOptionText($option['pollOptionText']);
151
			$vote->setVoteOptionId($option['id']);
152
			$vote->setVoteAnswer($setTo);
153
154
			$this->mapper->insert($vote);
155
156
		} finally {
157
			$this->logService->setLog($vote->getPollId(), 'setVote', $vote->getUserId());
158
			return new DataResponse($vote, Http::STATUS_OK);
159
		}
160
	}
161
162
163
	/**
164
	 * delete
165
	 * @NoAdminRequired
166
	 * @param integer $voteId
167
	 * @param string $userId
168
	 * @param integer $pollId
169
	 * @return DataResponse
170
	 */
171
	public function delete($voteId = 0, $userId = '', $pollId = 0) {
172
		$this->logger->alert('Deleting vote no. ' . $voteId);
173
174
		try {
175
			if ($voteId) {
176
				$vote = $this->mapper->delete($voteId);
0 ignored issues
show
Unused Code introduced by
The assignment to $vote is dead and can be removed.
Loading history...
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

176
				$vote = $this->mapper->delete(/** @scrutinizer ignore-type */ $voteId);
Loading history...
177
				$this->logger->alert('Deleting vote no. ' . $voteId);
178
				return new DataResponse(null, Http::STATUS_OK);
179
			} elseif ($pollId && $userId) {
180
				$votes = $this->mapper->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->mapper->deleteByP...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...
181
				$this->logger->alert('Deleting votes from ' . $userId . ' in poll ' . $pollId);
182
				return new DataResponse(null, Http::STATUS_OK);
183
			} elseif ($pollId) {
184
				$votes = $this->mapper->deleteByPoll($pollId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $votes is correct as $this->mapper->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...
185
				$this->logger->alert('Deleting all votes in poll ' . $pollId);
186
				return new DataResponse(null, Http::STATUS_OK);
187
			} else {
188
				return DataResponse(null, Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
The function DataResponse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

188
				return /** @scrutinizer ignore-call */ DataResponse(null, Http::STATUS_NOT_FOUND);
Loading history...
189
			}
190
		} catch (DoesNotExistException $e) {
191
			return DataResponse(null, Http::STATUS_NOT_FOUND);
192
		}
193
	}
194
195
	/**
196
	 * Public functions
197
	 */
198
199
	/**
200
	 * setByToken
201
	 * @NoAdminRequired
202
	 * @PublicPage
203
	 * @NoCSRFRequired
204
	 * @param Array $option
205
	 * @param string $setTo
206
	 * @param string $token
207
	 * @return DataResponse
208
	 */
209
	public function setByToken($option, $setTo, $token) {
210
		try {
211
			$this->acl->setToken($token);
212
		} catch (DoesNotExistException $e) {
213
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
214
		}
215
216
		return $this->set($this->acl->getPollId(), $option, $this->acl->getUserId(), $setTo);
217
218
	}
219
220
	/**
221
	 * getByToken
222
	 * Read all votes of a poll based on a share token and return list as array
223
	 * @NoAdminRequired
224
	 * @PublicPage
225
	 * @NoCSRFRequired
226
	 * @param string $token
227
	 * @return DataResponse
228
	 */
229
	public function getByToken($token) {
230
231
		try {
232
			$this->acl->setToken($token);
233
		} catch (DoesNotExistException $e) {
234
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
235
		}
236
237
		return $this->get($this->acl->getPollId());
238
239
	}
240
241
}
242