Passed
Pull Request — release-11.5.x (#3230)
by Markus
41:39 queued 17:21
created

SearchRequestBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 95.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 47
c 1
b 0
f 0
dl 0
loc 164
ccs 43
cts 45
cp 0.9556
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequest() 0 12 1
A __construct() 0 4 1
A adjustPageArgumentToPositiveInteger() 0 5 2
A buildForFrequentSearches() 0 4 1
A shouldHideResultsFromInitialSearch() 0 7 5
A buildForSearch() 0 8 1
A buildForSuggest() 0 17 1
A applyPassedResultsPerPage() 0 29 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Domain\Search;
19
20
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
21
use ApacheSolrForTypo3\Solr\System\Session\FrontendUserSession;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * The SearchRequestBuilder is responsible to build a valid SearchRequest.
26
 * @author Timo Hund <[email protected]>
27
 */
28
class SearchRequestBuilder
29
{
30
    /**
31
     * @var TypoScriptConfiguration
32
     */
33
    protected TypoScriptConfiguration $typoScriptConfiguration;
34
35
    /**
36
     * @var FrontendUserSession
37
     */
38
    protected FrontendUserSession $session;
39
40
    /**
41
     * SearchRequestBuilder constructor.
42
     * @param TypoScriptConfiguration $typoScriptConfiguration
43
     * @param FrontendUserSession|null $frontendUserSession
44
     */
45 38
    public function __construct(TypoScriptConfiguration $typoScriptConfiguration, FrontendUserSession $frontendUserSession = null)
46
    {
47 38
        $this->typoScriptConfiguration = $typoScriptConfiguration;
48 38
        $this->session = $frontendUserSession ?? GeneralUtility::makeInstance(FrontendUserSession::class);
49
    }
50
51
    /**
52
     * @param array $controllerArguments
53
     * @param int $pageId
54
     * @param int $languageId
55
     * @return SearchRequest
56
     */
57 35
    public function buildForSearch(array $controllerArguments, int $pageId, int $languageId): SearchRequest
58
    {
59 35
        $controllerArguments = $this->adjustPageArgumentToPositiveInteger($controllerArguments);
60
61
        /* @var SearchRequest $searchRequest */
62 35
        $argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace();
63 35
        $searchRequest = $this->getRequest([$argumentsNamespace => $controllerArguments], $pageId, $languageId);
64 35
        return $this->applyPassedResultsPerPage($searchRequest);
65
    }
66
67
    /**
68
     * Checks if the passed resultsPerPageValue is valid and applies it. If the perPage value was changed it is stored in
69
     * the session and the current page is set to 0, since the pagination should start from the beginning then.
70
     *
71
     * @param SearchRequest $searchRequest
72
     * @return SearchRequest
73
     */
74 35
    protected function applyPassedResultsPerPage(SearchRequest $searchRequest): SearchRequest
75
    {
76 35
        $requestedPerPage = $searchRequest->getResultsPerPage();
77
78 35
        $perPageSwitchOptions = $this->typoScriptConfiguration->getSearchResultsPerPageSwitchOptionsAsArray();
79 35
        if (isset($requestedPerPage) && in_array($requestedPerPage, $perPageSwitchOptions)) {
80 2
            $this->session->setPerPage($requestedPerPage);
81 2
            $searchRequest->setPage(0);
82
        }
83
84 35
        $defaultResultsPerPage = $this->typoScriptConfiguration->getSearchResultsPerPage();
85 35
        $currentNumberOfResultsShown = $defaultResultsPerPage;
86 35
        if ($this->session->getHasPerPage()) {
87 1
            $sessionResultPerPage = $this->session->getPerPage();
88 1
            if (in_array($sessionResultPerPage, $perPageSwitchOptions)) {
89 1
                $currentNumberOfResultsShown = (int)$sessionResultPerPage;
90
            }
91
        }
92
93 35
        if ($this->shouldHideResultsFromInitialSearch($searchRequest)) {
94
            // initialize search with an empty query, which would by default return all documents
95
            // anyway, tell Solr to not return any result documents
96
            // Solr will still return facets though
97 1
            $currentNumberOfResultsShown = 0;
98
        }
99
100 35
        $searchRequest->setResultsPerPage($currentNumberOfResultsShown);
101
102 35
        return $searchRequest;
103
    }
104
105
    /**
106
     * Checks it the results should be hidden in the response.
107
     *
108
     * @param SearchRequest $searchRequest
109
     * @return bool
110
     */
111 35
    protected function shouldHideResultsFromInitialSearch(SearchRequest $searchRequest): bool
112
    {
113 35
        return ($this->typoScriptConfiguration->getSearchInitializeWithEmptyQuery() ||
114 35
            $this->typoScriptConfiguration->getSearchInitializeWithQuery()) &&
115 35
            !$this->typoScriptConfiguration->getSearchShowResultsOfInitialEmptyQuery() &&
116 35
            !$this->typoScriptConfiguration->getSearchShowResultsOfInitialQuery() &&
117 35
            $searchRequest->getRawUserQueryIsNull();
118
    }
119
120
    /**
121
     * @param int $pageId
122
     * @param int $languageId
123
     * @return SearchRequest
124
     */
125
    public function buildForFrequentSearches(int $pageId, int $languageId): SearchRequest
126
    {
127
        /* @var SearchRequest $searchRequest */
128
        return $this->getRequest([], $pageId, $languageId);
129
    }
130
131
    /**
132
     * @param array $controllerArguments
133
     * @param string $rawUserQuery
134
     * @param int $pageId
135
     * @param int $languageId
136
     * @return SearchRequest
137
     */
138 3
    public function buildForSuggest(
139
        array $controllerArguments,
140
        string $rawUserQuery,
141
        int $pageId,
142
        int $languageId
143
    ): SearchRequest {
144 3
        $controllerArguments['page'] = 0;
145 3
        $controllerArguments['q'] = $rawUserQuery;
146 3
        $argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace();
147
148 3
        return $this->getRequest(
149
            [
150 3
                'q' => $rawUserQuery,
151
                $argumentsNamespace => $controllerArguments,
152
            ],
153
            $pageId,
154
            $languageId
155
        );
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 38
    protected function getRequest(array $requestArguments = [], int $pageId = 0, int $languageId = 0): SearchRequest
167
    {
168 38
        return GeneralUtility::makeInstance(
169
            SearchRequest::class,
170
            /** @scrutinizer ignore-type */
171
            $requestArguments,
172
            /** @scrutinizer ignore-type */
173
            $pageId,
174
            /** @scrutinizer ignore-type */
175
            $languageId,
176
            /** @scrutinizer ignore-type */
177 38
            $this->typoScriptConfiguration
178
        );
179
    }
180
181
    /**
182
     * This method sets the page argument to an expected positive integer value in the arguments array.
183
     *
184
     * @param array $arguments
185
     * @return array
186
     */
187 35
    protected function adjustPageArgumentToPositiveInteger(array $arguments): array
188
    {
189 35
        $page = isset($arguments['page']) ? (int)($arguments['page']) : 0;
190 35
        $arguments['page'] = max($page, 0);
191 35
        return $arguments;
192
    }
193
}
194