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

VoteService::list()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 20
rs 9.9332
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\NotAuthorizedException;
29
30
use OCA\Polls\Db\Vote;
31
use OCA\Polls\Db\VoteMapper;
32
use OCA\Polls\Db\OptionMapper;
33
use OCA\Polls\Service\AnonymizeService;
34
use OCA\Polls\Service\LogService;
35
use OCA\Polls\Model\Acl;
36
37
class VoteService  {
38
39
	private $voteMapper;
40
	private $vote;
41
	private $optionMapper;
42
	private $anonymizer;
43
	private $logService;
44
	private $acl;
45
46
	/**
47
	 * VoteController constructor.
48
	 * @param VoteMapper $voteMapper
49
	 * @param Vote $vote
50
	 * @param OptionMapper $optionMapper
51
	 * @param AnonymizeService $anonymizer
52
	 * @param LogService $logService
53
	 * @param Acl $acl
54
	 */
55
	public function __construct(
56
		VoteMapper $voteMapper,
57
		Vote $vote,
58
		OptionMapper $optionMapper,
59
		AnonymizeService $anonymizer,
60
		LogService $logService,
61
		Acl $acl
62
	) {
63
		$this->voteMapper = $voteMapper;
64
		$this->vote = $vote;
65
		$this->optionMapper = $optionMapper;
66
		$this->anonymizer = $anonymizer;
67
		$this->logService = $logService;
68
		$this->acl = $acl;
69
	}
70
71
	/**
72
	 * Get all votes of given poll
73
	 * Read all votes of a poll based on the poll id and return list as array
74
	 * @NoAdminRequired
75
	 * @param integer $pollId
76
	 * @param string $token
77
	 * @return Vote
78
	 */
79
	public function list($pollId = 0, $token = '') {
80
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) {
81
			throw new NotAuthorizedException;
82
		}
83
84
		if (!$this->acl->getAllowSeeResults()) {
85
			return $this->voteMapper->findByPollAndUser($this->acl->getpollId(), $this->acl->getUserId());
86
		} elseif (!$this->acl->getAllowSeeUsernames()) {
87
			$this->anonymizer->set($this->acl->getpollId(), $this->acl->getUserId());
88
			return $this->anonymizer->getVotes();
89
		} else {
90
			return $this->voteMapper->findByPoll($this->acl->getpollId());
91
		}
92
	}
93
94
	/**
95
	 * set
96
	 * @NoAdminRequired
97
	 * @param integer $pollId
98
	 * @param Array $option
99
	 * @param string $setTo
100
	 * @param string $token
101
	 * @return Vote
102
	 */
103
	public function set($pollId = 0, $pollOptionText, $setTo, $token = '') {
104
105
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowVote()) {
106
			throw new NotAuthorizedException;
107
		}
108
109
		$option = $this->optionMapper->findByPollAndText($this->acl->getpollId(), $pollOptionText);
110
111
		try {
112
			$this->vote = $this->voteMapper->findSingleVote($this->acl->getpollId(), $option->getPollOptionText(), $this->acl->getUserId());
113
			$this->vote->setVoteAnswer($setTo);
114
			$this->voteMapper->update($this->vote);
115
116
		} catch (DoesNotExistException $e) {
117
			// Vote does not exist, insert as new Vote
118
			$this->vote = new Vote();
119
120
			$this->vote->setPollId($this->acl->getpollId());
121
			$this->vote->setUserId($this->acl->getUserId());
122
			$this->vote->setVoteOptionText($option->getPollOptionText());
123
			$this->vote->setVoteOptionId($option->getId());
124
			$this->vote->setVoteAnswer($setTo);
125
			$this->voteMapper->insert($this->vote);
126
127
		} finally {
128
			$this->logService->setLog($this->vote->getPollId(), 'setVote', $this->vote->getUserId());
129
			return $this->vote;
130
		}
131
	}
132
133
	/**
134
	 * delete
135
	 * @NoAdminRequired
136
	 * @NoCSRFRequired
137
	 * @param integer $voteId
138
	 * @param string $userId
139
	 * @param integer $pollId
140
	 * @return Vote
141
	 */
142
	public function delete($pollId, $userId) {
143
144
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
145
			throw new NotAuthorizedException;
146
		}
147
148
		$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...
149
	}
150
151
}
152