Passed
Pull Request — main (#3261)
by Sebastian
48:30 queued 07:03
created

OptionsFacetParser::getOptionsFromSolrResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
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\ResultSet\Facets\OptionBased\Options;
19
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
21
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetParser;
22
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
23
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
26
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
27
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
28
29
/**
30
 * Class OptionsFacetParser
31
 */
32
class OptionsFacetParser extends AbstractFacetParser
33
{
34
    /**
35
     * @var Dispatcher|null
36
     */
37
    protected ?Dispatcher $dispatcher = null;
38
39
    /**
40
     * @param Dispatcher $dispatcher
41
     */
42 17
    public function injectDispatcher(Dispatcher $dispatcher)
43
    {
44 17
        $this->dispatcher = $dispatcher;
45
    }
46
47
    /**
48
     * @param SearchResultSet $resultSet
49
     * @param string $facetName
50
     * @param array $facetConfiguration
51
     * @return OptionsFacet|null
52
     * @throws InvalidSlotException
53
     * @throws InvalidSlotReturnException
54
     */
55 39
    public function parse(SearchResultSet $resultSet, string $facetName, array $facetConfiguration): ?AbstractFacet
56
    {
57 39
        $response = $resultSet->getResponse();
58 39
        $fieldName = $facetConfiguration['field'];
59 39
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
60 39
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
61 39
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
62 39
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
63 39
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
64 39
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
65 39
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
66 39
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
67
68 39
        if ($hasNoOptionsToShow && $hideEmpty) {
69 1
            return null;
70
        }
71
72
        /** @var $facet OptionsFacet */
73 39
        $facet = GeneralUtility::makeInstance(
74
            OptionsFacet::class,
75
            $resultSet,
76
            $facetName,
77
            $fieldName,
78
            $label,
79
            $facetConfiguration,
80 39
            $this->objectManager
81
        );
82
83 39
        $hasActiveOptions = count($optionsFromRequest) > 0;
84 39
        $facet->setIsUsed($hasActiveOptions);
85 39
        $facet->setIsAvailable($hasOptionsInResponse);
86
87 39
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
88 39
        foreach ($optionsToCreate as $optionsValue => $count) {
89 37
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
90 1
                continue;
91
            }
92
93 37
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
94 37
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
95 37
            $facet->addOption(
96 37
                GeneralUtility::makeInstance(
97
                    Option::class,
98
                    $facet,
99
                    $label,
100
                    $optionsValue,
101
                    $count,
102
                    $isOptionsActive,
103 37
                    ($metricsFromSolrResponse[$optionsValue] ?? [])
104
                )
105
            );
106
        }
107
108
        // after all options have been created we apply a manualSortOrder if configured
109
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
110
        // need to be handled in the frontend.
111 39
        $this->applyManualSortOrder($facet, $facetConfiguration);
112 39
        $this->applyReverseOrder($facet, $facetConfiguration);
113
114 39
        if (!is_null($this->dispatcher)) {
115 17
            $this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
116
        }
117
118 39
        return $facet;
119
    }
120
121
    /**
122
     * @param string $facetName
123
     * @param ResponseAdapter $response
124
     * @return array
125
     */
126 39
    protected function getOptionsFromSolrResponse(string $facetName, ResponseAdapter $response): array
127
    {
128 39
        $optionsFromSolrResponse = [];
129 39
        if (!isset($response->facets->{$facetName})) {
130 10
            return $optionsFromSolrResponse;
131
        }
132
133 35
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
134 35
            $optionValue = $bucket->val;
135 35
            $optionCount = $bucket->count;
136 35
            $optionsFromSolrResponse[$optionValue] = $optionCount;
137
        }
138
139 35
        return $optionsFromSolrResponse;
140
    }
141
142
    /**
143
     * @param string $facetName
144
     * @param ResponseAdapter $response
145
     * @return array
146
     */
147 39
    protected function getMetricsFromSolrResponse(string $facetName, ResponseAdapter $response): array
148
    {
149 39
        $metricsFromSolrResponse = [];
150
151 39
        if (!isset($response->facets->{$facetName}->buckets)) {
152 10
            return [];
153
        }
154
155 35
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
156 35
            $bucketVariables = get_object_vars($bucket);
157 35
            foreach ($bucketVariables as $key => $value) {
158 35
                if (strpos($key, 'metrics_') === 0) {
159 1
                    $metricsKey = str_replace('metrics_', '', $key);
160 1
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
161
                }
162
            }
163
        }
164
165 35
        return $metricsFromSolrResponse;
166
    }
167
}
168