ShareMapper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 66
c 1
b 0
f 0
dl 0
loc 150
ccs 0
cts 55
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A findByPollAndUser() 0 13 1
A findByToken() 0 10 1
A __construct() 0 2 1
A findAll() 0 7 1
A deleteByPoll() 0 9 1
A findByPoll() 0 10 1
A remove() 0 9 1
B removeDuplicates() 0 38 6
A deleteByUserId() 0 6 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<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(int $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 deleteByUserId(string $userId): void {
123
		$query = $this->db->getQueryBuilder();
124
		$query->delete($this->getTableName())
125
			->where('user_id = :userId')
126
			->setParameter('userId', $userId);
127
		$query->execute();
128
	}
129
130
	/**
131
	 * @return void
132
	 */
133
	public function remove(int $shareId): void {
134
		$qb = $this->db->getQueryBuilder();
135
136
		$qb->delete($this->getTableName())
137
		   ->where(
138
			   $qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))
139
		   );
140
141
		$qb->execute();
142
	}
143
144
	/**
145
	 * @return void
146
	 */
147
	public function removeDuplicates() {
148
		try {
149
			$query = $this->db->getQueryBuilder();
150
			// make sure, all public shares fit to the unique index added in schemaChange(),
151
			// by copying token to user_id
152
			$query->update($this->getTableName())
153
				->set('user_id', 'token')
154
				->where('type = :type')
155
				->setParameter('type', 'public')
156
				->execute();
157
158
			// remove duplicates from oc_polls_share
159
			// preserve the first entry
160
			$query = $this->db->getQueryBuilder();
161
			$query->select('id', 'type', 'poll_id', 'user_id')
162
				->from($this->getTableName());
163
			$foundEntries = $query->execute();
164
165
			$delete = $this->db->getQueryBuilder();
166
			$delete->delete($this->getTableName())->where('id = :id');
167
168
			$entries2Keep = [];
169
170
			while ($row = $foundEntries->fetch()) {
171
				$currentRecord = [
172
					$row['poll_id'],
173
					$row['type'],
174
					$row['user_id']
175
				];
176
177
				if (in_array($currentRecord, $entries2Keep) || $row['user_id'] === null || $row['type'] === '') {
178
					$delete->setParameter('id', $row['id']);
179
					$delete->execute();
180
				} else {
181
					$entries2Keep[] = $currentRecord;
182
				}
183
			}
184
		} catch (TableNotFoundException $e) {
185
			// ignore
186
		}
187
	}
188
}
189