Passed
Push — master ( ae02be...f196a8 )
by Timo
21:07
created

QueryGroupFacetParser::parse()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.0023

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 24
cts 25
cp 0.96
rs 8.7433
c 0
b 0
f 0
cc 6
eloc 34
nc 3
nop 3
crap 6.0023

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 30
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
37
    {
38 30
        $response = $resultSet->getResponse();
39 30
        $fieldName = $facetConfiguration['field'];
40 30
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
41
42 30
        $rawOptions = $this->getRawOptions($response, $fieldName);
43 30
        $noOptionsInResponse = $rawOptions === [];
44 30
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
45
46 30
        if ($noOptionsInResponse && $hideEmpty) {
47 2
            return null;
48
        }
49
50
        /** @var QueryGroupFacet $facet */
51 28
        $facet = $this->objectManager->get(
52 28
            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
            $facetConfiguration
58
        );
59
60 28
        $activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
61 28
        $facet->setIsUsed(in_array($facetName, $activeFacets, true));
62
63 28
        if (!$noOptionsInResponse) {
64 28
            $facet->setIsAvailable(true);
65 28
            foreach ($rawOptions as $query => $count) {
66 28
                $value = $this->getValueByQuery($query, $facetConfiguration);
67
                // Skip unknown queries
68 28
                if ($value === null) {
69
                    continue;
70
                }
71
72 28
                $isOptionsActive = $resultSet->getUsedSearchRequest()->getHasFacetValue($facetName, $value);
73 28
                $label = $this->getLabelFromRenderingInstructions(
74
                    $value,
75
                    $count,
76
                    $facetName,
77
                    $facetConfiguration
78
                );
79 28
                $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 28
        $facet = $this->applyManualSortOrder($facet, $facetConfiguration);
88
89 28
        $facet = $this->applyReverseOrder($facet, $facetConfiguration);
90
91 28
        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 30
    protected function getRawOptions(\Apache_Solr_Response $response, $fieldName)
102
    {
103 30
        $options = [];
104
105 30
        foreach ($response->facet_counts->facet_queries as $rawValue => $count) {
106 30
            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 28
            $rawValue = preg_replace('/^\{!ex=[^\}]*\}(.*)/', '\\1', $rawValue);
114
115 28
            list($field, $query) = explode(':', $rawValue, 2);
116 28
            if ($field === $fieldName) {
117 28
                $options[$query] = $count;
118
            }
119
        }
120
121 30
        return $options;
122
    }
123
124
    /**
125
     * @param string $query
126
     * @param array $facetConfiguration
127
     * @return string|null
128
     */
129 28
    protected function getValueByQuery($query, array $facetConfiguration)
130
    {
131 28
        $value = null;
132 28
        foreach ($facetConfiguration['queryGroup.'] as $valueKey => $config) {
133 28
            if (isset($config['query']) && $config['query'] === $query) {
134 28
                $value = rtrim($valueKey, '.');
135 28
                break;
136
            }
137
        }
138 28
        return $value;
139
    }
140
}
141