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

SearchService::generateSearchRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
nc 1
cc 1
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\Service;
32
33
34
use Exception;
35
use OC\App\AppManager;
36
use OC\User\NoUserException;
37
use OCA\Circles\Api\v1\Circles;
38
use OCA\FullTextSearch\Exceptions\EmptySearchException;
39
use OCA\FullTextSearch\Exceptions\ProviderDoesNotExistException;
40
use OCA\FullTextSearch\Model\SearchRequest;
41
use OCA\FullTextSearch\Model\SearchResult;
42
use OCP\FullTextSearch\IFullTextSearchPlatform;
43
use OCP\FullTextSearch\IFullTextSearchProvider;
44
use OCP\FullTextSearch\Model\DocumentAccess;
45
use OCP\FullTextSearch\Model\ISearchRequest;
46
use OCP\FullTextSearch\Model\ISearchResult;
47
use OCP\FullTextSearch\Service\ISearchService;
48
use OCP\IGroupManager;
49
use OCP\IUser;
50
use OCP\IUserManager;
51
52
53
/**
54
 * Class SearchService
55
 *
56
 * @package OCA\FullTextSearch\Service
57
 */
58
class SearchService implements ISearchService {
59
60
61
	/** @var string */
62
	private $userId;
63
64
	/** @var AppManager */
65
	private $appManager;
66
67
	/** @var IUserManager */
68
	private $userManager;
69
70
	/** @var IGroupManager */
71
	private $groupManager;
72
73
	/** @var ConfigService */
74
	private $configService;
75
76
	/** @var ProviderService */
77
	private $providerService;
78
79
	/** @var PlatformService */
80
	private $platformService;
81
82
	/** @var MiscService */
83
	private $miscService;
84
85
86
	/**
87
	 * SearchService constructor.
88
	 *
89
	 * @param string $userId
90
	 * @param AppManager $appManager
91
	 * @param IUserManager $userManager
92
	 * @param IGroupManager $groupManager
93
	 * @param ConfigService $configService
94
	 * @param ProviderService $providerService
95
	 * @param PlatformService $platformService
96
	 * @param MiscService $miscService
97
	 */
98 View Code Duplication
	public function __construct(
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...
99
		$userId, AppManager $appManager, IUserManager $userManager,
100
		IGroupManager $groupManager,
101
		ConfigService $configService, ProviderService $providerService,
102
		PlatformService $platformService,
103
		MiscService $miscService
104
	) {
105
		$this->userId = $userId;
106
		$this->appManager = $appManager;
107
		$this->userManager = $userManager;
108
		$this->groupManager = $groupManager;
109
		$this->configService = $configService;
110
		$this->providerService = $providerService;
111
		$this->platformService = $platformService;
112
		$this->miscService = $miscService;
113
	}
114
115
116
	/**
117
	 * @param array $request
118
	 *
119
	 * @return ISearchRequest
120
	 */
121
	public function generateSearchRequest(array $request): ISearchRequest {
122
		$searchRequest = new SearchRequest();
123
		$searchRequest->importFromArray($request);
124
125
		return $searchRequest;
126
	}
127
128
129
	/**
130
	 * @param string $userId
131
	 * @param ISearchRequest $request
132
	 *
133
	 * @return ISearchResult[]
134
	 * @throws EmptySearchException
135
	 * @throws Exception
136
	 * @throws ProviderDoesNotExistException
137
	 */
138
	public function search(string $userId, ISearchRequest $request): array {
139
140
		$this->searchRequestCannotBeEmpty($request);
141
142
		if ($userId === '') {
143
			$userId = $this->userId;
144
		}
145
146
		$user = $this->userManager->get($userId);
147
		if ($user === null) {
148
			throw new NoUserException('User does not exist');
149
		}
150
151
		/** @var $request SearchRequest */
152
		$request->setAuthor($user->getUID());
153
		$request->cleanSearch();
154
155
		$providers = $this->providerService->getFilteredProviders($request->getProviders());
156
		$wrapper = $this->platformService->getPlatform();
157
		$platform = $wrapper->getPlatform();
158
159
		$access = $this->getDocumentAccessFromUser($user);
160
		$result = $this->searchFromProviders($platform, $providers, $access, $request);
161
162
//		foreach ($result as $searchResult) {
163
//			$searchResult->setPlatform($platform);
164
//			$searchResult->setRequest($request);
165
//		}
166
//
167
		return $result;
168
	}
169
170
171
	/**
172
	 * @param ISearchRequest $request
173
	 *
174
	 * @throws EmptySearchException
175
	 */
176
	private function searchRequestCannotBeEmpty(ISearchRequest $request) {
177
		if ($request === null || strlen($request->getSearch()) < 1) {
178
			throw new EmptySearchException('search cannot be empty');
179
		}
180
	}
181
182
183
	/**
184
	 * @param IFullTextSearchPlatform $platform
185
	 * @param DocumentAccess $access
186
	 * @param IFullTextSearchProvider[] $providers
187
	 * @param SearchRequest $request
188
	 *
189
	 * @return ISearchResult[]
190
	 */
191
	private function searchFromProviders(
192
		IFullTextSearchPlatform $platform, array $providers, DocumentAccess $access,
193
		SearchRequest $request
194
	): array {
195
		$result = [];
196
		foreach ($providers AS $provider) {
197
			$provider->improveSearchRequest($request);
198
199
			$searchResult = new SearchResult($request);
200
			$searchResult->setProvider($provider);
201
			$searchResult->setPlatform($platform);
202
203
			$platform->searchRequest($searchResult, $access);
204
			$provider->improveSearchResult($searchResult);
205
206
			$result[] = $searchResult;
207
		}
208
209
		return $result;
210
	}
211
212
213
	/**
214
	 * @param IUser $user
215
	 *
216
	 * @return DocumentAccess
217
	 */
218
	private function getDocumentAccessFromUser(IUser $user): DocumentAccess {
219
		$rights = new DocumentAccess();
220
221
		$rights->setViewerId($user->getUID());
222
		$rights->setGroups($this->groupManager->getUserGroupIds($user));
223
224
		if ($this->appManager->isEnabledForUser('circles', $user)) {
225
			try {
226
				$rights->setCircles(Circles::joinedCircleIds($user->getUID()));
227
			} catch (Exception $e) {
228
				$this->miscService->log('Circles is set as enabled but: ' . $e->getMessage());
229
			}
230
		}
231
232
		return $rights;
233
	}
234
235
236
}
237