Completed
Push — master ( 8de354...4e0f9f )
by Maxence
01:38
created

FilesEvents::onFileRename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * Files_FullTextSearch - Index the content of your files
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\Files_FullTextSearch\Events;
32
33
34
use daita\MySmallPhpTools\Traits\TArrayTools;
35
use OCA\Files_FullTextSearch\Model\FilesDocument;
36
use OCA\Files_FullTextSearch\Service\FilesService;
37
use OCA\Files_FullTextSearch\Service\MiscService;
38
use OCP\Files\InvalidPathException;
39
use OCP\Files\NotFoundException;
40
use OCP\FullTextSearch\IFullTextSearchManager;
41
use OCP\FullTextSearch\Model\IIndex;
42
43
44
/**
45
 * Class FilesEvents
46
 *
47
 * @package OCA\Files_FullTextSearch\Events
48
 */
49
class FilesEvents {
50
51
52
	use TArrayTools;
53
54
55
	/** @var string */
56
	private $userId;
57
58
	/** @var IFullTextSearchManager */
59
	private $fullTextSearchManager;
60
61
	/** @var FilesService */
62
	private $filesService;
63
64
	/** @var MiscService */
65
	private $miscService;
66
67
68
	/**
69
	 * FilesEvents constructor.
70
	 *
71
	 * @param string $userId
72
	 * @param IFullTextSearchManager $fullTextSearchManager
73
	 * @param FilesService $filesService
74
	 * @param MiscService $miscService
75
	 */
76
	public function __construct(
77
		$userId, IFullTextSearchManager $fullTextSearchManager, FilesService $filesService,
78
		MiscService $miscService
79
	) {
80
		$this->userId = $userId;
81
		$this->fullTextSearchManager = $fullTextSearchManager;
82
		$this->filesService = $filesService;
83
		$this->miscService = $miscService;
84
	}
85
86
87
	/**
88
	 * @param array $params
89
	 *
90
	 * @throws InvalidPathException
91
	 * @throws NotFoundException
92
	 */
93 View Code Duplication
	public function onNewFile(array $params) {
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...
94
		$path = $this->get('path', $params, '');
95
		if ($path === '') {
96
			return;
97
		}
98
99
		$file = $this->filesService->getFileFromPath($this->userId, $path);
100
		$this->fullTextSearchManager->createIndex(
101
			'files', (string)$file->getId(), $this->userId, IIndex::INDEX_FULL
102
		);
103
	}
104
105
106
	/**
107
	 * @param array $params
108
	 *
109
	 * @throws InvalidPathException
110
	 * @throws NotFoundException
111
	 */
112
	public function onFileUpdate(array $params) {
113
		$path = $this->get('path', $params, '');
114
		if ($path === '') {
115
			return;
116
		}
117
118
		$file = $this->filesService->getFileFromPath($this->userId, $path);
119
		$this->fullTextSearchManager->updateIndexStatus(
120
			'files', (string)$file->getId(), IIndex::INDEX_FULL
121
		);
122
	}
123
124
125
	/**
126
	 * @param array $params
127
	 *
128
	 * @throws NotFoundException
129
	 * @throws InvalidPathException
130
	 */
131
	public function onFileRename(array $params) {
132
		$target = $this->get('newpath', $params, '');
133
		if ($target === '') {
134
			return;
135
		}
136
137
		$file = $this->filesService->getFileFromPath($this->userId, $target);
138
		$this->fullTextSearchManager->updateIndexStatus(
139
			'files', (string)$file->getId(), IIndex::INDEX_META
140
		);
141
	}
142
143
144
	/**
145
	 * @param array $params
146
	 *
147
	 * @throws InvalidPathException
148
	 * @throws NotFoundException
149
	 */
150 View Code Duplication
	public function onFileTrash(array $params) {
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...
151
		// check if trashbin does not exist. -> onFileDelete
152
		// we do not index trashbin
153
154
		$path = $this->get('path', $params, '');
155
		if ($path === '') {
156
			return;
157
		}
158
159
		$file = $this->filesService->getFileFromPath($this->userId, $path);
160
		$this->fullTextSearchManager->updateIndexStatus(
161
			'files', (string)$file->getId(), IIndex::INDEX_REMOVE, true
162
		);
163
	}
164
165
166
	/**
167
	 * @param array $params
168
	 *
169
	 * @throws InvalidPathException
170
	 * @throws NotFoundException
171
	 */
172
	public function onFileRestore(array $params) {
173
		$path = $this->get('filePath', $params, '');
174
		if ($path === '') {
175
			return;
176
		}
177
178
		$file = $this->filesService->getFileFromPath($this->userId, $path);
179
		$this->fullTextSearchManager->updateIndexStatus(
180
			'files', (string)$file->getId(), IIndex::INDEX_FULL
181
		);
182
	}
183
184
185
	/**
186
	 * @param array $params
187
	 */
188
	public function onFileDelete(array $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
//		$path = $this->get('path', $params, '');
190
//		if ($path === '') {
191
//			return;
192
//		}
193
194
//		$file = $this->filesService->getFileFromPath($this->userId, $path);
195
//		$this->fullTextSearchManager->updateIndexStatus('files', (string) $file->getId(), Index::INDEX_REMOVE);
196
	}
197
198
199
	/**
200
	 * @param array $params
201
	 */
202 View Code Duplication
	public function onFileShare(array $params) {
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...
203
		$fileId = $this->get('itemSource', $params, '');
204
		if ($fileId === '') {
205
			return;
206
		}
207
208
		$this->fullTextSearchManager->updateIndexStatus(
209
			'files', $fileId, FilesDocument::STATUS_FILE_ACCESS
210
		);
211
	}
212
213
214
	/**
215
	 * @param array $params
216
	 */
217 View Code Duplication
	public function onFileUnshare(array $params) {
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...
218
		$fileId = $this->get('itemSource', $params, '');
219
		if ($fileId === '') {
220
			return;
221
		}
222
223
		$this->fullTextSearchManager->updateIndexStatus(
224
			'files', $fileId, FilesDocument::STATUS_FILE_ACCESS
225
		);
226
	}
227
228
229
	public function onNewScannedFile2(array $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
230
	}
231
232
233
	public function onNewScannedFile(array $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
	}
235
}
236
237
238
239
240