Passed
Push — master ( e55157...ed38f6 )
by Timo
27:41
created

OptionsFacetParser::parse()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 49
ccs 33
cts 33
cp 1
rs 8.7857
c 0
b 0
f 0
cc 6
nc 8
nop 3
crap 6
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 55
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
33
    {
34 55
        $response = $resultSet->getResponse();
35 55
        $fieldName = $facetConfiguration['field'];
36 55
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
37 55
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
38 55
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
39 55
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
40 55
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
41 55
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
42 55
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
43 55
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
44
45 55
        if ($hasNoOptionsToShow && $hideEmpty) {
46 6
            return null;
47
        }
48
49
        /** @var $facet OptionsFacet */
50 53
        $facet = $this->objectManager->get(
51 53
            OptionsFacet::class,
52 53
            $resultSet,
53 53
            $facetName,
54 53
            $fieldName,
55 53
            $label,
56 53
            $facetConfiguration
57
        );
58
59 53
        $hasActiveOptions = count($optionsFromRequest) > 0;
60 53
        $facet->setIsUsed($hasActiveOptions);
61 53
        $facet->setIsAvailable($hasOptionsInResponse);
62
63 53
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
64 53
        foreach ($optionsToCreate as $optionsValue => $count) {
65 51
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
66 1
                continue;
67
            }
68
69 51
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
70 51
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
71 51
            $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 53
        $this->applyManualSortOrder($facet, $facetConfiguration);
78 53
        $this->applyReverseOrder($facet, $facetConfiguration);
79
80 53
        return $facet;
81
    }
82
83
    /**
84
     * @param string $facetName
85
     * @param ResponseAdapter $response
86
     * @return array
87
     */
88 55
    protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
89
    {
90 55
        $optionsFromSolrResponse = [];
91 55
        if (!isset($response->facets->{$facetName})) {
92 14
            return $optionsFromSolrResponse;
93
        }
94
95 52
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
96 49
            $optionValue = $bucket->val;
97 49
            $optionCount = $bucket->count;
98 49
            $optionsFromSolrResponse[$optionValue] = $optionCount;
99
        }
100
101 52
        return $optionsFromSolrResponse;
102
    }
103
104
    /**
105
     * @param string $facetName
106
     * @param ResponseAdapter $response
107
     * @return array
108
     */
109 55
    protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
110
    {
111 55
        $metricsFromSolrResponse = [];
112
113 55
        if (!isset($response->facets->{$facetName}->buckets)) {
114 14
            return [];
115
        }
116
117 52
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
118 49
            $bucketVariables = get_object_vars($bucket);
119 49
            foreach ($bucketVariables as $key => $value) {
120 49
                if (strpos($key, 'metrics_') === 0) {
121 1
                    $metricsKey = str_replace('metrics_', '', $key);
122 49
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
123
                }
124
            }
125
        }
126
127 52
        return $metricsFromSolrResponse;
128
    }
129
}
130