Passed
Push — master ( 17ae05...2920d4 )
by Timo
27:57
created

OptionsFacetParser::getMetricsFromSolrResponse()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5.0187
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
21
/**
22
 * Class OptionsFacetParser
23
 */
24
class OptionsFacetParser extends AbstractFacetParser
25
{
26
    /**
27
     * @param SearchResultSet $resultSet
28
     * @param string $facetName
29
     * @param array $facetConfiguration
30
     * @return OptionsFacet|null
31
     */
32 54
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
33
    {
34 54
        $response = $resultSet->getResponse();
35 54
        $fieldName = $facetConfiguration['field'];
36 54
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
37 54
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
38 54
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
39 54
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
40 54
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
41 54
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
42 54
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
43 54
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
44
45 54
        if ($hasNoOptionsToShow && $hideEmpty) {
46 6
            return null;
47
        }
48
49
        /** @var $facet OptionsFacet */
50 52
        $facet = $this->objectManager->get(
51 52
            OptionsFacet::class,
52 52
            $resultSet,
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Object...ManagerInterface::get() has too many arguments starting with $resultSet. ( Ignorable by Annotation )

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

52
        /** @scrutinizer ignore-call */ 
53
        $facet = $this->objectManager->get(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53 52
            $facetName,
54 52
            $fieldName,
55 52
            $label,
56 52
            $facetConfiguration
57
        );
58
59 52
        $hasActiveOptions = count($optionsFromRequest) > 0;
60 52
        $facet->setIsUsed($hasActiveOptions);
61 52
        $facet->setIsAvailable($hasOptionsInResponse);
62
63 52
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
64 52
        foreach ($optionsToCreate as $optionsValue => $count) {
65 50
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
66 1
                continue;
67
            }
68
69 50
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
70 50
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
71 50
            $facet->addOption(new Option($facet, $label, $optionsValue, $count, $isOptionsActive, $metricsFromSolrResponse[$optionsValue]));
72
        }
73
74
        // after all options have been created we apply a manualSortOrder if configured
75
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
76
        // need to be handled in the frontend.
77 52
        $this->applyManualSortOrder($facet, $facetConfiguration);
78 52
        $this->applyReverseOrder($facet, $facetConfiguration);
79
80 52
        return $facet;
81
    }
82
83
    /**
84
     * @param string $facetName
85
     * @param ResponseAdapter $response
86
     * @return array
87
     */
88 54
    protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
89
    {
90 54
        $optionsFromSolrResponse = [];
91 54
        if (!isset($response->facets->{$facetName})) {
92 9
            return $optionsFromSolrResponse;
93
        }
94
95 51
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
96 48
            $optionValue = $bucket->val;
97 48
            $optionCount = $bucket->count;
98 48
            $optionsFromSolrResponse[$optionValue] = $optionCount;
99
        }
100
101 51
        return $optionsFromSolrResponse;
102
    }
103
104
    /**
105
     * @param string $facetName
106
     * @param ResponseAdapter $response
107
     * @return array
108
     */
109 54
    protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
110
    {
111 54
        $metricsFromSolrResponse = [];
112
113 54
        if (!isset($response->facets->{$facetName}->buckets)) {
114 9
            return [];
115
        }
116
117 51
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
118 48
            $bucketVariables = get_object_vars($bucket);
119 48
            foreach ($bucketVariables as $key => $value) {
120 48
                if (strpos($key, 'metrics_') === 0) {
121
                    $metricsKey = str_replace('metrics_', '', $key);
122 48
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
123
                }
124
            }
125
        }
126
127 51
        return $metricsFromSolrResponse;
128
    }
129
}
130