Passed
Push — master ( 5f60a5...391298 )
by Timo
24:49
created

QueryGroupFacetParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 96.23%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 115
ccs 51
cts 53
cp 0.9623
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 59 7
A getRawOptions() 0 21 4
A getValueByQuery() 0 10 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\QueryGroup;
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 QueryGroupFacetParser
22
 *
23
 * @author Frans Saris <[email protected]>
24
 * @author Timo Hund <[email protected]>
25
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\QueryGroupFacet
26
 */
27
class QueryGroupFacetParser extends AbstractFacetParser
28
{
29
30
    /**
31
     * @param SearchResultSet $resultSet
32
     * @param string $facetName
33
     * @param array $facetConfiguration
34
     * @return QueryGroupFacet|null
35
     */
36 39
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
37
    {
38 39
        $response = $resultSet->getResponse();
39 39
        $fieldName = $facetConfiguration['field'];
40 39
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
41
42 39
        $rawOptions = $this->getRawOptions($response, $fieldName);
43 39
        $noOptionsInResponse = $rawOptions === [];
44 39
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
45
46 39
        if ($noOptionsInResponse && $hideEmpty) {
47 4
            return null;
48
        }
49
50
        /** @var QueryGroupFacet $facet */
51 36
        $facet = $this->objectManager->get(
52 36
            QueryGroupFacet::class,
53 36
            $resultSet,
54 36
            $facetName,
55 36
            $fieldName,
56 36
            $label,
57 36
            $facetConfiguration
58
        );
59
60 36
        $activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
61 36
        $facet->setIsUsed(in_array($facetName, $activeFacets, true));
62
63 36
        if (!$noOptionsInResponse) {
64 36
            $facet->setIsAvailable(true);
65 36
            foreach ($rawOptions as $query => $count) {
66 36
                $value = $this->getValueByQuery($query, $facetConfiguration);
67
                // Skip unknown queries
68 36
                if ($value === null) {
69
                    continue;
70
                }
71
72 36
                if ($this->getIsExcludedFacetValue($query, $facetConfiguration)) {
73
                    continue;
74
                }
75
76 36
                $isOptionsActive = $resultSet->getUsedSearchRequest()->getHasFacetValue($facetName, $value);
77 36
                $label = $this->getLabelFromRenderingInstructions(
78 36
                    $value,
79 36
                    $count,
80 36
                    $facetName,
81 36
                    $facetConfiguration
82
                );
83 36
                $facet->addOption(new Option($facet, $label, $value, $count, $isOptionsActive));
84
            }
85
        }
86
87
88
        // after all options have been created we apply a manualSortOrder if configured
89
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
90
        // need to be handled in the frontend.
91 36
        $this->applyManualSortOrder($facet, $facetConfiguration);
92 36
        $this->applyReverseOrder($facet, $facetConfiguration);
93
94 36
        return $facet;
95
    }
96
97
    /**
98
     * Get raw query options
99
     *
100
     * @param \Apache_Solr_Response $response
101
     * @param string $fieldName
102
     * @return array
103
     */
104 39
    protected function getRawOptions(\Apache_Solr_Response $response, $fieldName)
105
    {
106 39
        $options = [];
107
108 39
        foreach ($response->facet_counts->facet_queries as $rawValue => $count) {
109 39
            if ((int)$count === 0) {
110 18
                continue;
111
            }
112
113
            // todo: add test cases to check if this is needed https://forge.typo3.org/issues/45440
114
            // remove tags from the facet.query response, for facet.field
115
            // and facet.range Solr does that on its own automatically
116 36
            $rawValue = preg_replace('/^\{!ex=[^\}]*\}(.*)/', '\\1', $rawValue);
117
118 36
            list($field, $query) = explode(':', $rawValue, 2);
119 36
            if ($field === $fieldName) {
120 36
                $options[$query] = $count;
121
            }
122
        }
123
124 39
        return $options;
125
    }
126
127
    /**
128
     * @param string $query
129
     * @param array $facetConfiguration
130
     * @return string|null
131
     */
132 36
    protected function getValueByQuery($query, array $facetConfiguration)
133
    {
134 36
        $value = null;
135 36
        foreach ($facetConfiguration['queryGroup.'] as $valueKey => $config) {
136 36
            if (isset($config['query']) && $config['query'] === $query) {
137 36
                $value = rtrim($valueKey, '.');
138 36
                break;
139
            }
140
        }
141 36
        return $value;
142
    }
143
}
144