Completed
Push — master ( 924656...92b011 )
by Maxence
02:05
created

SearchService::searchRequestCannotBeEmpty()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Service;
28
29
use Exception;
30
use OC\App\AppManager;
31
use OCA\Circles\Api\v1\Circles;
32
use OCA\FullTextSearch\Exceptions\EmptySearchException;
33
use OCA\FullTextSearch\Exceptions\ProviderDoesNotExistException;
34
use OCA\FullTextSearch\IFullTextSearchPlatform;
35
use OCA\FullTextSearch\IFullTextSearchProvider;
36
use OCA\FullTextSearch\Model\DocumentAccess;
37
use OCA\FullTextSearch\Model\SearchRequest;
38
use OCA\FullTextSearch\Model\SearchResult;
39
use OCP\IGroupManager;
40
use OCP\IUser;
41
use OCP\IUserManager;
42
43
44
class SearchService {
45
46
	/** @var string */
47
	private $userId;
48
49
	/** @var AppManager */
50
	private $appManager;
51
52
	/** @var IUserManager */
53
	private $userManager;
54
55
	/** @var IGroupManager */
56
	private $groupManager;
57
58
	/** @var ConfigService */
59
	private $configService;
60
61
	/** @var ProviderService */
62
	private $providerService;
63
64
	/** @var PlatformService */
65
	private $platformService;
66
67
	/** @var MiscService */
68
	private $miscService;
69
70
71
	/**
72
	 * IndexService constructor.
73
	 *
74
	 * @param string $userId
75
	 * @param AppManager $appManager
76
	 * @param IUserManager $userManager
77
	 * @param IGroupManager $groupManager
78
	 * @param ConfigService $configService
79
	 * @param ProviderService $providerService
80
	 * @param PlatformService $platformService
81
	 * @param MiscService $miscService
82
	 */
83
	public function __construct(
84
		$userId, AppManager $appManager, IUserManager $userManager, IGroupManager $groupManager,
85
		ConfigService $configService, ProviderService $providerService,
86
		PlatformService $platformService,
87
		MiscService $miscService
88
	) {
89
		$this->userId = $userId;
90
		$this->appManager = $appManager;
91
		$this->userManager = $userManager;
92
		$this->groupManager = $groupManager;
93
		$this->configService = $configService;
94
		$this->providerService = $providerService;
95
		$this->platformService = $platformService;
96
		$this->miscService = $miscService;
97
	}
98
99
100
	/**
101
	 * @param string $userId
102
	 * @param SearchRequest $request
103
	 *
104
	 * @return SearchResult[]
105
	 * @throws EmptySearchException
106
	 * @throws Exception
107
	 * @throws ProviderDoesNotExistException
108
	 */
109
	public function search($userId, SearchRequest $request) {
110
111
		$this->searchRequestCannotBeEmpty($request);
112
113
		if ($userId === null) {
114
			$userId = $this->userId;
115
		}
116
117
		$user = $this->userManager->get($userId);
118
		$request->setAuthor($user->getUID());
119
		$request->cleanSearch();
120
121
		$providers = $this->providerService->getFilteredProviders($request->getProviders());
122
		$platform = $this->platformService->getPlatform();
123
124
		$access = $this->getDocumentAccessFromUser($user);
125
		$result = $this->searchFromProviders($platform, $providers, $access, $request);
126
127
		foreach ($result as $searchResult) {
128
			$searchResult->setPlatform($platform);
129
			$searchResult->setRequest($request);
130
		}
131
132
		return $result;
133
	}
134
135
136
	/**
137
	 * @param SearchRequest $request
138
	 *
139
	 * @throws EmptySearchException
140
	 */
141
	private function searchRequestCannotBeEmpty(SearchRequest $request) {
142
		if ($request === null || strlen($request->getSearch()) < 2) {
143
			throw new EmptySearchException('search cannot be empty');
144
		}
145
	}
146
147
148
	/**
149
	 * @param IFullTextSearchPlatform $platform
150
	 * @param DocumentAccess $access
151
	 * @param IFullTextSearchProvider[] $providers
152
	 * @param SearchRequest $request
153
	 *
154
	 * @return SearchResult[]
155
	 */
156
	private function searchFromProviders(
157
		IFullTextSearchPlatform $platform, $providers, DocumentAccess $access,
158
		SearchRequest $request
159
	) {
160
		$result = [];
161
		foreach ($providers AS $provider) {
162
			$provider->improveSearchRequest($request);
163
			$searchResult = $platform->searchDocuments($provider, $access, $request);
164
			$searchResult->setProvider($provider);
165
			$searchResult->setPlatform($platform);
166
167
			$provider->improveSearchResult($searchResult);
168
			$result[] = $searchResult;
169
		}
170
171
		return $result;
172
	}
173
174
175
	/**
176
	 * @param IUser $user
177
	 *
178
	 * @return DocumentAccess
179
	 */
180
	private function getDocumentAccessFromUser(IUser $user) {
181
		$rights = new DocumentAccess();
182
183
		$rights->setViewerId($user->getUID());
184
		$rights->setGroups($this->groupManager->getUserGroupIds($user));
185
186
		if ($this->appManager->isEnabledForUser('circles', $user)) {
187
			try {
188
				$rights->setCircles(Circles::joinedCircleIds($user->getUID()));
189
			} catch (Exception $e) {
190
				$this->miscService->log('Circles is set as enabled but: ' . $e->getMessage());
191
			}
192
		}
193
194
		return $rights;
195
	}
196
197
198
}