Passed
Push — task/2976_TYPO3.11_compatibili... ( d7dfd0...4be1cc )
by Rafael
03:40
created

OptionsFacetParser::getMetricsFromSolrResponse()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options;
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\AbstractFacetParser;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
22
23
/**
24
 * Class OptionsFacetParser
25
 */
26
class OptionsFacetParser extends AbstractFacetParser
27
{
28
    /**
29
     * @var Dispatcher
30
     */
31
    protected $dispatcher;
32
33
    /**
34
     * @param Dispatcher $dispatcher
35
     */
36 16
    public function injectDispatcher(Dispatcher $dispatcher)
37
    {
38 16
        $this->dispatcher = $dispatcher;
39 16
    }
40
41
    /**
42
     * @param SearchResultSet $resultSet
43
     * @param string $facetName
44
     * @param array $facetConfiguration
45
     * @return OptionsFacet|null
46
     */
47 38
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
48
    {
49 38
        $response = $resultSet->getResponse();
50 38
        $fieldName = $facetConfiguration['field'];
51 38
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
52 38
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
53 38
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
54 38
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
55 38
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
56 38
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
57 38
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
58 38
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
59
60 38
        if ($hasNoOptionsToShow && $hideEmpty) {
61 1
            return null;
62
        }
63
64
        /** @var $facet OptionsFacet */
65 38
        $facet = $this->objectManager->get(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
        $facet = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66 38
            OptionsFacet::class,
67
            $resultSet,
68
            $facetName,
69
            $fieldName,
70
            $label,
71
            $facetConfiguration
72
        );
73
74 38
        $hasActiveOptions = count($optionsFromRequest) > 0;
75 38
        $facet->setIsUsed($hasActiveOptions);
76 38
        $facet->setIsAvailable($hasOptionsInResponse);
77
78 38
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
79 38
        foreach ($optionsToCreate as $optionsValue => $count) {
80 36
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
81 1
                continue;
82
            }
83
84 36
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
85 36
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
86 36
            $facet->addOption($this->objectManager->get(Option::class, $facet, $label, $optionsValue, $count, $isOptionsActive, ($metricsFromSolrResponse[$optionsValue] ?? null)));
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

86
            $facet->addOption(/** @scrutinizer ignore-deprecated */ $this->objectManager->get(Option::class, $facet, $label, $optionsValue, $count, $isOptionsActive, ($metricsFromSolrResponse[$optionsValue] ?? null)));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
87
        }
88
89
        // after all options have been created we apply a manualSortOrder if configured
90
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
91
        // need to be handled in the frontend.
92 38
        $this->applyManualSortOrder($facet, $facetConfiguration);
93 38
        $this->applyReverseOrder($facet, $facetConfiguration);
94
95 38
        if(!is_null($this->dispatcher)) {
96 16
            $this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
97
        }
98
99 38
        return $facet;
100
    }
101
102
    /**
103
     * @param string $facetName
104
     * @param ResponseAdapter $response
105
     * @return array
106
     */
107 38
    protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
108
    {
109 38
        $optionsFromSolrResponse = [];
110 38
        if (!isset($response->facets->{$facetName})) {
111 10
            return $optionsFromSolrResponse;
112
        }
113
114 34
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
115 34
            $optionValue = $bucket->val;
116 34
            $optionCount = $bucket->count;
117 34
            $optionsFromSolrResponse[$optionValue] = $optionCount;
118
        }
119
120 34
        return $optionsFromSolrResponse;
121
    }
122
123
    /**
124
     * @param string $facetName
125
     * @param ResponseAdapter $response
126
     * @return array
127
     */
128 38
    protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
129
    {
130 38
        $metricsFromSolrResponse = [];
131
132 38
        if (!isset($response->facets->{$facetName}->buckets)) {
133 10
            return [];
134
        }
135
136 34
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
137 34
            $bucketVariables = get_object_vars($bucket);
138 34
            foreach ($bucketVariables as $key => $value) {
139 34
                if (strpos($key, 'metrics_') === 0) {
140 1
                    $metricsKey = str_replace('metrics_', '', $key);
141 1
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
142
                }
143
            }
144
        }
145
146 34
        return $metricsFromSolrResponse;
147
    }
148
}
149