Passed
Push — master ( 16f071...683c38 )
by Timo
22:25
created

OptionsFacetParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 104
ccs 49
cts 53
cp 0.9245
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 49 6
B getMetricsFromSolrResponse() 0 19 5
A getOptionsFromSolrResponse() 0 14 3
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
20
/**
21
 * Class OptionsFacetParser
22
 */
23
class OptionsFacetParser extends AbstractFacetParser
24
{
25
    /**
26
     * @param SearchResultSet $resultSet
27
     * @param string $facetName
28
     * @param array $facetConfiguration
29
     * @return OptionsFacet|null
30
     */
31 66
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
32
    {
33 66
        $response = $resultSet->getResponse();
34 66
        $fieldName = $facetConfiguration['field'];
35 66
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
36 66
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
37 66
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
38 66
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
39 66
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
40 66
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
41 66
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
42 66
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
43
44 66
        if ($hasNoOptionsToShow && $hideEmpty) {
45 8
            return null;
46
        }
47
48
        /** @var $facet OptionsFacet */
49 62
        $facet = $this->objectManager->get(
50 62
            OptionsFacet::class,
51 62
            $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

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