Completed
Push — master ( 856486...57a834 )
by René
16s queued 11s
created

ShareMapper::removeDuplicates()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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