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

VoteService::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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