Failed Conditions
Push — master ( a052b2...ee9c7d )
by Rafael
18:58
created

SuggestController::suggestAction()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 15
nc 7
nop 2
crap 6
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script 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 General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Search\Suggest\SuggestService;
29
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Class SuggestController
34
 *
35
 * @author Frans Saris <[email protected]>
36
 * @author Timo Hund <[email protected]>
37
 * @package ApacheSolrForTypo3\Solr\Controller
38
 */
39
class SuggestController extends AbstractBaseController
40
{
41
    /**
42
     * This method creates a suggest json response that can be used in a suggest layer.
43
     *
44
     * @param string $queryString
45
     * @param string $callback
46
     * @return string
47
     */
48
    public function suggestAction($queryString, $callback)
49
    {
50
        $jsonPCallback = htmlspecialchars($callback);
51
        // Get suggestions
52
        $rawQuery = htmlspecialchars(mb_strtolower(trim($queryString)));
53
54
        try {
55
            /** @var SuggestService $suggestService */
56
            $suggestService = GeneralUtility::makeInstance(SuggestService::class, $this->typoScriptFrontendController, $this->searchService, $this->typoScriptConfiguration);
57
            $additionalFilters = htmlspecialchars(GeneralUtility::_GET('filters'));
58
59
            $pageId = $this->typoScriptFrontendController->getRequestedId();
60
            $languageId = $this->typoScriptFrontendController->sys_language_uid;
61
            $arguments = (array)$this->request->getArguments();
62
            $searchRequest = $this->getSearchRequestBuilder()->buildForSuggest($arguments, $rawQuery, $pageId, $languageId);
63
            $result = $suggestService->getSuggestions($searchRequest, $additionalFilters);
64
        } catch (SolrUnavailableException $e) {
65
            $this->handleSolrUnavailable();
66
            $result = ['status' => false];
67
        }
68
69
        return htmlspecialchars($jsonPCallback) . '(' . json_encode($result) . ')';
70
    }
71
72
}
73