Passed
Pull Request — master (#1320)
by René
07:41 queued 03:42
created

VoteMapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 5%

Importance

Changes 0
Metric Value
wmc 13
eloc 68
c 0
b 0
f 0
dl 0
loc 134
ccs 3
cts 60
cp 0.05
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A deleteByPollAndUser() 0 6 1
A findSingleVote() 0 9 1
A findParticipantsVotes() 0 7 1
A findByPollAndUser() 0 8 1
A countYesVotes() 0 8 1
A findParticipantsByPoll() 0 10 1
A deleteByPoll() 0 5 1
A removeDuplicates() 0 27 4
A findByPoll() 0 7 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[email protected]>
6
 * @author René Gieling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Polls\Db;
26
27
use OCP\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\QBMapper;
30
use Doctrine\DBAL\Exception\TableNotFoundException;
31
32
/**
33
 * @template-extends QBMapper<Vote>
34
 */
35
class VoteMapper extends QBMapper {
36
	public function __construct(IDBConnection $db) {
37 1
		parent::__construct($db, 'polls_votes', '\OCA\Polls\Db\Vote');
38 1
	}
39 1
40
	/**
41
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
42
	 * @return Vote[]
43
	 */
44
	public function findByPoll(int $pollId) {
45
		$qb = $this->db->getQueryBuilder();
46
47
		$qb->select('*')
48
		   ->from($this->getTableName())
49
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)));
50
		return $this->findEntities($qb);
51
	}
52
53
	/**
54
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
55
	 * @return Vote[]
56
	 */
57
	public function findByPollAndUser(int $pollId, string $userId) {
58
		$qb = $this->db->getQueryBuilder();
59
60
		$qb->select('*')
61
		   ->from($this->getTableName())
62
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
63
		   ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
64
		return $this->findEntities($qb);
65
	}
66
67
	/**
68
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
69
	 * @return Vote
70
	 */
71
	public function findSingleVote(int $pollId, string $optionText, string $userId) {
72
		$qb = $this->db->getQueryBuilder();
73
74
		$qb->select('*')
75
		   ->from($this->getTableName())
76
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
77
		   ->andWhere($qb->expr()->eq('vote_option_text', $qb->createNamedParameter($optionText, IQueryBuilder::PARAM_STR)))
78
		   ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
79
		return $this->findEntity($qb);
80
	}
81
82
	/**
83
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
84
	 * @return Vote[]
85
	 */
86
	public function findParticipantsByPoll(int $pollId) {
87
		$qb = $this->db->getQueryBuilder();
88
89
		$qb->selectDistinct('user_id')
90
		   ->from($this->getTableName())
91
		   ->where(
92
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
93
		   );
94
95
		return $this->findEntities($qb);
96
	}
97
98
	/**
99
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
100
	 *
101
	 * @return Vote[]
102
	 *
103
	 * @psalm-return array<array-key, Vote>
104
	 */
105
	public function findParticipantsVotes(int $pollId, $userId): array {
106
		$qb = $this->db->getQueryBuilder();
107
		$qb->select('*')
108
		   ->from($this->getTableName())
109
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
110
		   ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
111
		return $this->findEntities($qb);
112
	}
113
114
	public function deleteByPollAndUser(int $pollId, string $userId): void {
115
		$qb = $this->db->getQueryBuilder();
116
		$qb->delete($this->getTableName())
117
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
118
		   ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
119
		   ->execute();
120
	}
121
122
	public function deleteByPoll($pollId): void {
123
		$qb = $this->db->getQueryBuilder();
124
		$qb->delete($this->getTableName())
125
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
126
		   ->execute();
127
	}
128
129
	public function countYesVotes(string $userId, int $pollId): int {
130
		$qb = $this->db->getQueryBuilder();
131
		$qb->select('*')
132
		   ->from($this->getTableName())
133
		   ->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
134
		   ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
135
		   ->andWhere($qb->expr()->eq('vote_answer', $qb->createNamedParameter('yes', IQueryBuilder::PARAM_STR)));
136
		return count($this->findEntities($qb));
137
	}
138
139
	/**
140
	 * @return void
141
	 */
142
	public function removeDuplicates() {
143
		try {
144
			$query = $this->db->getQueryBuilder();
145
			$query->select('id', 'poll_id', 'user_id', 'vote_option_text')
146
				->from($this->getTableName());
147
			$foundEntries = $query->execute();
148
149
			$delete = $this->db->getQueryBuilder();
150
			$delete->delete($this->getTableName())
151
				->where('id = :id');
152
153
			$entries2Keep = [];
154
155
			while ($row = $foundEntries->fetch()) {
156
				$currentRecord = [
157
					$row['poll_id'],
158
					$row['user_id'],
159
					$row['vote_option_text']
160
				];
161
				if (in_array($currentRecord, $entries2Keep)) {
162
					$delete->setParameter('id', $row['id']);
163
					$delete->execute();
164
				} else {
165
					$entries2Keep[] = $currentRecord;
166
				}
167
			}
168
		} catch (TableNotFoundException $e) {
169
			// ignore
170
		}
171
	}
172
}
173