Passed
Push — master ( a95893...a4d2df )
by Timo
57s
created

SearchController::solrNotAvailableAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2.1481
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller\Frontend;
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\Web\Response;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * Class SearchController
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 * @package ApacheSolrForTypo3\Solr\Controller
28
 */
29
class SearchController extends AbstractBaseController
30
{
31
    /**
32
     * @var \TYPO3\CMS\Fluid\View\TemplateView
33
     */
34
    protected $view;
35
36
    /**
37
     * Provide search query in extbase arguments.
38
     */
39 28
    protected function initializeAction()
40
    {
41 28
        parent::initializeAction();
42
43 28
        $query = GeneralUtility::_GET('q');
44 28
        if ($query !== null) {
45 21
            $this->request->setArgument('q', $query);
46
        }
47 28
    }
48
49
    /**
50
     * Results
51
     */
52 23
    public function resultsAction()
53
    {
54 23
        if (!$this->searchService->getIsSolrAvailable()) {
55
            $this->forward('solrNotAvailable');
56
        }
57
58 23
        $searchRequest = $this->buildSearchRequest();
59 23
        $searchResultSet = $this->searchService->search($searchRequest);
60
61
        // we pass the search result set to the controller context, to have the possibility
62
        // to access it without passing it from partial to partial
63 23
        $this->controllerContext->setSearchResultSet($searchResultSet);
64
65 23
        $this->view->assignMultiple(
66
            [
67 23
                'hasSearched' => $this->searchService->getHasSearched(),
68 23
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
69 23
                'resultSet' => $searchResultSet
70
            ]
71
        );
72 23
    }
73
74
    /**
75
     * @return SearchRequest
76
     */
77 23
    protected function buildSearchRequest()
78
    {
79 23
        $rawUserQuery = null;
80 23
        if ($this->request->hasArgument('q')) {
81 21
            $rawUserQuery = $this->request->getArgument('q');
82
        }
83
84 23
        $arguments = $this->request->getArguments();
85 23
        $page = isset($arguments['page']) ? $arguments['page'] - 1 : 0;
86 23
        $arguments['page'] = max($page, 0);
87
88
        /** @var $searchRequest SearchRequest */
89 23
        $searchRequest = $this->getRequest(['q' => $rawUserQuery, 'tx_solr' => $arguments]);
90
91 23
        return $searchRequest;
92
    }
93
94
    /**
95
     * @param array $requestArguments
96
     * @return SearchRequest
97
     */
98 24
    private function getRequest(array $requestArguments = [])
99
    {
100 24
        $searchRequest = GeneralUtility::makeInstance(
101 24
            SearchRequest::class,
102
            $requestArguments,
103 24
            $this->typoScriptFrontendController->getRequestedId(),
104 24
            $this->typoScriptFrontendController->sys_language_uid,
105 24
            $this->typoScriptConfiguration);
106 24
        return $searchRequest;
107
    }
108
109
    /**
110
     * Form
111
     */
112 2
    public function formAction()
113
    {
114 2
        $this->view->assignMultiple(
115
            [
116 2
                'search' => $this->searchService->getSearch(),
117 2
                'additionalFilters' => $this->searchService->getAdditionalFilters()
118
            ]
119
        );
120 2
    }
121
122
    /**
123
     * Frequently Searched
124
     */
125 1
    public function frequentlySearchedAction()
126
    {
127
        /** @var  $searchResultSet SearchResultSet */
128 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
129 1
        $searchResultSet->setUsedSearchRequest($this->getRequest());
130 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
131
132 1
        $this->view->assignMultiple(
133
            [
134 1
                'hasSearched' => $this->searchService->getHasSearched(),
135 1
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
136 1
                'resultSet' => $searchResultSet
137
            ]
138
        );
139 1
    }
140
141
    /**
142
     * This action allows to render a detailView with data from solr.
143
     *
144
     * @param string $documentId
145
     */
146 1
    public function detailAction($documentId = '')
147
    {
148 1
        if (!$this->searchService->getIsSolrAvailable()) {
149
            $this->forward('solrNotAvailable');
150
        }
151
152 1
        $document = $this->searchService->getDocumentById($documentId);
153 1
        $this->view->assign('document', $document);
154 1
    }
155
156
    /**
157
     * Rendered when no search is available.
158
     * @return string
159
     */
160 1
    public function solrNotAvailableAction()
161
    {
162 1
        if ($this->response instanceof Response) {
163 1
            $this->response->setStatus(503);
164
        }
165 1
    }
166
}
167