Passed
Pull Request — master (#1284)
by René
06:52 queued 03:10
created

VoteMapper::removeDuplicates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 23
rs 9.6666
ccs 0
cts 7
cp 0
cc 3
nc 3
nop 0
crap 12
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
31
/**
32
 * @template-extends QBMapper<Vote>
33
 */
34
class VoteMapper extends QBMapper {
35
	public function __construct(IDBConnection $db) {
36
		parent::__construct($db, 'polls_votes', '\OCA\Polls\Db\Vote');
37 1
	}
38 1
39 1
	/**
40
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
41
	 * @return Vote[]
42
	 */
43
	public function findByPoll(int $pollId) {
44
		$qb = $this->db->getQueryBuilder();
45
46
		$qb->select('*')
47
		   ->from($this->getTableName())
48
		   ->where(
49
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
50
		   );
51
52
		return $this->findEntities($qb);
53
	}
54
55
	/**
56
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
57
	 * @return Vote[]
58
	 */
59
	public function findByPollAndUser(int $pollId, string $userId) {
60
		$qb = $this->db->getQueryBuilder();
61
62
		$qb->select('*')
63
		   ->from($this->getTableName())
64
		   ->where(
65
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
66
		   )
67
		   ->andWhere(
68
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
69
		   );
70
71
		return $this->findEntities($qb);
72
	}
73
74
	/**
75
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
76
	 * @return Vote
77
	 */
78
	public function findSingleVote(int $pollId, string $optionText, string $userId) {
79
		$qb = $this->db->getQueryBuilder();
80
81
		$qb->select('*')
82
		   ->from($this->getTableName())
83
		   ->where(
84
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
85
		   )
86
		   ->andWhere(
87
			   $qb->expr()->eq('vote_option_text', $qb->createNamedParameter($optionText, IQueryBuilder::PARAM_STR))
88
		   )
89
		   ->andWhere(
90
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
91
		   );
92
		return $this->findEntity($qb);
93
	}
94
95
	/**
96
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
97
	 * @return Vote[]
98
	 */
99
	public function findParticipantsByPoll(int $pollId) {
100
		$qb = $this->db->getQueryBuilder();
101
102
		$qb->selectDistinct('user_id')
103
		   ->from($this->getTableName())
104
		   ->where(
105
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
106
		   );
107
108
		return $this->findEntities($qb);
109
	}
110
111
	/**
112
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
113
	 *
114
	 * @return Vote[]
115
	 *
116
	 * @psalm-return array<array-key, Vote>
117
	 */
118
	public function findParticipantsVotes(int $pollId, $userId): array {
119
		$qb = $this->db->getQueryBuilder();
120
121
		$qb->select('*')
122
		   ->from($this->getTableName())
123
		   ->where(
124
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
125
		   )
126
		   ->andWhere(
127
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
128
		   );
129
130
		return $this->findEntities($qb);
131
	}
132
133
	public function deleteByPollAndUser(int $pollId, string $userId): void {
134
		$qb = $this->db->getQueryBuilder();
135
136
		$qb->delete($this->getTableName())
137
		   ->where(
138
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
139
		   )
140
		   ->andWhere(
141
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
142
		   );
143
144
		$qb->execute();
145
	}
146
147
	public function deleteByPoll($pollId): void {
148
		$qb = $this->db->getQueryBuilder();
149
150
		$qb->delete($this->getTableName())
151
		->where(
152
			$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
153
		);
154
155
		$qb->execute();
156
	}
157
158
	/**
159
	 * @return void
160
	 */
161
	public function removeDuplicates() {
162
		$query = $this->db->getQueryBuilder();
163
		$query->select('id', 'poll_id', 'user_id', 'vote_option_text')
164
			->from($this->getTableName());
165
		$foundEntries = $query->execute();
166
167
		$delete = $this->db->getQueryBuilder();
168
		$delete->delete($this->getTableName())
169
			->where('id = :id');
170
171
		$entries2Keep = [];
172
173
		while ($row = $foundEntries->fetch()) {
174
			$currentRecord = [
175
				$row['poll_id'],
176
				$row['user_id'],
177
				$row['vote_option_text']
178
			];
179
			if (in_array($currentRecord, $entries2Keep)) {
180
				$delete->setParameter('id', $row['id']);
181
				$delete->execute();
182
			} else {
183
				$entries2Keep[] = $currentRecord;
184
			}
185
		}
186
	}
187
}
188