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

LogMapper::removeDuplicates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 27
rs 9.6
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\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\QBMapper;
30
31
/**
32
 * @template-extends QBMapper<Log>
33
 */
34
class LogMapper extends QBMapper {
35
	public function __construct(IDBConnection $db) {
36
		parent::__construct($db, 'polls_log', '\OCA\Polls\Db\Log');
37
	}
38
39
	/**
40
	 * @return Log
41
	 */
42
	public function find($id): Log {
43
		$qb = $this->db->getQueryBuilder();
44
45
		$qb->select('*')
46
		   ->from($this->getTableName())
47
		   ->where(
48
			   $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
49
		   );
50
51
		return $this->findEntity($qb);
52
	}
53
54
	/**
55
	 * @return Log[]
56
	 * @psalm-return array<array-key, Log>
57
	 */
58
	public function findByPollId($pollId): array {
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
	 * @return Log[]
72
	 * @psalm-return array<array-key, Log>
73
	 */
74
	public function findUnprocessed(): array {
75
		$qb = $this->db->getQueryBuilder();
76
77
		$qb->select('*')
78
			->from($this->getTableName())
79
			->where(
80
			   $qb->expr()->eq('processed', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
81
		   );
82
83
		return $this->findEntities($qb);
84
	}
85
86
	/**
87
	 * @return Log[]
88
	 * @psalm-return array<array-key, Log>
89
	 */
90
	public function findUnprocessedPolls(): array {
91
		$qb = $this->db->getQueryBuilder();
92
93
		$qb->selectDistinct('poll_id')
94
			->from($this->getTableName())
95
			->where($qb->expr()->eq('processed', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
96
		return $this->findEntities($qb);
97
	}
98
99
	/**
100
	 * @return Log
101
	 */
102
	public function getLastRecord(int $pollId): Log {
103
		$qb = $this->db->getQueryBuilder();
104
105
		$qb->select('*')
106
			->from($this->getTableName())
107
			->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
108
			->setMaxResults(1)
109
			->orderBy('id', 'DESC');
110
111
		return $this->findEntity($qb);
112
	}
113
114
	/**
115
	 * @return void
116
	 */
117
	public function removeDuplicates() {
118
		// remove duplicates from oc_polls_log
119
		// preserve the first entry
120
		$query = $this->db->getQueryBuilder();
121
		$query->select('id', 'processed', 'poll_id', 'user_id', 'message_id', 'message')
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['processed'],
134
				$row['poll_id'],
135
				$row['user_id'],
136
				$row['message_id'],
137
				$row['message']
138
			];
139
			if (in_array($currentRecord, $entries2Keep)) {
140
				$delete->setParameter('id', $row['id']);
141
				$delete->execute();
142
			} else {
143
				$entries2Keep[] = $currentRecord;
144
			}
145
		}
146
	}
147
}
148