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

VoteService::set()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 39
ccs 0
cts 24
cp 0
rs 8.4444
cc 8
nc 21
nop 3
crap 72
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 OCP\AppFramework\Db\DoesNotExistException;
27
use OCA\Polls\Exceptions\NotAuthorizedException;
28
use OCA\Polls\Exceptions\VoteLimitExceededException;
29
30
use OCA\Polls\Db\Log;
31
use OCA\Polls\Db\OptionMapper;
32
use OCA\Polls\Db\VoteMapper;
33
use OCA\Polls\Db\Vote;
34
use OCA\Polls\Model\Acl;
35
36
class VoteService {
37
38
	/** @var VoteMapper */
39
	private $voteMapper;
40
41
	/** @var Vote */
42
	private $vote;
43
44
	/** @var OptionMapper */
45
	private $optionMapper;
46
47
	/** @var AnonymizeService */
48
	private $anonymizer;
49
50
	/** @var LogService */
51
	private $logService;
52
53
	/** @var Acl */
54
	private $acl;
55
56
	public function __construct(
57
		VoteMapper $voteMapper,
58
		Vote $vote,
59
		OptionMapper $optionMapper,
60
		AnonymizeService $anonymizer,
61
		LogService $logService,
62
		Acl $acl
63
	) {
64
		$this->voteMapper = $voteMapper;
65
		$this->vote = $vote;
66
		$this->optionMapper = $optionMapper;
67
		$this->anonymizer = $anonymizer;
68
		$this->logService = $logService;
69
		$this->acl = $acl;
70
	}
71
72
	/**
73
	 * Read all votes of a poll based on the poll id and return list as array
74
	 */
75
	public function list(int $pollId = 0, string $token = ''): array {
76
		if ($token) {
77
			$this->acl->setToken($token);
78
		} else {
79
			$this->acl->setPollId($pollId)->requestView();
80
		}
81
82
		try {
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
		} catch (DoesNotExistException $e) {
92
			return [];
93
		}
94
	}
95
96
	/**
97
	 * Set vote
98
	 */
99
	public function set(int $optionId, string $setTo, string $token = ''): Vote {
100
		$option = $this->optionMapper->find($optionId);
101
102
		if ($token) {
103
			$this->acl->setToken($token)->requestVote();
104
			if (intval($option->getPollId()) !== $this->acl->getPollId()) {
105
				throw new NotAuthorizedException;
106
			}
107
		} else {
108
			$this->acl->setPollId($option->getPollId())->requestVote();
109
		}
110
111
		if ($setTo === 'yes') {
112
			$this->acl->requestYesVotes();
113
		}
114
115
		if ($setTo === 'yes'
116
			&& $this->acl->getOptionLimit()
117
			&& $this->voteMapper->countYesVotesByOption($this->acl->getPollId(), $option->getPollOptionText())) {
118
			throw new VoteLimitExceededException;
119
		}
120
121
		try {
122
			$this->vote = $this->voteMapper->findSingleVote($this->acl->getPollId(), $option->getPollOptionText(), $this->acl->getUserId());
123
			$this->vote->setVoteAnswer($setTo);
124
			$this->voteMapper->update($this->vote);
125
		} catch (DoesNotExistException $e) {
126
			// Vote does not exist, insert as new Vote
127
			$this->vote = new Vote();
128
129
			$this->vote->setPollId($this->acl->getPollId());
130
			$this->vote->setUserId($this->acl->getUserId());
131
			$this->vote->setVoteOptionText($option->getPollOptionText());
132
			$this->vote->setVoteOptionId($option->getId());
133
			$this->vote->setVoteAnswer($setTo);
134
			$this->voteMapper->insert($this->vote);
135
		}
136
		$this->logService->setLog($this->acl->getPollId(), Log::MSG_ID_SETVOTE, $this->vote->getUserId());
137
		return $this->vote;
138
	}
139
140
	/**
141
	 * Remove user from poll
142
	 */
143
	public function delete(int $pollId, string $userId): string {
144
		$this->acl->setPollId($pollId)->requestEdit();
145
		$this->voteMapper->deleteByPollAndUser($pollId, $userId);
146
		return $userId;
147
	}
148
}
149