QueryGroupFacetParser::parse()   B
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7.0091

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 59
ccs 33
cts 35
cp 0.9429
rs 8.4266
cc 7
nc 3
nop 3
crap 7.0091

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
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
20
21
/**
22
 * Class QueryGroupFacetParser
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
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
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
37 40
    {
38
        $response = $resultSet->getResponse();
39 40
        $fieldName = $facetConfiguration['field'];
40 40
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
41 40
42
        $rawOptions = $this->getRawOptions($response, $fieldName);
43 40
        $noOptionsInResponse = $rawOptions === [];
44 40
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
45 40
46
        if ($noOptionsInResponse && $hideEmpty) {
47 40
            return null;
48 5
        }
49
50
        /** @var QueryGroupFacet $facet */
51
        $facet = $this->objectManager->get(
52 37
            QueryGroupFacet::class,
53 37
            $resultSet,
54 37
            $facetName,
55 37
            $fieldName,
56 37
            $label,
57 37
            $facetConfiguration
58 37
        );
59
60
        $activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
61 37
        $facet->setIsUsed(in_array($facetName, $activeFacets, true));
62 37
63
        if (!$noOptionsInResponse) {
64 37
            $facet->setIsAvailable(true);
65 37
            foreach ($rawOptions as $query => $count) {
66 37
                $value = $this->getValueByQuery($query, $facetConfiguration);
67 37
                // Skip unknown queries
68
                if ($value === null) {
69 37
                    continue;
70
                }
71
72
                if ($this->getIsExcludedFacetValue($query, $facetConfiguration)) {
73 37
                    continue;
74
                }
75
76
                $isOptionsActive = $resultSet->getUsedSearchRequest()->getHasFacetValue($facetName, $value);
77 37
                $label = $this->getLabelFromRenderingInstructions(
78 37
                    $value,
79 37
                    $count,
80 37
                    $facetName,
81 37
                    $facetConfiguration
82 37
                );
83
                $facet->addOption($this->objectManager->get(Option::class, $facet, $label, $value, $count, $isOptionsActive));
84 37
            }
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
        $this->applyManualSortOrder($facet, $facetConfiguration);
92 37
        $this->applyReverseOrder($facet, $facetConfiguration);
93 37
94
        return $facet;
95 37
    }
96
97
    /**
98
     * Get raw query options
99
     *
100
     * @param ResponseAdapter $response
101
     * @param string $fieldName
102
     * @return array
103
     */
104
    protected function getRawOptions(ResponseAdapter $response, $fieldName)
105 40
    {
106
        $options = [];
107 40
108
        foreach ($response->facet_counts->facet_queries as $rawValue => $count) {
109 40
            if ((int)$count === 0) {
110 40
                continue;
111 19
            }
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
            $rawValue = preg_replace('/^\{!ex=[^\}]*\}(.*)/', '\\1', $rawValue);
117 37
118
            list($field, $query) = explode(':', $rawValue, 2);
119 37
            if ($field === $fieldName) {
120 37
                $options[$query] = $count;
121 37
            }
122
        }
123
124
        return $options;
125 40
    }
126
127
    /**
128
     * @param string $query
129
     * @param array $facetConfiguration
130
     * @return string|null
131
     */
132
    protected function getValueByQuery($query, array $facetConfiguration)
133 37
    {
134
        $value = null;
135 37
        foreach ($facetConfiguration['queryGroup.'] as $valueKey => $config) {
136 37
            if (isset($config['query']) && $config['query'] === $query) {
137 37
                $value = rtrim($valueKey, '.');
138 37
                break;
139 37
            }
140
        }
141
        return $value;
142 37
    }
143
}
144