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

SearchController::resultsAction()   B

Complexity

Conditions 6
Paths 41

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6.0852

Importance

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