Passed
Push — master ( 79fc7e...55473d )
by Joas
12:07 queued 12s
created

UnifiedSearchController::getRouteInformation()   A

Complexity

Conditions 5
Paths 21

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 21
nop 1
dl 0
loc 31
rs 9.3554
c 0
b 0
f 0
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\Route\IRouter;
36
use OCP\Search\ISearchQuery;
37
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
38
39
class UnifiedSearchController extends Controller {
40
41
	/** @var SearchComposer */
42
	private $composer;
43
44
	/** @var IUserSession */
45
	private $userSession;
46
47
	/** @var IRouter */
48
	private $router;
49
50
	public function __construct(IRequest $request,
51
								IUserSession $userSession,
52
								SearchComposer $composer,
53
								IRouter $router) {
54
		parent::__construct('core', $request);
55
56
		$this->composer = $composer;
57
		$this->userSession = $userSession;
58
		$this->router = $router;
59
	}
60
61
	/**
62
	 * @NoAdminRequired
63
	 * @NoCSRFRequired
64
	 *
65
	 * @param string $from the url the user is currently at
66
	 *
67
	 * @return JSONResponse
68
	 */
69
	public function getProviders(string $from = ''): JSONResponse {
70
		[$route, $parameters] = $this->getRouteInformation($from);
71
72
		return new JSONResponse(
73
			$this->composer->getProviders($route, $parameters)
74
		);
75
	}
76
77
	/**
78
	 * @NoAdminRequired
79
	 * @NoCSRFRequired
80
	 *
81
	 * @param string $providerId
82
	 * @param string $term
83
	 * @param int|null $sortOrder
84
	 * @param int|null $limit
85
	 * @param int|string|null $cursor
86
	 * @param string $from
87
	 *
88
	 * @return JSONResponse
89
	 */
90
	public function search(string $providerId,
91
						   string $term = '',
92
						   ?int $sortOrder = null,
93
						   ?int $limit = null,
94
						   $cursor = null,
95
						   string $from = ''): JSONResponse {
96
		if (empty(trim($term))) {
97
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
98
		}
99
		[$route, $routeParameters] = $this->getRouteInformation($from);
100
101
		return new JSONResponse(
102
			$this->composer->search(
103
				$this->userSession->getUser(),
104
				$providerId,
105
				new SearchQuery(
106
					$term,
107
					$sortOrder ?? ISearchQuery::SORT_DATE_DESC,
108
					$limit ?? SearchQuery::LIMIT_DEFAULT,
109
					$cursor,
110
					$route,
111
					$routeParameters
112
				)
113
			)
114
		);
115
	}
116
117
	protected function getRouteInformation(string $url): array {
118
		$routeStr = '';
119
		$parameters = [];
120
121
		if ($url !== '') {
122
			$urlParts = parse_url($url);
123
124
			try {
125
				$parameters = $this->router->findMatchingRoute($urlParts['path']);
0 ignored issues
show
Bug introduced by
The method findMatchingRoute() does not exist on OCP\Route\IRouter. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\Route\IRouter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
				/** @scrutinizer ignore-call */ 
126
    $parameters = $this->router->findMatchingRoute($urlParts['path']);
Loading history...
126
127
				// contacts.PageController.index => contacts.Page.index
128
				$route = $parameters['caller'];
129
				if (substr($route[1], -10) === 'Controller') {
130
					$route[1] = substr($route[1], 0, -10);
131
				}
132
				$routeStr = implode('.', $route);
133
134
				// cleanup
135
				unset($parameters['_route'], $parameters['action'], $parameters['caller']);
136
			} catch (ResourceNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
137
			}
138
139
			if (isset($urlParts['query'])) {
140
				parse_str($urlParts['query'], $queryParameters);
141
				$parameters = array_merge($parameters, $queryParameters);
142
			}
143
		}
144
145
		return [
146
			$routeStr,
147
			$parameters,
148
		];
149
	}
150
}
151