Passed
Pull Request — main (#3444)
by Markus
29:09
created

SearchController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 68.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 80
c 1
b 0
f 0
dl 0
loc 205
ccs 57
cts 83
cp 0.6867
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 initializeView() 0 12 4
A getAdditionalFilters() 0 3 1
A solrNotAvailableAction() 0 4 1
A frequentlySearchedAction() 0 20 1
A detailAction() 0 13 3
A handleSolrUnavailable() 0 4 1
A formAction() 0 15 2
B resultsAction() 0 46 6
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
        if ($this->searchService === null) {
105 33
            return $this->handleSolrUnavailable();
106 33
        }
107 33
108 33
        try {
109
            $arguments = $this->request->getArguments();
110 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

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