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

SubscriptionMapper::findByPollAndUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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<Subscription>
33
 */
34
class SubscriptionMapper extends QBMapper {
35
	public function __construct(IDBConnection $db) {
36
		parent::__construct($db, 'polls_notif', '\OCA\Polls\Db\Subscription');
37 1
	}
38 1
39 1
	/**
40
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
41
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
42
	 * @return Subscription[]
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
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
55
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
56
	 * @return Subscription[]
57
	 */
58
	public function findAllByPoll(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
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
73
	 * @return Subscription
74
	 */
75
	public function findByPollAndUser(int $pollId, $userId) {
76
		$qb = $this->db->getQueryBuilder();
77
78
		$qb->select('*')
79
			->from($this->getTableName())
80
			->where(
81
				$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
82
			)
83
			->andWhere(
84
				$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
85
			);
86
87
		return $this->findEntity($qb);
88
	}
89
90
	public function unsubscribe($pollId, $userId): void {
91
		$qb = $this->db->getQueryBuilder();
92
93
		$qb->delete($this->getTableName())
94
		->where(
95
			$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
96
		)
97
		->andWhere(
98
			$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
99
		);
100
101
		$qb->execute();
102
	}
103
104
	/**
105
	 * @return void
106
	 */
107
	public function removeDuplicates() {
108
		$query = $this->db->getQueryBuilder();
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
109
110
		// remove duplicates from oc_polls_share
111
		// preserve the first entry
112
		$query = $this->db->getQueryBuilder();
113
		$query->select('id', 'poll_id', 'user_id')
114
			->from($this->getTableName());
115
		$foundEntries = $query->execute();
116
117
		$delete = $this->db->getQueryBuilder();
118
		$delete->delete($this->getTableName())->where('id = :id');
119
120
		$entries2Keep = [];
121
122
		while ($row = $foundEntries->fetch()) {
123
			$currentRecord = [
124
				$row['poll_id'],
125
				$row['user_id']
126
			];
127
128
			if (in_array($currentRecord, $entries2Keep)) {
129
				$delete->setParameter('id', $row['id']);
130
				$delete->execute();
131
			} else {
132
				$entries2Keep[] = $currentRecord;
133
			}
134
		}
135
	}
136
}
137