Passed
Push — master ( 203e1d...831cab )
by Timo
27:25
created

SearchController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 66.99%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 7
dl 0
loc 175
ccs 69
cts 103
cp 0.6699
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAction() 0 9 2
A initializeView() 0 10 3
A getCustomTemplateFromConfiguration() 0 6 1
A resultsAction() 0 21 2
A buildSearchRequest() 0 15 2
A adjustPageArgumentToPositiveInteger() 0 7 2
A getRequest() 0 10 1
A formAction() 0 9 1
A frequentlySearchedAction() 0 15 1
A detailAction() 0 9 2
A solrNotAvailableAction() 0 6 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
20
use TYPO3\CMS\Extbase\Mvc\Web\Response;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Fluid\View\TemplateView;
23
24
/**
25
 * Class SearchController
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 * @package ApacheSolrForTypo3\Solr\Controller
30
 */
31
class SearchController extends AbstractBaseController
32
{
33
    /**
34
     * @var TemplateView
35
     */
36
    protected $view;
37
38
    /**
39
     * Provide search query in extbase arguments.
40
     */
41 29
    protected function initializeAction()
42
    {
43 29
        parent::initializeAction();
44
45 29
        $query = GeneralUtility::_GET('q');
46 29
        if ($query !== null) {
47 22
            $this->request->setArgument('q', $query);
48
        }
49 29
    }
50
51
    /**
52
     * @param ViewInterface $view
53
     */
54 29
    public function initializeView(ViewInterface $view)
55
    {
56 29
        if($view instanceof TemplateView) {
57 29
            $customTemplate = $this->getCustomTemplateFromConfiguration();
58 29
            if($customTemplate === '') {
59 28
                return;
60
            }
61 1
            $view->setTemplatePathAndFilename($customTemplate);
62
        }
63 1
    }
64
65
    /**
66
     * @return string
67
     */
68 29
    protected function getCustomTemplateFromConfiguration()
69
    {
70 29
        $templateKey = str_replace('Action', '', $this->actionMethodName);
71 29
        $customTemplate = $this->typoScriptConfiguration->getTemplateByFileKey($templateKey);
72 29
        return $customTemplate;
73
    }
74
75
    /**
76
     * Results
77
     */
78 24
    public function resultsAction()
79
    {
80 24
        if (!$this->searchService->getIsSolrAvailable()) {
81
            $this->forward('solrNotAvailable');
82
        }
83
84 24
        $searchRequest = $this->buildSearchRequest();
85 24
        $searchResultSet = $this->searchService->search($searchRequest);
86
87
        // we pass the search result set to the controller context, to have the possibility
88
        // to access it without passing it from partial to partial
89 24
        $this->controllerContext->setSearchResultSet($searchResultSet);
90
91 24
        $this->view->assignMultiple(
92
            [
93 24
                'hasSearched' => $this->searchService->getHasSearched(),
94 24
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
95 24
                'resultSet' => $searchResultSet
96
            ]
97
        );
98 24
    }
99
100
    /**
101
     * @return SearchRequest
102
     */
103 24
    protected function buildSearchRequest()
104
    {
105 24
        $rawUserQuery = null;
106 24
        if ($this->request->hasArgument('q')) {
107 22
            $rawUserQuery = $this->request->getArgument('q');
108
        }
109
110 24
        $arguments = (array)$this->request->getArguments();
111 24
        $arguments = $this->adjustPageArgumentToPositiveInteger($arguments);
112
113
        /** @var $searchRequest SearchRequest */
114 24
        $searchRequest = $this->getRequest(['q' => $rawUserQuery, 'tx_solr' => $arguments]);
115
116 24
        return $searchRequest;
117
    }
118
119
    /**
120
     * This methods sets the page argument to an expected positive integer value in the arguments array.
121
     *
122
     * @param array $arguments
123
     * @return array
124
     */
125 24
    protected function adjustPageArgumentToPositiveInteger(array $arguments)
126
    {
127 24
        $page = isset($arguments['page']) ? intval($arguments['page']) - 1 : 0;
128 24
        $arguments['page'] = max($page, 0);
129
130 24
        return $arguments;
131
    }
132
133
    /**
134
     * @param array $requestArguments
135
     * @return SearchRequest
136
     */
137 25
    private function getRequest(array $requestArguments = [])
138
    {
139 25
        $searchRequest = GeneralUtility::makeInstance(
140 25
            SearchRequest::class,
141
            $requestArguments,
142 25
            $this->typoScriptFrontendController->getRequestedId(),
143 25
            $this->typoScriptFrontendController->sys_language_uid,
144 25
            $this->typoScriptConfiguration);
145 25
        return $searchRequest;
146
    }
147
148
    /**
149
     * Form
150
     */
151 2
    public function formAction()
152
    {
153 2
        $this->view->assignMultiple(
154
            [
155 2
                'search' => $this->searchService->getSearch(),
156 2
                'additionalFilters' => $this->searchService->getAdditionalFilters()
157
            ]
158
        );
159 2
    }
160
161
    /**
162
     * Frequently Searched
163
     */
164 1
    public function frequentlySearchedAction()
165
    {
166
        /** @var  $searchResultSet SearchResultSet */
167 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
168 1
        $searchResultSet->setUsedSearchRequest($this->getRequest());
169 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
170
171 1
        $this->view->assignMultiple(
172
            [
173 1
                'hasSearched' => $this->searchService->getHasSearched(),
174 1
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
175 1
                'resultSet' => $searchResultSet
176
            ]
177
        );
178 1
    }
179
180
    /**
181
     * This action allows to render a detailView with data from solr.
182
     *
183
     * @param string $documentId
184
     */
185 1
    public function detailAction($documentId = '')
186
    {
187 1
        if (!$this->searchService->getIsSolrAvailable()) {
188
            $this->forward('solrNotAvailable');
189
        }
190
191 1
        $document = $this->searchService->getDocumentById($documentId);
192 1
        $this->view->assign('document', $document);
193 1
    }
194
195
    /**
196
     * Rendered when no search is available.
197
     * @return string
198
     */
199 1
    public function solrNotAvailableAction()
200
    {
201 1
        if ($this->response instanceof Response) {
202 1
            $this->response->setStatus(503);
203
        }
204 1
    }
205
}
206