Passed
Push — master ( 7972a5...654cd1 )
by Christoph
11:53 queued 12s
created

UnifiedSearchController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
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
namespace OC\Core\Controller;
27
28
use OC\Search\SearchComposer;
29
use OC\Search\SearchQuery;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
use OCP\Search\ISearchQuery;
36
37
class UnifiedSearchController extends Controller {
38
39
	/** @var SearchComposer */
40
	private $composer;
41
42
	/** @var IUserSession */
43
	private $userSession;
44
45
	public function __construct(IRequest $request,
46
								IUserSession $userSession,
47
								SearchComposer $composer) {
48
		parent::__construct('core', $request);
49
50
		$this->composer = $composer;
51
		$this->userSession = $userSession;
52
	}
53
54
	/**
55
	 * @NoAdminRequired
56
	 * @NoCSRFRequired
57
	 */
58
	public function getProviders(): JSONResponse {
59
		return new JSONResponse(
60
			$this->composer->getProviders()
61
		);
62
	}
63
64
	/**
65
	 * @NoAdminRequired
66
	 * @NoCSRFRequired
67
	 *
68
	 * @param string $providerId
69
	 * @param string $term
70
	 * @param int|null $sortOrder
71
	 * @param int|null $limit
72
	 * @param int|string|null $cursor
73
	 *
74
	 * @return JSONResponse
75
	 */
76
	public function search(string $providerId,
77
						   string $term = '',
78
						   ?int $sortOrder = null,
79
						   ?int $limit = null,
80
						   $cursor = null): JSONResponse {
81
		if (empty($term)) {
82
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
83
		}
84
85
		return new JSONResponse(
86
			$this->composer->search(
87
				$this->userSession->getUser(),
88
				$providerId,
89
				new SearchQuery(
90
					$term,
91
					$sortOrder ?? ISearchQuery::SORT_DATE_DESC,
92
					$limit ?? SearchQuery::LIMIT_DEFAULT,
93
					$cursor
94
				)
95
			)
96
		);
97
	}
98
}
99