Passed
Push — master ( 4a930e...4b35d1 )
by Timo
22:47
created

applyRequirements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet;
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\ResultSet\Facets\AbstractFacet;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\FacetRegistry;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RequirementsService;
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Sorting\Sorting;
21
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Spellchecking\Suggestion;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Object\ObjectManager;
24
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
25
26
/**
27
 * This processor is used to transform the solr response into a
28
 * domain object hierarchy that can be used in the application (controller and view).
29
 *
30
 * @author Frans Saris <[email protected]>
31
 * @author Timo Hund <[email protected]>
32
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet
33
 */
34
class ResultSetReconstitutionProcessor implements SearchResultSetProcessor
35
{
36
    /**
37
     * @var ObjectManagerInterface
38
     */
39
    protected $objectManager;
40
41
    /**
42
     * @return ObjectManagerInterface
43
     */
44 61
    public function getObjectManager()
45
    {
46 61
        if ($this->objectManager === null) {
47 33
            $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
48
        }
49 61
        return $this->objectManager;
50
    }
51
52
    /**
53
     * @param ObjectManagerInterface $objectManager
54
     */
55 28
    public function setObjectManager($objectManager)
56
    {
57 28
        $this->objectManager = $objectManager;
58 28
    }
59
60
61
    /**
62
     * @return FacetRegistry
63
     */
64 61
    protected function getFacetRegistry()
65
    {
66 61
        return $this->getObjectManager()->get(FacetRegistry::class);
67
    }
68
69
    /**
70
     * The implementation can be used to influence a SearchResultSet that is
71
     * created and processed in the SearchResultSetService.
72
     *
73
     * @param SearchResultSet $resultSet
74
     * @return SearchResultSet
75
     */
76 72
    public function process(SearchResultSet $resultSet)
77
    {
78 72
        if (!$resultSet instanceof SearchResultSet) {
0 ignored issues
show
introduced by
$resultSet is always a sub-type of ApacheSolrForTypo3\Solr\...sultSet\SearchResultSet.
Loading history...
79
            return $resultSet;
80
        }
81
82 72
        $resultSet = $this->parseSpellCheckingResponseIntoObjects($resultSet);
83 72
        $resultSet = $this->parseSortingIntoObjects($resultSet);
84
85
        // here we can reconstitute other domain objects from the solr response
86 72
        $resultSet = $this->parseFacetsIntoObjects($resultSet);
87
88 72
        return $resultSet;
89
    }
90
91
    /**
92
     * @param SearchResultSet $resultSet
93
     * @return SearchResultSet
94
     */
95 72
    protected function parseSortingIntoObjects(SearchResultSet $resultSet)
96
    {
97 72
        $configuration = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration();
98 72
        $hasSorting = $resultSet->getUsedSearchRequest()->getHasSorting();
99 72
        $activeSortingName = $resultSet->getUsedSearchRequest()->getSortingName();
100 72
        $activeSortingDirection = $resultSet->getUsedSearchRequest()->getSortingDirection();
101
102
        // no configuration available
103 72
        if (!isset($configuration)) {
104 7
            return $resultSet;
105
        }
106
107
        // no sorting enabled
108 65
        if (!$configuration->getSearchSorting()) {
109 31
            return $resultSet;
110
        }
111 34
        foreach ($configuration->getSearchSortingOptionsConfiguration() as $sortingKeyName => $sortingOptions) {
112 34
            $sortingName = rtrim($sortingKeyName, '.');
113 34
            $selected = false;
114 34
            $direction = $configuration->getSearchSortingDefaultOrderBySortOptionName($sortingName);
115
116
            // when we have an active sorting in the request we compare the sortingName and mark is as active and
117
            // use the direction from the request
118 34
            if ($hasSorting && $activeSortingName == $sortingName) {
119 1
                $selected = true;
120 1
                $direction = $activeSortingDirection;
121
            }
122
123 34
            $field = $sortingOptions['field'];
124 34
            $label = $sortingOptions['label'];
125
126 34
            $isResetOption = $field === 'relevance';
127
            // @todo allow stdWrap on label
128 34
            $sorting = new Sorting($resultSet, $sortingName, $field, $direction, $label, $selected, $isResetOption);
129 34
            $resultSet->addSorting($sorting);
130
        }
131
132 34
        return $resultSet;
133
    }
134
135
    /**
136
     * @param SearchResultSet $resultSet
137
     * @return SearchResultSet
138
     */
139 72
    private function parseSpellCheckingResponseIntoObjects(SearchResultSet $resultSet)
140
    {
141
        //read the response
142 72
        $response = $resultSet->getResponse();
143
144 72
        if (!is_array($response->spellcheck->suggestions)) {
145 64
            return $resultSet;
146
        }
147
148 8
        $misspelledTerm = '';
149 8
        foreach ($response->spellcheck->suggestions as $key => $suggestionData) {
150 5
            if (is_string($suggestionData)) {
151 5
                $misspelledTerm = $key;
152 5
                continue;
153
            }
154
155 5
            if ($misspelledTerm === '') {
156
                throw new \UnexpectedValueException('No missspelled term before suggestion');
157
            }
158
159 5
            if (!is_object($suggestionData) && !is_array($suggestionData->suggestion)) {
160
                continue;
161
            }
162
163 5
            foreach ($suggestionData->suggestion as $suggestedTerm) {
164 5
                $suggestion = $this->createSuggestionFromResponseFragment($suggestionData, $suggestedTerm, $misspelledTerm);
165
                //add it to the resultSet
166 5
                $resultSet->addSpellCheckingSuggestion($suggestion);
167
            }
168
169
        }
170
171 8
        return $resultSet;
172
    }
173
174
    /**
175
     * @param \stdClass $suggestionData
176
     * @param string $suggestedTerm
177
     * @param string $misspelledTerm
178
     * @return Suggestion
179
     */
180 5
    private function createSuggestionFromResponseFragment($suggestionData, $suggestedTerm, $misspelledTerm)
181
    {
182 5
        $numFound = isset($suggestionData->numFound) ? $suggestionData->numFound : 0;
183 5
        $startOffset = isset($suggestionData->startOffset) ? $suggestionData->startOffset : 0;
184 5
        $endOffset = isset($suggestionData->endOffset) ? $suggestionData->endOffset : 0;
185
186
        // by now we avoid to use GeneralUtility::makeInstance, since we only create a value object
187
        // and the usage might be a overhead.
188 5
        $suggestion = new Suggestion($suggestedTerm, $misspelledTerm, $numFound, $startOffset, $endOffset);
189 5
        return $suggestion;
190
    }
191
192
    /**
193
     * Parse available facets into objects
194
     *
195
     * @param SearchResultSet $resultSet
196
     * @return SearchResultSet
197
     */
198 72
    private function parseFacetsIntoObjects(SearchResultSet $resultSet)
199
    {
200
        // Make sure we can access the facet configuration
201 72
        if (!$resultSet->getUsedSearchRequest() || !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()) {
202 7
            return $resultSet;
203
        }
204
205
        // Read the response
206 65
        $response = $resultSet->getResponse();
207 65
        if (!is_object($response->facet_counts) && !is_object($response->facets)) {
208 4
            return $resultSet;
209
        }
210
211
        /** @var FacetRegistry $facetRegistry */
212 61
        $facetRegistry = $this->getFacetRegistry();
213 61
        $facetsConfiguration = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingFacets();
214
215 61
        foreach ($facetsConfiguration as $name => $options) {
216 59
            if (!is_array($options)) {
217
                continue;
218
            }
219 59
            $facetName = rtrim($name, '.');
220 59
            $type = !empty($options['type']) ? $options['type'] : '';
221
222 59
            $parser = $facetRegistry->getPackage($type)->getParser();
223 59
            $facet = $parser->parse($resultSet, $facetName, $options);
224 59
            if ($facet !== null) {
225 59
                $resultSet->addFacet($facet);
226
            }
227
        }
228
229 61
        $this->applyRequirements($resultSet);
230
231 61
        return $resultSet;
232
    }
233
234
    /**
235
     * @param SearchResultSet $resultSet
236
     */
237 61
    protected function applyRequirements(SearchResultSet $resultSet)
238
    {
239 61
        $requirementsService = $this->getRequirementsService();
240 61
        $facets = $resultSet->getFacets();
241 61
        foreach ($facets as $facet) {
242
            /** @var $facet AbstractFacet */
243 57
            $requirementsMet = $requirementsService->getAllRequirementsMet($facet);
244 57
            $facet->setAllRequirementsMet($requirementsMet);
245
        }
246 61
    }
247
248
    /**
249
     * @return RequirementsService
250
     */
251 61
    protected function getRequirementsService()
252
    {
253 61
        return $this->getObjectManager()->get(RequirementsService::class);
254
    }
255
}
256