Passed
Push — release-11.5.x ( 39fc07...8ccd81 )
by Markus
34:52 queued 29:33
created

SuggestController::handleSolrUnavailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Controller;
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\Suggest\SuggestService;
19
20
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
21
use ApacheSolrForTypo3\Solr\Util;
22
use Psr\Http\Message\ResponseInterface;
23
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Class SuggestController
28
 *
29
 * @author Frans Saris <[email protected]>
30
 * @author Timo Hund <[email protected]>
31
 * @copyright (c) 2017 Timo Hund <[email protected]>
32
 */
33
class SuggestController extends AbstractBaseController
34
{
35
    /**
36
     * This method creates a suggest json response that can be used in a suggest layer.
37
     *
38
     * @param string $queryString
39
     * @param string|null $callback
40
     * @param array|null $additionalFilters
41
     * @return ResponseInterface
42
     * @throws AspectNotFoundException
43
     */
44 3
    public function suggestAction(string $queryString, ?string $callback = null, ?array $additionalFilters = []): ResponseInterface
45
    {
46
        // Get suggestions
47 3
        $rawQuery = htmlspecialchars(mb_strtolower(trim($queryString)));
48
49 3
        if ($this->searchService === null) {
50
            return $this->handleSolrUnavailable();
51
        }
52
53
        try {
54
            /** @var SuggestService $suggestService */
55 3
            $suggestService = GeneralUtility::makeInstance(
56 3
                SuggestService::class,
57
                /** @scrutinizer ignore-type */
58 3
                $this->typoScriptFrontendController,
59
                /** @scrutinizer ignore-type */
60 3
                $this->searchService,
61
                /** @scrutinizer ignore-type */
62 3
                $this->typoScriptConfiguration
63 3
            );
64
65 3
            $additionalFilters = is_array($additionalFilters) ? array_map('htmlspecialchars', $additionalFilters) : [];
0 ignored issues
show
introduced by
The condition is_array($additionalFilters) is always true.
Loading history...
66 3
            $pageId = $this->typoScriptFrontendController->getRequestedId();
0 ignored issues
show
Bug introduced by
The method getRequestedId() does not exist on null. ( Ignorable by Annotation )

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

66
            /** @scrutinizer ignore-call */ 
67
            $pageId = $this->typoScriptFrontendController->getRequestedId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67 3
            $languageId = Util::getLanguageUid();
68 3
            $arguments = $this->request->getArguments();
69
70 3
            $searchRequest = $this->getSearchRequestBuilder()->buildForSuggest($arguments, $rawQuery, $pageId, $languageId);
71 3
            $result = $suggestService->getSuggestions($searchRequest, $additionalFilters);
72
        } catch (SolrUnavailableException $e) {
73
            return $this->handleSolrUnavailable();
74
        }
75 3
        if ($callback) {
76 2
            return $this->htmlResponse(htmlspecialchars($callback) . '(' . json_encode($result, JSON_UNESCAPED_SLASHES) . ')');
77
        }
78 1
        return $this->htmlResponse(json_encode($result, JSON_UNESCAPED_SLASHES));
79
    }
80
81
    private function handleSolrUnavailable(): ResponseInterface
82
    {
83
        $this->logSolrUnavailable();
84
        $result = ['status' => false];
85
        return $this->htmlResponse(json_encode($result, JSON_UNESCAPED_SLASHES))->withStatus(503, self::STATUS_503_MESSAGE);
86
    }
87
}
88