GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1)
by
unknown
12:30
created

Controller/SearchControllerTrait.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the Stinger Entity Search package.
6
 *
7
 * (c) Oliver Kotte <[email protected]>
8
 * (c) Florian Meyer <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace StingerSoft\EntitySearchBundle\Controller;
15
16
use StingerSoft\EntitySearchBundle\Form\QueryType;
17
use StingerSoft\EntitySearchBundle\Model\PaginatableResultSet;
18
use StingerSoft\EntitySearchBundle\Model\Query;
19
use StingerSoft\EntitySearchBundle\Services\Mapping\DocumentToEntityMapperInterface;
20
use StingerSoft\EntitySearchBundle\Services\SearchService;
21
use Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Session\SessionInterface;
24
25
trait SearchControllerTrait {
26
27
	/**
28
	 * @var SearchService
29
	 */
30
	protected $searchService;
31
	private $availableFacets;
32
	private $facetFormatter;
33
34
	public function searchAction(Request $request, DocumentToEntityMapperInterface $mapper) {
35
		if($request->query->get('term', false) !== false) {
0 ignored issues
show
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
			$this->setSearchTerm($request->getSession(), $request->query->get('term'));
37
			return $this->redirectToRoute('stinger_soft_entity_search_search');
38
		}
39
40
		$term = $this->getSearchTerm($request->getSession());
41
		$facets = $this->getSearchFacets($request->getSession());
42
		$availableFacets = $this->getAvailableFacets();
43
44
		$query = new Query($term, $facets, array_keys($availableFacets));
45
46
		$facetForm = $this->createForm(QueryType::class, $query, array(
47
			'used_facets' => $this->getConfiguredUsedFacets($query->getUsedFacets())
48
		));
49
50
		$facetForm->handleRequest($request);
51
		if($facetForm->isSubmitted()) {
52
			if($facetForm->get('clear')->isClicked()) {
53
				$query->setFacets($this->getDefaultFacets());
54
			}
55
			$this->setSearchTerm($request->getSession(), $query->getSearchTerm());
56
			$this->setSearchFacets($request->getSession(), $query->getFacets());
57
		} else {
58
			$query->setFacets($this->getDefaultFacets());
59
		}
60
61
		try {
62
			$result = $this->searchService->search($query);
63
64
			$facetForm = $this->createForm(QueryType::class, $query, array(
65
				'result'          => $result,
66
				'used_facets'     => $this->getConfiguredUsedFacets($query->getUsedFacets()),
67
				'facet_formatter' => $this->getFacetFormatter()
68
			));
69
70
			$page = (int)$request->query->get('page', 1);
71
			$results = null;
72
			if($result instanceof PaginatableResultSet) {
73
				$results = $result->paginate($page, $this->getResultsPerPage());
74
			} elseif($result !== null) {
75
				$results = $result->getResults(($page - 1) * $this->getResultsPerPage(), $this->getResultsPerPage());
76
			}
77
78
			return $this->render($this->getTemplate(), array(
79
				'results'   => $results,
80
				'resultSet' => $result,
81
				'term'      => $query->getSearchTerm(),
82
				'mapper'    => $mapper,
83
				'facetForm' => $facetForm->createView()
84
			));
85
		} catch(\Exception $exception) {
86
			$response = $this->render($this->getErrorTemplate(), array(
87
				'error' => $exception->getMessage(),
88
				'term'  => $query->getSearchTerm()
89
			));
90
			return $response->setStatusCode(500);
91
		}
92
	}
93
94
	/**
95
	 * Returns a JSON array of autocompletions for the given term.
96
	 * The term can be provided as a GET or POST paramater with the name <em>term</em>
97
	 *
98
	 * @param Request $request
99
	 * @return \Symfony\Component\HttpFoundation\JsonResponse
100
	 */
101
	public function autocompleteAction(Request $request) {
102
		$term = $request->get('term');
103
		return new JsonResponse($this->searchService->autocomplete($term, $this->getSuggestionCount()));
104
	}
105
106
	/**
107
	 * Provides an online help for the configured search service.
108
	 * If the search service has no online help defined a warning message will be displayed to the user.
109
	 *
110
	 * @param Request $request
111
	 * @return \Symfony\Component\HttpFoundation\Response
112
	 */
113
	public function onlineHelpAction(Request $request) {
114
		$template = $this->searchService->getOnlineHelp($request->getLocale(), $this->getDefaultLocale());
115
		return $this->render($template ?: 'StingerSoftEntitySearchBundle:Help:no_help.html.twig');
116
	}
117
118
	/**
119
	 * @param SearchService $searchService
120
	 * @required
121
	 */
122
	public function setSearchService(SearchService $searchService): void {
123
		$this->searchService = $searchService;
124
	}
125
126
	protected function getConfiguredUsedFacets(array $queryUsedFacets) {
127
		$availableFacets = $this->getAvailableFacets();
128
		$usedFacets = array();
129
		foreach($queryUsedFacets as $queryUsedFacet) {
130
			$usedFacets[$queryUsedFacet] = $availableFacets[$queryUsedFacet];
131
		}
132
		return $usedFacets;
133
	}
134
135
	/**
136
	 * Maximum number of suggestions which should be provided by the autocomplete action
137
	 *
138
	 * @return integer
139
	 */
140
	protected function getSuggestionCount() {
141
		return 10;
142
	}
143
144
	/**
145
	 * Prefix to store information in the session variable
146
	 *
147
	 * @return string
148
	 */
149
	protected function getSessionPrefix() {
150
		return 'stinger_soft_entity_search';
151
	}
152
153
	/**
154
	 * Number of results which should be displayed per page
155
	 *
156
	 * @return integer
157
	 */
158
	protected function getResultsPerPage() {
159
		return 10;
160
	}
161
162
	protected function getTemplate() {
163
		return 'StingerSoftEntitySearchBundle:Search:results.html.twig';
164
	}
165
166
	protected function getErrorTemplate() {
167
		return 'StingerSoftEntitySearchBundle:Search:error.html.twig';
168
	}
169
170
	protected function getFacetFormatter() {
171
		$this->initFacets();
172
		return $this->facetFormatter;
173
	}
174
175
	/**
176
	 * Returns an array of preselected facets (no facet selected is the default)
177
	 *
178
	 * @return string[string][]
179
	 */
180
	protected function getDefaultFacets() {
181
		$availableFacets = $this->getAvailableFacets();
182
		return array_combine(array_keys($availableFacets), array_fill(0, count($availableFacets), array()));
183
	}
184
185
	/**
186
	 * Returns an array of the available facets which should be offered the user as a filter
187
	 *
188
	 * @return string[]
189
	 */
190
	protected function getAvailableFacets() {
191
		$this->initFacets();
192
		return $this->availableFacets;
193
	}
194
195
	protected function initFacets() {
196
		if(!$this->availableFacets) {
197
			$this->availableFacets = array();
198
			$this->facetFormatter = array();
199
200
			$facetServices = $this->getParameter('stinger_soft.entity_search.available_facets');
201
			foreach($facetServices as $facetServiceId) {
202
				$facetService = $this->searchService->getFacet($facetServiceId);
203
				$this->availableFacets[$facetService->getField()] = $facetService->getFormOptions();
204
				if($facetService->getFacetFormatter()) {
205
					$this->facetFormatter[$facetService->getField()] = $facetService->getFacetFormatter();
206
				}
207
			}
208
		}
209
	}
210
211
	/**
212
	 * Fetches the searched facets from the session object
213
	 *
214
	 * @param SessionInterface $session
215
	 * @return string[string][]
216
	 */
217
	protected function getSearchFacets(SessionInterface $session) {
218
		$facets = $session->get($this->getSessionPrefix() . '_facets', false);
219
		return $facets ? \json_decode($facets, true) : $this->getDefaultFacets();
220
	}
221
222
	/**
223
	 * Sets the given search facets in the user's session
224
	 *
225
	 * @param SessionInterface $session
226
	 * @param string[string][] $facets
227
	 */
228
	protected function setSearchFacets(SessionInterface $session, $facets) {
229
		$session->set($this->getSessionPrefix() . '_facets', \json_encode($facets));
230
	}
231
232
	/**
233
	 * Fetches the search term from the session object
234
	 *
235
	 * @param SessionInterface $session
236
	 * @return mixed
237
	 */
238
	protected function getSearchTerm(SessionInterface $session) {
239
		return $session->get($this->getSessionPrefix() . '_term', false);
240
	}
241
242
	/**
243
	 * Sets the given search term in the user's session
244
	 *
245
	 * @param SessionInterface $session
246
	 * @param string $term
247
	 */
248
	protected function setSearchTerm(SessionInterface $session, $term) {
249
		$session->set($this->getSessionPrefix() . '_term', $term);
250
	}
251
252
}