Passed
Pull Request — release-11.2.x (#3604)
by Markus
32:12 queued 27:55
created

QueryGroupFacetParser::parse()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 7.0541

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 58
ccs 26
cts 29
cp 0.8966
rs 8.4266
cc 7
nc 3
nop 3
crap 7.0541

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\QueryGroup;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetParser;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
20
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
21
22
/**
23
 * Class QueryGroupFacetParser
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 */
28
class QueryGroupFacetParser extends AbstractFacetParser
29
{
30
31
    /**
32
     * @param SearchResultSet $resultSet
33
     * @param string $facetName
34
     * @param array $facetConfiguration
35
     * @return QueryGroupFacet|null
36
     */
37 8
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
38
    {
39 8
        $response = $resultSet->getResponse();
40 8
        $fieldName = $facetConfiguration['field'];
41 8
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
42
43 8
        $rawOptions = $this->getRawOptions($response, $fieldName);
44 8
        $noOptionsInResponse = $rawOptions === [];
45 8
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
46
47 8
        if ($noOptionsInResponse && $hideEmpty) {
48
            return null;
49
        }
50
51
        /** @var QueryGroupFacet $facet */
52 8
        $facet = $this->objectManager->get(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

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

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
53 8
            QueryGroupFacet::class,
54
            $resultSet,
55
            $facetName,
56
            $fieldName,
57
            $label,
58 8
            $facetConfiguration
59
        );
60
61 8
        $activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
62 8
        $facet->setIsUsed(in_array($facetName, $activeFacets, true));
63
64 8
        if (!$noOptionsInResponse) {
65 8
            $facet->setIsAvailable(true);
66 8
            foreach ($rawOptions as $query => $count) {
67 8
                $value = $this->getValueByQuery($query, $facetConfiguration);
68
                // Skip unknown queries
69 8
                if ($value === null) {
70
                    continue;
71
                }
72
73 8
                if ($this->getIsExcludedFacetValue($query, $facetConfiguration)) {
74
                    continue;
75
                }
76
77 8
                $isOptionsActive = $resultSet->getUsedSearchRequest()->getHasFacetValue($facetName, $value);
78 8
                $label = $this->getLabelFromRenderingInstructions(
79
                    $value,
80
                    $count,
81
                    $facetName,
82 8
                    $facetConfiguration
83
                );
84 8
                $facet->addOption($this->objectManager->get(Option::class, $facet, $label, $value, $count, $isOptionsActive));
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

84
                $facet->addOption(/** @scrutinizer ignore-deprecated */ $this->objectManager->get(Option::class, $facet, $label, $value, $count, $isOptionsActive));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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 8
        $this->applyManualSortOrder($facet, $facetConfiguration);
92 8
        $this->applyReverseOrder($facet, $facetConfiguration);
93
94 8
        return $facet;
95
    }
96
97
    /**
98
     * Get raw query options
99
     *
100
     * @param ResponseAdapter $response
101
     * @param string $fieldName
102
     * @return array
103
     */
104 8
    protected function getRawOptions(ResponseAdapter $response, $fieldName)
105
    {
106 8
        $options = [];
107
108 8
        foreach ($response->facet_counts->facet_queries as $rawValue => $count) {
109 8
            if ((int)$count === 0) {
110 4
                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 8
            $rawValue = preg_replace('/^\{!ex=[^\}]*\}(.*)/', '\\1', $rawValue);
117
118 8
            list($field, $query) = explode(':', $rawValue, 2);
119 8
            if ($field === $fieldName) {
120 8
                $options[$query] = $count;
121
            }
122
        }
123
124 8
        return $options;
125
    }
126
127
    /**
128
     * @param string $query
129
     * @param array $facetConfiguration
130
     * @return string|null
131
     */
132 8
    protected function getValueByQuery($query, array $facetConfiguration)
133
    {
134 8
        $value = null;
135 8
        foreach ($facetConfiguration['queryGroup.'] as $valueKey => $config) {
136 8
            if (isset($config['query']) && $config['query'] === $query) {
137 8
                $value = rtrim($valueKey, '.');
138 8
                break;
139
            }
140
        }
141 8
        return $value;
142
    }
143
}
144