Completed
Push — master ( 6a4750...2879ec )
by Timo
16s
created

SuggestController::handleSolrUnavailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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\Logging\SolrLogManager;
30
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Class SuggestController
35
 *
36
 * @author Frans Saris <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 * @package ApacheSolrForTypo3\Solr\Controller
39
 */
40
class SuggestController extends AbstractBaseController
41
{
42
    /**
43
     * This method creates a suggest json response that can be used in a suggest layer.
44
     *
45
     * @param string $queryString
46
     * @param string $callback
47
     * @return string
48
     */
49
    public function suggestAction($queryString, $callback)
50
    {
51
        $jsonPCallback = htmlspecialchars($callback);
52
        // Get suggestions
53
        $rawQuery = htmlspecialchars(mb_strtolower(trim($queryString)));
54
55
        try {
56
            /** @var SuggestService $suggestService */
57
            $suggestService = GeneralUtility::makeInstance(SuggestService::class, $this->typoScriptFrontendController, $this->searchService, $this->typoScriptConfiguration);
58
            $additionalFilters = htmlspecialchars(GeneralUtility::_GET('filters'));
59
60
            $pageId = $this->typoScriptFrontendController->getRequestedId();
61
            $languageId = $this->typoScriptFrontendController->sys_language_uid;
62
            $arguments = (array)$this->request->getArguments();
63
            $searchRequest = $this->getSearchRequestBuilder()->buildForSuggest($arguments, $rawQuery, $pageId, $languageId);
64
            $result = $suggestService->getSuggestions($searchRequest, $additionalFilters);
65
        } catch (SolrUnavailableException $e) {
66
            $this->handleSolrUnavailable();
67
            $result = ['status' => false];
68
        }
69
70
        return htmlspecialchars($jsonPCallback) . '(' . json_encode($result) . ')';
71
    }
72
73
    /**
74
     * Called when the solr server is unavailable.
75
     *
76
     * @return void
77
     */
78
    protected function handleSolrUnavailable()
79
    {
80
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
81
                /** @var SolrLogManager $logger */
82
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
83
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
84
        }
85
    }
86
87
}
88