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

ShareMapper::removeDuplicates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 35
rs 9.552
ccs 0
cts 4
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<Share>
33
 */
34
class ShareMapper extends QBMapper {
35
	public function __construct(IDBConnection $db) {
36
		parent::__construct($db, 'polls_share', '\OCA\Polls\Db\Share');
37
	}
38
39
	/**
40
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
41
	 * @return Share[]
42
	 */
43
	public function findAll() {
44
		$qb = $this->db->getQueryBuilder();
45
46
		$qb->select('*')
47
		   ->from($this->getTableName());
48
49
		return $this->findEntities($qb);
50
	}
51
52
53
	/**
54
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
55
	 * @return Share[]
56
	 */
57
	public function findByPoll(int $pollId) {
58
		$qb = $this->db->getQueryBuilder();
59
60
		$qb->select('*')
61
		   ->from($this->getTableName())
62
		   ->where(
63
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
64
		   );
65
66
		return $this->findEntities($qb);
67
	}
68
69
	/**
70
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
71
	 * @return Share
72
	 */
73
	public function findByPollAndUser(int $pollId, string $userId): Share {
74
		$qb = $this->db->getQueryBuilder();
75
76
		$qb->select('*')
77
		   ->from($this->getTableName())
78
		   ->where(
79
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
80
		   )
81
		   ->andWhere(
82
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
83
		   );
84
85
		return $this->findEntity($qb);
86
	}
87
88
	/**
89
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
90
	 * @return Share
91
	 */
92
	public function findByToken(string $token) {
93
		$qb = $this->db->getQueryBuilder();
94
95
		$qb->select('*')
96
		   ->from($this->getTableName())
97
		   ->where(
98
			   $qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))
99
		   );
100
101
		return $this->findEntity($qb);
102
	}
103
104
	/**
105
	 * @return void
106
	 */
107
	public function deleteByPoll($pollId): void {
108
		$qb = $this->db->getQueryBuilder();
109
110
		$qb->delete($this->getTableName())
111
		   ->where(
112
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
113
		   );
114
115
		$qb->execute();
116
	}
117
118
	/**
119
	 * @return void
120
	 */
121
	public function remove($shareId): void {
122
		$qb = $this->db->getQueryBuilder();
123
124
		$qb->delete($this->getTableName())
125
		   ->where(
126
			   $qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))
127
		   );
128
129
		$qb->execute();
130
	}
131
132
	/**
133
	 * @return void
134
	 */
135
	public function removeDuplicates() {
136
		$query = $this->db->getQueryBuilder();
137
138
		// make sure, all public shares fit to the unique index added in schemaChange(),
139
		// by copying token to user_id
140
		$query->update($this->getTableName())
141
			->set('user_id', 'token')
142
			->where('type = :type')
143
			->setParameter('type', 'public')
144
			->execute();
145
146
		// remove duplicates from oc_polls_share
147
		// preserve the first entry
148
		$query = $this->db->getQueryBuilder();
149
		$query->select('id', 'type', 'poll_id', 'user_id')
150
			->from($this->getTableName());
151
		$foundEntries = $query->execute();
152
153
		$delete = $this->db->getQueryBuilder();
154
		$delete->delete($this->getTableName())->where('id = :id');
155
156
		$entries2Keep = [];
157
158
		while ($row = $foundEntries->fetch()) {
159
			$currentRecord = [
160
				$row['poll_id'],
161
				$row['type'],
162
				$row['user_id']
163
			];
164
165
			if (in_array($currentRecord, $entries2Keep)) {
166
				$delete->setParameter('id', $row['id']);
167
				$delete->execute();
168
			} else {
169
				$entries2Keep[] = $currentRecord;
170
			}
171
		}
172
	}
173
}
174