Passed
Push — master ( d4f966...dddccf )
by Rafael
34:04 queued 25:52
created

SearchRequestBuilder::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\System\Session\FrontendUserSession;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * The SearchRequestBuilder is responsible to build a valid SearchRequest.
34
 */
35
class SearchRequestBuilder
36
{
37
38
    /**
39
     * @var TypoScriptConfiguration
40
     */
41
    protected $typoScriptConfiguration;
42
43
    /**
44
     * @var FrontendUserSession
45
     */
46
    protected $session = null;
47
48
    /**
49
     * SearchRequestBuilder constructor.
50
     * @param TypoScriptConfiguration $typoScriptConfiguration
51
     * @param FrontendUserSession $frontendUserSession
52
     */
53 40
    public function __construct(TypoScriptConfiguration $typoScriptConfiguration, FrontendUserSession $frontendUserSession = null)
54
    {
55 40
        $this->typoScriptConfiguration = $typoScriptConfiguration;
56 40
        $this->session = $frontendUserSession ?? GeneralUtility::makeInstance(FrontendUserSession::class);
57 40
    }
58
59
    /**
60
     * @param array $controllerArguments
61
     * @param int $pageId
62
     * @param int $languageId
63
     * @return SearchRequest
64
     */
65 38
    public function buildForSearch(array $controllerArguments, $pageId, $languageId)
66
    {
67 38
        $controllerArguments = $this->adjustPageArgumentToPositiveInteger($controllerArguments);
68
69
        /** @var $searchRequest SearchRequest */
70 38
        $argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace();
71 38
        $searchRequest = $this->getRequest([$argumentsNamespace => $controllerArguments], $pageId, $languageId);
72 38
        $searchRequest = $this->applyPassedResultsPerPage($searchRequest);
73
74 38
        return $searchRequest;
75
    }
76
77
    /**
78
     * Checks if the passed resultsPerPageValue is valid and applies it. If the perPage value was changed it is stored in
79
     * the session and the current page is set to 0, since the pagination should start from the beginning then.
80
     *
81
     * @param SearchRequest $searchRequest
82
     * @return SearchRequest
83
     */
84 38
    protected function applyPassedResultsPerPage(SearchRequest $searchRequest)
85
    {
86 38
        $requestedPerPage = $searchRequest->getResultsPerPage();
87
88 38
        $perPageSwitchOptions = $this->typoScriptConfiguration->getSearchResultsPerPageSwitchOptionsAsArray();
89 38
        if (isset($requestedPerPage) && in_array($requestedPerPage, $perPageSwitchOptions)) {
90 5
            $this->session->setPerPage($requestedPerPage);
91 5
            $searchRequest->setPage(0);
92
        }
93
94 38
        $defaultResultsPerPage = $this->typoScriptConfiguration->getSearchResultsPerPage();
95 38
        $currentNumberOfResultsShown = $defaultResultsPerPage;
96 38
        if ($this->session->getHasPerPage()) {
97 4
            $sessionResultPerPage = $this->session->getPerPage();
98 4
            if (in_array($sessionResultPerPage, $perPageSwitchOptions)) {
99 4
                $currentNumberOfResultsShown = (int)$sessionResultPerPage;
100
            }
101
        }
102
103 38
        if ($this->shouldHideResultsFromInitialSearch($searchRequest)) {
104
            // initialize search with an empty query, which would by default return all documents
105
            // anyway, tell Solr to not return any result documents
106
            // Solr will still return facets though
107 2
            $currentNumberOfResultsShown = 0;
108
        }
109
110 38
        $searchRequest->setResultsPerPage($currentNumberOfResultsShown);
111
112 38
        return $searchRequest;
113
    }
114
115
    /**
116
     * Checks it the results should be hidden in the response.
117
     *
118
     * @param SearchRequest $searchRequest
119
     * @return bool
120
     */
121 38
    protected function shouldHideResultsFromInitialSearch(SearchRequest $searchRequest)
122
    {
123 38
        return ($this->typoScriptConfiguration->getSearchInitializeWithEmptyQuery() ||
124 38
            $this->typoScriptConfiguration->getSearchInitializeWithQuery()) &&
125 38
            !$this->typoScriptConfiguration->getSearchShowResultsOfInitialEmptyQuery() &&
126 38
            !$this->typoScriptConfiguration->getSearchShowResultsOfInitialQuery() &&
127 38
            $searchRequest->getRawUserQueryIsNull();
128
    }
129
130
    /**
131
     * @param int $pageId
132
     * @param int $languageId
133
     * @return SearchRequest
134
     */
135 1
    public function buildForFrequentSearches($pageId, $languageId)
136
    {
137
        /** @var $searchRequest SearchRequest */
138 1
        $searchRequest = $this->getRequest([], $pageId, $languageId);
139 1
        return $searchRequest;
140
    }
141
142
    /**
143
     * @param array $controllerArguments
144
     * @param string $rawUserQuery
145
     * @param int $pageId
146
     * @param int $languageId
147
     * @return SearchRequest
148
     */
149 1
    public function buildForSuggest(array $controllerArguments, $rawUserQuery, $pageId, $languageId)
150
    {
151 1
        $controllerArguments['page'] = 0;
152 1
        $controllerArguments['q'] = $rawUserQuery;
153 1
        $argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace();
154
155 1
        return $this->getRequest(['q' => $rawUserQuery, $argumentsNamespace => $controllerArguments], $pageId, $languageId);
156
    }
157
158
    /**
159
     * Creates an instance of the SearchRequest.
160
     *
161
     * @param array $requestArguments
162
     * @param int $pageId
163
     * @param int $languageId
164
     * @return SearchRequest
165
     */
166 40
    protected function getRequest(array $requestArguments = [], $pageId = 0, $languageId = 0)
167
    {
168 40
        $searchRequest = GeneralUtility::makeInstance(SearchRequest::class,
169 40
            /** @scrutinizer ignore-type */ $requestArguments,
170 40
            /** @scrutinizer ignore-type */ $pageId,
171 40
            /** @scrutinizer ignore-type */ $languageId,
172 40
            /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
173 40
        return $searchRequest;
174
    }
175
176
    /**
177
     * This methods sets the page argument to an expected positive integer value in the arguments array.
178
     *
179
     * @param array $arguments
180
     * @return array
181
     */
182 38
    protected function adjustPageArgumentToPositiveInteger(array $arguments)
183
    {
184 38
        $page = isset($arguments['page']) ? intval($arguments['page']) : 0;
185 38
        $arguments['page'] = max($page, 0);
186
187 38
        return $arguments;
188
    }
189
}
190