Passed
Push — master ( ea12d0...900c6a )
by Timo
20:36
created

QueryGroupFacetParser::parse()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 61
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7.0084

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 34
cts 36
cp 0.9444
rs 7.399
c 0
b 0
f 0
cc 7
eloc 36
nc 3
nop 3
crap 7.0084

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
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 34
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
37
    {
38 34
        $response = $resultSet->getResponse();
39 34
        $fieldName = $facetConfiguration['field'];
40 34
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
41
42 34
        $rawOptions = $this->getRawOptions($response, $fieldName);
43 34
        $noOptionsInResponse = $rawOptions === [];
44 34
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
45
46 34
        if ($noOptionsInResponse && $hideEmpty) {
47 3
            return null;
48
        }
49
50
        /** @var QueryGroupFacet $facet */
51 31
        $facet = $this->objectManager->get(
52 31
            QueryGroupFacet::class,
53 31
            $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 31
            $facetName,
55 31
            $fieldName,
56 31
            $label,
57 31
            $facetConfiguration
58
        );
59
60 31
        $activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
61 31
        $facet->setIsUsed(in_array($facetName, $activeFacets, true));
62
63 31
        if (!$noOptionsInResponse) {
64 31
            $facet->setIsAvailable(true);
65 31
            foreach ($rawOptions as $query => $count) {
66 31
                $value = $this->getValueByQuery($query, $facetConfiguration);
67
                // Skip unknown queries
68 31
                if ($value === null) {
69
                    continue;
70
                }
71
72 31
                if ($this->getIsExcludedFacetValue($query, $facetConfiguration)) {
73
                    continue;
74
                }
75
76 31
                $isOptionsActive = $resultSet->getUsedSearchRequest()->getHasFacetValue($facetName, $value);
77 31
                $label = $this->getLabelFromRenderingInstructions(
78 31
                    $value,
79 31
                    $count,
80 31
                    $facetName,
81 31
                    $facetConfiguration
82
                );
83 31
                $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 31
        $facet = $this->applyManualSortOrder($facet, $facetConfiguration);
92
93 31
        $facet = $this->applyReverseOrder($facet, $facetConfiguration);
94
95 31
        return $facet;
96
    }
97
98
    /**
99
     * Get raw query options
100
     *
101
     * @param \Apache_Solr_Response $response
102
     * @param string $fieldName
103
     * @return array
104
     */
105 34
    protected function getRawOptions(\Apache_Solr_Response $response, $fieldName)
106
    {
107 34
        $options = [];
108
109 34
        foreach ($response->facet_counts->facet_queries as $rawValue => $count) {
110 34
            if ((int)$count === 0) {
111 15
                continue;
112
            }
113
114
            // todo: add test cases to check if this is needed https://forge.typo3.org/issues/45440
115
            // remove tags from the facet.query response, for facet.field
116
            // and facet.range Solr does that on its own automatically
117 31
            $rawValue = preg_replace('/^\{!ex=[^\}]*\}(.*)/', '\\1', $rawValue);
118
119 31
            list($field, $query) = explode(':', $rawValue, 2);
120 31
            if ($field === $fieldName) {
121 31
                $options[$query] = $count;
122
            }
123
        }
124
125 34
        return $options;
126
    }
127
128
    /**
129
     * @param string $query
130
     * @param array $facetConfiguration
131
     * @return string|null
132
     */
133 31
    protected function getValueByQuery($query, array $facetConfiguration)
134
    {
135 31
        $value = null;
136 31
        foreach ($facetConfiguration['queryGroup.'] as $valueKey => $config) {
137 31
            if (isset($config['query']) && $config['query'] === $query) {
138 31
                $value = rtrim($valueKey, '.');
139 31
                break;
140
            }
141
        }
142 31
        return $value;
143
    }
144
}
145