Completed
Push — master ( 347a51...ad246f )
by Maxence
02:23
created

TickRequest::getTickBySource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
rs 9.8333
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for Nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch\Db;
32
33
34
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
35
use OCA\FullTextSearch\Model\Tick;
36
37
38
/**
39
 * Class TickRequest
40
 *
41
 * @package OCA\FullTextSearch\Db
42
 */
43
class TickRequest extends TickRequestBuilder {
44
45
46
	/**
47
	 * @param Tick $tick
48
	 *
49
	 * @return int
50
	 * @throws \Exception
51
	 */
52
	public function create(Tick $tick): int {
53
54
		try {
55
			$qb = $this->getTickInsertSql();
56
			$qb->setValue('source', $qb->createNamedParameter($tick->getSource()))
57
			   ->setValue('data', $qb->createNamedParameter(json_encode($tick->getData())))
58
			   ->setValue('action', $qb->createNamedParameter($tick->getAction()))
59
			   ->setValue('first_tick', $qb->createNamedParameter($tick->getFirstTick()))
60
			   ->setValue('tick', $qb->createNamedParameter($tick->getTick()))
61
			   ->setValue('status', $qb->createNamedParameter($tick->getStatus()));
62
63
			$qb->execute();
64
65
			return $qb->getLastInsertId();
66
		} catch (\Exception $e) {
67
			throw $e;
68
		}
69
	}
70
71
72
	/**
73
	 * @param Tick $tick
74
	 *
75
	 * @return bool
76
	 */
77
	public function update(Tick $tick): bool {
78
79
		try {
80
			$this->getTickById($tick->getId());
81
		} catch (TickDoesNotExistException $e) {
82
			return false;
83
		}
84
85
		$qb = $this->getTickUpdateSql();
86
		$qb->set('data', $qb->createNamedParameter(json_encode($tick->getData())))
87
		   ->set('tick', $qb->createNamedParameter($tick->getTick()))
88
		   ->set('action', $qb->createNamedParameter($tick->getAction()))
89
		   ->set('status', $qb->createNamedParameter($tick->getStatus()));
90
91
		$this->limitToId($qb, $tick->getId());
92
93
		$qb->execute();
94
95
		return true;
96
	}
97
98
99
	/**
100
	 * @param Tick $tick
101
	 */
102
	public function deleteIndex(Tick $tick) {
103
		$qb = $this->getTickDeleteSql();
104
		$this->limitToId($qb, $tick->getId());
105
		$this->limitToSource($qb, $tick->getSource());
106
107
		$qb->execute();
108
	}
109
110
111
	/**
112
	 *
113
	 */
114
	public function reset() {
115
		$qb = $this->getTickDeleteSql();
116
117
		$qb->execute();
118
	}
119
120
121
	/**
122
	 * return tick.
123
	 *
124
	 * @param int $id
125
	 *
126
	 * @return Tick
127
	 * @throws TickDoesNotExistException
128
	 */
129 View Code Duplication
	public function getTickById(int $id): Tick {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
		$qb = $this->getTickSelectSql();
131
		$this->limitToId($qb, $id);
132
133
		$cursor = $qb->execute();
134
		$data = $cursor->fetch();
135
		$cursor->closeCursor();
136
137
		if ($data === false) {
138
			throw new TickDoesNotExistException($this->l10n->t('Tick not found'));
139
		}
140
141
		return $this->parseTickSelectSql($data);
142
	}
143
144
145
	/**
146
	 * return ticks.
147
	 *
148
	 * @param string $status
149
	 *
150
	 * @return Tick[]
151
	 */
152 View Code Duplication
	public function getTicksByStatus(string $status): array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
154
		$ticks = [];
155
156
		$qb = $this->getTickSelectSql();
157
		$this->limitToStatus($qb, $status);
158
159
		$cursor = $qb->execute();
160
		while ($data = $cursor->fetch()) {
161
			$ticks[] = $this->parseTickSelectSql($data);
162
		}
163
		$cursor->closeCursor();
164
165
		return $ticks;
166
	}
167
168
169
	/**
170
	 * @param string $source
171
	 *
172
	 * @return Tick[]
173
	 */
174 View Code Duplication
	public function getTicksBySource(string $source): array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
		$qb = $this->getTickSelectSql();
176
		$this->limitToSource($qb, $source);
177
178
		$ticks = [];
179
		$cursor = $qb->execute();
180
		while ($data = $cursor->fetch()) {
181
			$ticks[] = $this->parseTickSelectSql($data);
182
		}
183
		$cursor->closeCursor();
184
185
		return $ticks;
186
	}
187
188
189
}
190