FullTextSearchService::getCardsFromStack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2019
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
29
namespace OCA\Deck\Service;
30
31
32
use OC\FullTextSearch\Model\DocumentAccess;
33
use OC\FullTextSearch\Model\IndexDocument;
34
use OCA\Deck\Db\Board;
35
use OCA\Deck\Db\BoardMapper;
36
use OCA\Deck\Db\Card;
37
use OCA\Deck\Db\CardMapper;
38
use OCA\Deck\Db\Stack;
39
use OCA\Deck\Db\StackMapper;
40
use OCA\Deck\Provider\DeckProvider;
41
use OCP\AppFramework\Db\DoesNotExistException;
42
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
43
use OCP\FullTextSearch\Exceptions\FullTextSearchAppNotAvailableException;
44
use OCP\FullTextSearch\IFullTextSearchManager;
45
use OCP\FullTextSearch\Model\IDocumentAccess;
46
use OCP\FullTextSearch\Model\IIndex;
47
use OCP\FullTextSearch\Model\IIndexDocument;
48
use Symfony\Component\EventDispatcher\GenericEvent;
49
50
51
/**
52
 * Class FullTextSearchService
53
 *
54
 * @package OCA\Deck\Service
55
 */
56
class FullTextSearchService {
57
58
59
	/** @var BoardMapper */
60
	private $boardMapper;
61
62
	/** @var StackMapper */
63
	private $stackMapper;
64
65
	/** @var CardMapper */
66
	private $cardMapper;
67
68
	/** @var IFullTextSearchManager */
69
	private $fullTextSearchManager;
70
71
72
	/**
73
	 * FullTextSearchService constructor.
74
	 *
75
	 * @param BoardMapper $boardMapper
76
	 * @param StackMapper $stackMapper
77
	 * @param CardMapper $cardMapper
78
	 * @param IFullTextSearchManager $fullTextSearchManager
79
	 */
80
	public function __construct(
81
		BoardMapper $boardMapper, StackMapper $stackMapper, CardMapper $cardMapper,
82
		IFullTextSearchManager $fullTextSearchManager
83
	) {
84
		$this->boardMapper = $boardMapper;
85
		$this->stackMapper = $stackMapper;
86
		$this->cardMapper = $cardMapper;
87
88
		$this->fullTextSearchManager = $fullTextSearchManager;
89
	}
90
91
92
	/**
93
	 * @param GenericEvent $e
94
	 */
95
	public function onCardCreated(GenericEvent $e) {
96
		$cardId = $e->getArgument('id');
0 ignored issues
show
Bug introduced by
The method getArgument() does not seem to exist on object<OCP\FullTextSearc...pNotAvailableException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
		$userId = $e->getArgument('userId');
0 ignored issues
show
Bug introduced by
The method getArgument() does not seem to exist on object<OCP\FullTextSearc...pNotAvailableException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
99
		try {
100
			$this->fullTextSearchManager->createIndex(
101
				DeckProvider::DECK_PROVIDER_ID, (string)$cardId, $userId, IIndex::INDEX_FULL
102
			);
103
		} catch (FullTextSearchAppNotAvailableException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
104
		}
105
	}
106
107
108
	/**
109
	 * @param GenericEvent $e
110
	 */
111 View Code Duplication
	public function onCardUpdated(GenericEvent $e) {
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...
112
		$cardId = $e->getArgument('id');
0 ignored issues
show
Bug introduced by
The method getArgument() does not seem to exist on object<OCP\FullTextSearc...pNotAvailableException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
		try {
115
			$this->fullTextSearchManager->updateIndexStatus(
116
			DeckProvider::DECK_PROVIDER_ID, (string)$cardId, IIndex::INDEX_CONTENT
117
		);
118
		} catch (FullTextSearchAppNotAvailableException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
119
		}
120
	}
121
122
123
	/**
124
	 * @param GenericEvent $e
125
	 */
126 View Code Duplication
	public function onCardDeleted(GenericEvent $e) {
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...
127
		$cardId = $e->getArgument('id');
0 ignored issues
show
Bug introduced by
The method getArgument() does not seem to exist on object<OCP\FullTextSearc...pNotAvailableException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
129
		try {
130
			$this->fullTextSearchManager->updateIndexStatus(
131
				DeckProvider::DECK_PROVIDER_ID, (string)$cardId, IIndex::INDEX_REMOVE
132
			);
133
		} catch (FullTextSearchAppNotAvailableException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
134
		}
135
	}
136
137
138
	/**
139
	 * @param GenericEvent $e
140
	 */
141
	public function onBoardShares(GenericEvent $e) {
142
		$boardId = (int)$e->getArgument('boardId');
0 ignored issues
show
Bug introduced by
The method getArgument() does not seem to exist on object<OCP\FullTextSearc...pNotAvailableException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
144
		$cards = array_map(
145
			function(Card $item) {
146
				return $item->getId();
147
			},
148
			$this->getCardsFromBoard($boardId)
149
		);
150
		try {
151
			$this->fullTextSearchManager->updateIndexesStatus(
152
				DeckProvider::DECK_PROVIDER_ID, $cards, IIndex::INDEX_META
153
			);
154
		} catch (FullTextSearchAppNotAvailableException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
155
		}
156
	}
157
158
159
	/**
160
	 * @param Card $card
161
	 *
162
	 * @return IIndexDocument
163
	 */
164
	public function generateIndexDocumentFromCard(Card $card): IIndexDocument {
165
		$document = new IndexDocument(DeckProvider::DECK_PROVIDER_ID, (string)$card->getId());
166
167
		return $document;
168
	}
169
170
171
	/**
172
	 * @param IIndexDocument $document
173
	 *
174
	 * @throws DoesNotExistException
175
	 * @throws MultipleObjectsReturnedException
176
	 */
177
	public function fillIndexDocument(IIndexDocument $document) {
178
		/** @var Card $card */
179
		$card = $this->cardMapper->find((int)$document->getId());
180
181
		$document->setTitle(($card->getTitle() === null) ? '' : $card->getTitle());
182
		$document->setContent(($card->getDescription() === null) ? '' : $card->getDescription());
183
		$document->setAccess($this->generateDocumentAccessFromCardId((int)$card->getId()));
184
	}
185
186
187
	/**
188
	 * @param int $cardId
189
	 *
190
	 * @return IDocumentAccess
191
	 * @throws DoesNotExistException
192
	 * @throws MultipleObjectsReturnedException
193
	 */
194
	public function generateDocumentAccessFromCardId(int $cardId): IDocumentAccess {
195
		$board = $this->getBoardFromCardId($cardId);
196
197
		return new DocumentAccess($board->getOwner());
198
	}
199
200
201
	/**
202
	 * @param string $userId
203
	 *
204
	 * @return Card[]
205
	 */
206
	public function getCardsFromUser(string $userId): array {
207
		$cards = [];
208
		$boards = $this->getBoardsFromUser($userId);
209
		foreach ($boards as $board) {
210
			$stacks = $this->getStacksFromBoard($board->getId());
211
			foreach ($stacks as $stack) {
212
				$cards = array_merge($cards, $this->getCardsFromStack($stack->getId()));
213
			}
214
		}
215
216
		return $cards;
217
	}
218
219
220
	/**
221
	 * @param int $boardId
222
	 *
223
	 * @return Card[]
224
	 */
225
	public function getCardsFromBoard(int $boardId): array {
226
		$cards = [];
227
		$stacks = $this->getStacksFromBoard($boardId);
228
		foreach ($stacks as $stack) {
229
			$cards = array_merge($cards, $this->getCardsFromStack($stack->getId()));
230
		}
231
232
		return $cards;
233
	}
234
235
236
	/**
237
	 * @param int $cardId
238
	 *
239
	 * @return Board
240
	 * @throws DoesNotExistException
241
	 * @throws MultipleObjectsReturnedException
242
	 */
243
	public function getBoardFromCardId(int $cardId): Board {
244
		$boardId = (int)$this->cardMapper->findBoardId($cardId);
245
		/** @var Board $board */
246
		$board = $this->boardMapper->find($boardId);
247
248
		return $board;
249
	}
250
251
252
	/**
253
	 * @param int $stackId
254
	 *
255
	 * @return Card[]
256
	 */
257
	private function getCardsFromStack(int $stackId): array {
258
		return $this->cardMapper->findAll($stackId, null, null);
259
	}
260
261
262
	/**
263
	 * @param int $boardId
264
	 *
265
	 * @return Stack[]
266
	 */
267
	private function getStacksFromBoard(int $boardId): array {
268
		return $this->stackMapper->findAll($boardId, null, null);
269
270
	}
271
272
273
	/**
274
	 * @param string $userId
275
	 *
276
	 * @return Board[]
277
	 */
278
	private function getBoardsFromUser(string $userId): array {
279
		return $this->boardMapper->findAllByUser($userId, null, null, -1);
280
	}
281
282
283
}
284
285