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

OptionMapper::removeDuplicates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 25
rs 9.6666
ccs 0
cts 7
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<Option>
33
 */
34
class OptionMapper extends QBMapper {
35
	public function __construct(IDBConnection $db) {
36
		parent::__construct($db, 'polls_options', '\OCA\Polls\Db\Option');
37 1
	}
38 1
39 1
	/**
40
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
41
	 * @return Option
42
	 */
43
	public function find(int $id): Option {
44
		$qb = $this->db->getQueryBuilder();
45
46
		$qb->select('*')
47
		   ->from($this->getTableName())
48
		   ->where(
49
			   $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
50
		   );
51
52
		return $this->findEntity($qb);
53
	}
54
55
	/**
56
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
57
	 * @return Option[]
58
	 */
59
	public function findByPoll(int $pollId): array {
60
		$qb = $this->db->getQueryBuilder();
61
62
		$qb->select('*')
63
		   ->from($this->getTableName())
64
		   ->where(
65
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
66
		   )
67
		   ->orderBy('order', 'ASC');
68
69
		return $this->findEntities($qb);
70
	}
71
72
	/**
73
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
74
	 * @return Option
75
	 */
76
	public function findByPollAndText($pollId, $pollOptionText): Option {
77
		$qb = $this->db->getQueryBuilder();
78
79
		$qb->select('*')
80
		   ->from($this->getTableName())
81
		   ->where(
82
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
83
		   )
84
		   ->andWhere(
85
			   $qb->expr()->eq('poll_option_text', $qb->createNamedParameter($pollOptionText, IQueryBuilder::PARAM_STR))
86
		   )
87
		   ->orderBy('order', 'ASC');
88
89
		return $this->findEntity($qb);
90
	}
91
92
	public function remove($optionId): void {
93
		$qb = $this->db->getQueryBuilder();
94
95
		$qb->delete($this->getTableName())
96
		   ->where(
97
			   $qb->expr()->eq('id', $qb->createNamedParameter($optionId, IQueryBuilder::PARAM_INT))
98
		   );
99
100
		$qb->execute();
101
	}
102
103
	public function deleteByPoll($pollId): void {
104
		$qb = $this->db->getQueryBuilder();
105
106
		$qb->delete($this->getTableName())
107
		   ->where(
108
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
109
		   );
110
111
		$qb->execute();
112
	}
113
114
	/**
115
	 * @return void
116
	 */
117
	public function removeDuplicates() {
118
		// remove duplicates from oc_polls_options
119
		// preserve the first entry
120
		$query = $this->db->getQueryBuilder();
121
		$query->select('id', 'poll_id', 'poll_option_text', 'timestamp')
122
			->from($this->getTableName());
123
		$foundEntries = $query->execute();
124
125
		$delete = $this->db->getQueryBuilder();
126
		$delete->delete($this->getTableName())
127
			->where('id = :id');
128
129
		$entries2Keep = [];
130
131
		while ($row = $foundEntries->fetch()) {
132
			$currentRecord = [
133
				$row['poll_id'],
134
				$row['poll_option_text'],
135
				$row['timestamp']
136
			];
137
			if (in_array($currentRecord, $entries2Keep)) {
138
				$delete->setParameter('id', $row['id']);
139
				$delete->execute();
140
			} else {
141
				$entries2Keep[] = $currentRecord;
142
			}
143
		}
144
	}
145
}
146