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

PreferencesMapper::removeDuplicates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 26
rs 9.6333
ccs 0
cts 0
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\IDBConnection;
28
use OCP\AppFramework\Db\QBMapper;
29
30
/**
31
 * @template-extends QBMapper<Preferences>
32
 */
33
class PreferencesMapper extends QBMapper {
34
	public function __construct(IDBConnection $db) {
35
		parent::__construct($db, 'polls_preferences', '\OCA\Polls\Db\Preferences');
36
	}
37
38
	/**
39
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
40
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
41
	 * @return Preferences
42
	 */
43
44
	public function find($userId): Preferences {
45
		$qb = $this->db->getQueryBuilder();
46
47
		$qb->select('*')
48
		   ->from($this->getTableName())
49
		   ->where(
50
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
51
		   );
52
53
		return $this->findEntity($qb);
54
	}
55
56
	/**
57
	 * @return void
58
	 */
59
	public function removeDuplicates() {
60
		$query = $this->db->getQueryBuilder();
61
		$query->delete($this->getTableName())
62
			->where('user_id = :userId')
63
			->setParameter('userId', '');
64
		$query->execute();
65
66
		// remove duplicate preferences from oc_polls_preferences
67
		// preserve the last user setting in the db
68
		$query = $this->db->getQueryBuilder();
69
		$query->select('id', 'user_id')
70
			->from($this->getTableName());
71
		$users = $query->execute();
72
73
		$delete = $this->db->getQueryBuilder();
74
		$delete->delete($this->getTableName())
75
			->where('id = :id');
76
77
		$userskeep = [];
78
79
		while ($row = $users->fetch()) {
80
			if (in_array($row['user_id'], $userskeep)) {
81
				$delete->setParameter('id', $row['id']);
82
				$delete->execute();
83
			} else {
84
				$userskeep[] = $row['user_id'];
85
			}
86
		}
87
	}
88
}
89