Passed
Push — master ( 1b8200...cdf526 )
by Timo
21:46
created

QueryGroupFacetParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 84.31%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 114
ccs 43
cts 51
cp 0.8431
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getRawOptions() 0 22 4
A getValueByQuery() 0 11 4
B parse() 0 57 6
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 29
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
37
    {
38 29
        $response = $resultSet->getResponse();
39 29
        $fieldName = $facetConfiguration['field'];
40 29
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
41
42 29
        $rawOptions = $this->getRawOptions($response, $fieldName);
43 29
        $noOptionsInResponse = $rawOptions === [];
44 29
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
45
46 29
        if ($noOptionsInResponse && $hideEmpty) {
47 2
            return null;
48
        }
49
50
        /** @var QueryGroupFacet $facet */
51 27
        $facet = $this->objectManager->get(
52 27
            QueryGroupFacet::class,
53
            $resultSet,
0 ignored issues
show
Unused Code introduced by
The call to ObjectManagerInterface::get() has too many arguments starting with $resultSet.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
            $facetName,
55
            $fieldName,
56
            $label,
57 27
            $facetConfiguration
58
        );
59
60 27
        $activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
61 27
        $facet->setIsUsed(in_array($facetName, $activeFacets, true));
62
63 27
        if (!$noOptionsInResponse) {
64 27
            $facet->setIsAvailable(true);
65 27
            foreach ($rawOptions as $query => $count) {
66 27
                $value = $this->getValueByQuery($query, $facetConfiguration);
67
                // Skip unknown queries
68 27
                if (!$value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
69
                    continue;
70
                }
71
72 27
                $isOptionsActive = $resultSet->getUsedSearchRequest()->getHasFacetValue($facetName, $value);
73 27
                $label = $this->getLabelFromRenderingInstructions(
74
                    $value,
75
                    $count,
76
                    $facetName,
77 27
                    $facetConfiguration
78
                );
79 27
                $facet->addOption(new Option($facet, $label, $value, $count, $isOptionsActive));
80
            }
81
        }
82
83
84
        // after all options have been created we apply a manualSortOrder if configured
85
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
86
        // need to be handled in the frontend.
87 27
        $facet = $this->applyManualSortOrder($facet, $facetConfiguration);
88
89 27
        $facet = $this->applyReverseOrder($facet, $facetConfiguration);
90
91 27
        return $facet;
92
    }
93
94
    /**
95
     * Get raw query options
96
     *
97
     * @param \Apache_Solr_Response $response
98
     * @param string $fieldName
99
     * @return array
100
     */
101 29
    protected function getRawOptions(\Apache_Solr_Response $response, $fieldName)
102
    {
103 29
        $options = [];
104
105 29
        foreach ($response->facet_counts->facet_queries as $rawValue => $count) {
106 29
            if ((int)$count === 0) {
107 12
                continue;
108
            }
109
110
            // todo: add test cases to check if this is needed https://forge.typo3.org/issues/45440
111
            // remove tags from the facet.query response, for facet.field
112
            // and facet.range Solr does that on its own automatically
113 27
            $rawValue = preg_replace('/^\{!ex=[^\}]*\}(.*)/', '\\1', $rawValue);
114
115 27
            list($field, $query) = explode(':', $rawValue, 2);
116 27
            if ($field === $fieldName) {
117 27
                $options[$query] = $count;
118
            }
119
        }
120
121 29
        return $options;
122
    }
123
124
    /**
125
     * @param string $query
126
     * @param array $facetConfiguration
127
     * @return string|null
128
     */
129 27
    protected function getValueByQuery($query, array $facetConfiguration)
130
    {
131 27
        $value = null;
132 27
        foreach ($facetConfiguration['queryGroup.'] as $valueKey => $config) {
133 27
            if (isset($config['query']) && $config['query'] === $query) {
134 27
                $value = rtrim($valueKey, '.');
135 27
                break;
136
            }
137
        }
138 27
        return $value;
139
    }
140
}
141