Passed
Push — master ( 498335...52b5ef )
by Rafael
42:19
created

OptionsFacetParser::parse()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7.0011

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 53
ccs 34
cts 35
cp 0.9714
rs 8.4426
c 1
b 0
f 0
cc 7
nc 14
nop 3
crap 7.0011

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\Options;
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
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
22
23
/**
24
 * Class OptionsFacetParser
25
 */
26
class OptionsFacetParser extends AbstractFacetParser
27
{
28
    /**
29
     * @var Dispatcher
30
     */
31
    protected $dispatcher;
32
33
    /**
34
     * @param Dispatcher $dispatcher
35
     */
36 34
    public function injectDispatcher(Dispatcher $dispatcher)
37
    {
38 34
        $this->dispatcher = $dispatcher;
39 34
    }
40
41
    /**
42
     * @param SearchResultSet $resultSet
43
     * @param string $facetName
44
     * @param array $facetConfiguration
45
     * @return OptionsFacet|null
46
     */
47 36
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
48
    {
49 36
        $response = $resultSet->getResponse();
50 36
        $fieldName = $facetConfiguration['field'];
51 36
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
52 36
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
53 36
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
54 36
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
55 36
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
56 36
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
57 36
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
58 36
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
59
60 36
        if ($hasNoOptionsToShow && $hideEmpty) {
61 5
            return null;
62
        }
63
64
        /** @var $facet OptionsFacet */
65 34
        $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

65
        $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...
66 34
            OptionsFacet::class,
67 34
            $resultSet,
68 34
            $facetName,
69 34
            $fieldName,
70 34
            $label,
71 34
            $facetConfiguration
72
        );
73
74 34
        $hasActiveOptions = count($optionsFromRequest) > 0;
75 34
        $facet->setIsUsed($hasActiveOptions);
76 34
        $facet->setIsAvailable($hasOptionsInResponse);
77
78 34
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
79 34
        foreach ($optionsToCreate as $optionsValue => $count) {
80 34
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
81
                continue;
82
            }
83
84 34
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
85 34
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
86 34
            $facet->addOption($this->objectManager->get(Option::class, $facet, $label, $optionsValue, $count, $isOptionsActive, $metricsFromSolrResponse[$optionsValue]));
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

86
            $facet->addOption(/** @scrutinizer ignore-deprecated */ $this->objectManager->get(Option::class, $facet, $label, $optionsValue, $count, $isOptionsActive, $metricsFromSolrResponse[$optionsValue]));

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...
87
        }
88
89
        // after all options have been created we apply a manualSortOrder if configured
90
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
91
        // need to be handled in the frontend.
92 34
        $this->applyManualSortOrder($facet, $facetConfiguration);
93 34
        $this->applyReverseOrder($facet, $facetConfiguration);
94
95 34
        if(!is_null($this->dispatcher)) {
96 32
            $this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
97
        }
98
99 34
        return $facet;
100
    }
101
102
    /**
103
     * @param string $facetName
104
     * @param ResponseAdapter $response
105
     * @return array
106
     */
107 36
    protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
108
    {
109 36
        $optionsFromSolrResponse = [];
110 36
        if (!isset($response->facets->{$facetName})) {
111 5
            return $optionsFromSolrResponse;
112
        }
113
114 36
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
115 33
            $optionValue = $bucket->val;
116 33
            $optionCount = $bucket->count;
117 33
            $optionsFromSolrResponse[$optionValue] = $optionCount;
118
        }
119
120 36
        return $optionsFromSolrResponse;
121
    }
122
123
    /**
124
     * @param string $facetName
125
     * @param ResponseAdapter $response
126
     * @return array
127
     */
128 36
    protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
129
    {
130 36
        $metricsFromSolrResponse = [];
131
132 36
        if (!isset($response->facets->{$facetName}->buckets)) {
133 5
            return [];
134
        }
135
136 36
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
137 33
            $bucketVariables = get_object_vars($bucket);
138 33
            foreach ($bucketVariables as $key => $value) {
139 33
                if (strpos($key, 'metrics_') === 0) {
140 1
                    $metricsKey = str_replace('metrics_', '', $key);
141 1
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
142
                }
143
            }
144
        }
145
146 36
        return $metricsFromSolrResponse;
147
    }
148
}
149