Passed
Branch main (7dbc49)
by Rafael
04:08
created

SearchController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 65.79%

Importance

Changes 0
Metric Value
wmc 20
eloc 72
c 0
b 0
f 0
dl 0
loc 187
ccs 50
cts 76
cp 0.6579
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAction() 0 4 1
A getCustomTemplateFromConfiguration() 0 4 1
A mapGlobalQueryStringWhenEnabled() 0 8 3
A solrNotAvailableAction() 0 4 1
A frequentlySearchedAction() 0 20 1
A detailAction() 0 9 2
A handleSolrUnavailable() 0 4 1
A getAdditionalFilters() 0 3 1
A initializeView() 0 12 4
A formAction() 0 11 1
A resultsAction() 0 36 4
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\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\Pagination\ResultsPagination;
20
use ApacheSolrForTypo3\Solr\Pagination\ResultsPaginator;
21
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
22
use ApacheSolrForTypo3\Solr\Util;
23
use Psr\Http\Message\ResponseInterface;
24
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Http\ForwardResponse;
27
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
28
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
29
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
30
use TYPO3\CMS\Fluid\View\TemplateView;
31
use TYPO3Fluid\Fluid\View\ViewInterface;
32
33
/**
34
 * Class SearchController
35
 *
36
 * @author Frans Saris <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 */
39
class SearchController extends AbstractBaseController
40
{
41
    /**
42
     * @var TemplateView
43
     */
44
    protected $view;
45
46
    /**
47
     * Provide search query in extbase arguments.
48
     */
49 35
    protected function initializeAction()
50
    {
51 35
        parent::initializeAction();
52 35
        $this->mapGlobalQueryStringWhenEnabled();
53
    }
54
55 35
    protected function mapGlobalQueryStringWhenEnabled()
56
    {
57 35
        $query = GeneralUtility::_GET('q');
58
59 35
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
0 ignored issues
show
Bug introduced by
The method getSearchIgnoreGlobalQParameter() 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

59
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->/** @scrutinizer ignore-call */ getSearchIgnoreGlobalQParameter();

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...
60
61 35
        if ($useGlobalQueryString) {
62 1
            $this->request->setArgument('q', $query);
63
        }
64
    }
65
66
    /**
67
     * @param ViewInterface $view
68
     */
69 35
    public function initializeView($view)
70
    {
71 35
        if ($view instanceof TemplateView) {
72 35
            $customTemplate = $this->getCustomTemplateFromConfiguration();
73 35
            if ($customTemplate === '') {
74 34
                return;
75
            }
76
77 1
            if (strpos($customTemplate, 'EXT:') !== false) {
78 1
                $view->setTemplatePathAndFilename($customTemplate);
79
            } else {
80
                $view->setTemplate($customTemplate);
81
            }
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88 35
    protected function getCustomTemplateFromConfiguration(): string
89
    {
90 35
        $templateKey = str_replace('Action', '', $this->actionMethodName);
91 35
        return $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
92
    }
93
94
    /**
95
     * Results
96
     * @return ResponseInterface
97
     * @throws AspectNotFoundException
98
     * @throws NoSuchArgumentException
99
     * @throws InvalidSlotException
100
     * @throws InvalidSlotReturnException
101
     */
102 33
    public function resultsAction(): ResponseInterface
103
    {
104
        try {
105 33
            $arguments = $this->request->getArguments();
106 33
            $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

106
            /** @scrutinizer ignore-call */ 
107
            $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...
107 33
            $languageId = Util::getLanguageUid();
108 33
            $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
109
110 33
            $searchResultSet = $this->searchService->search($searchRequest);
0 ignored issues
show
Bug introduced by
The method search() 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

110
            /** @scrutinizer ignore-call */ 
111
            $searchResultSet = $this->searchService->search($searchRequest);

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...
111
112
            // we pass the search result set to the controller context, to have the possibility
113
            // to access it without passing it from partial to partial
114 33
            $this->controllerContext->setSearchResultSet($searchResultSet);
115
116 33
            $currentPage = $this->request->hasArgument('page') ? (int)$this->request->getArgument('page') : 1;
117 33
            $itemsPerPage = ($searchResultSet->getUsedResultsPerPage() ?: $this->typoScriptConfiguration->getSearchResultsPerPage(10));
118 33
            $paginator = GeneralUtility::makeInstance(ResultsPaginator::class, $searchResultSet, $currentPage, $itemsPerPage);
119 33
            $pagination = GeneralUtility::makeInstance(ResultsPagination::class, $paginator);
120 33
            $pagination->setMaxPageNumbers((int)$this->typoScriptConfiguration->getMaxPaginatorLinks(0));
121
122 33
            $values = [
123 33
                'additionalFilters' => $this->getAdditionalFilters(),
124
                'resultSet' => $searchResultSet,
125 33
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
126
                'arguments' => $arguments,
127
                'pagination' => $pagination,
128
                'currentPage' => $currentPage,
129
            ];
130
131 33
            $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
132
133 33
            $this->view->assignMultiple($values);
134
        } catch (SolrUnavailableException $e) {
135
            return $this->handleSolrUnavailable();
136
        }
137 33
        return $this->htmlResponse();
138
    }
139
140
    /**
141
     * Form
142
     */
143 1
    public function formAction(): ResponseInterface
144
    {
145 1
        $values = [
146 1
            'search' => $this->searchService->getSearch(),
147 1
            'additionalFilters' => $this->getAdditionalFilters(),
148 1
            'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
149
        ];
150 1
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
151
152 1
        $this->view->assignMultiple($values);
153 1
        return $this->htmlResponse();
154
    }
155
156
    /**
157
     * Frequently Searched
158
     */
159
    public function frequentlySearchedAction(): ResponseInterface
160
    {
161
        /** @var  $searchResultSet SearchResultSet */
162
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
163
164
        $pageId = $this->typoScriptFrontendController->getRequestedId();
165
        $languageId = Util::getLanguageUid();
166
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
167
        $searchResultSet->setUsedSearchRequest($searchRequest);
168
169
        $this->controllerContext->setSearchResultSet($searchResultSet);
170
171
        $values = [
172
            'additionalFilters' => $this->getAdditionalFilters(),
173
            'resultSet' => $searchResultSet,
174
        ];
175
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
176
177
        $this->view->assignMultiple($values);
178
        return $this->htmlResponse();
179
    }
180
181
    /**
182
     * This action allows to render a detailView with data from solr.
183
     *
184
     * @param string $documentId
185
     * @return ResponseInterface
186
     */
187 1
    public function detailAction(string $documentId = ''): ResponseInterface
188
    {
189
        try {
190 1
            $document = $this->searchService->getDocumentById($documentId);
191 1
            $this->view->assign('document', $document);
192
        } catch (SolrUnavailableException $e) {
193
            return $this->handleSolrUnavailable();
194
        }
195 1
        return $this->htmlResponse();
196
    }
197
198
    /**
199
     * Rendered when no search is available.
200
     * @return ResponseInterface
201
     */
202
    public function solrNotAvailableAction(): ResponseInterface
203
    {
204
        return $this->htmlResponse()
205
            ->withStatus(503, self::STATUS_503_MESSAGE);
206
    }
207
208
    /**
209
     * Called when the solr server is unavailable.
210
     */
211
    protected function handleSolrUnavailable(): ResponseInterface
212
    {
213
        parent::logSolrUnavailable();
214
        return new ForwardResponse('solrNotAvailable');
215
    }
216
217
    /**
218
     * This method can be overwritten to add additionalFilters for the autosuggest.
219
     * By default, suggest controller will apply the configured filters from the typoscript configuration.
220
     *
221
     * @return array
222
     */
223 34
    protected function getAdditionalFilters(): array
224
    {
225 34
        return [];
226
    }
227
}
228