Passed
Pull Request — master (#2985)
by
unknown
34:31
created

OptionsFacetParser::parse()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7.0018

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 53
ccs 29
cts 30
cp 0.9667
rs 8.4426
c 1
b 0
f 0
cc 7
nc 14
nop 3
crap 7.0018

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
    public function injectDispatcher(Dispatcher $dispatcher)
37
    {
38
        $this->dispatcher = $dispatcher;
39
    }
40
41
    /**
42
     * @param SearchResultSet $resultSet
43
     * @param string $facetName
44
     * @param array $facetConfiguration
45
     * @return OptionsFacet|null
46
     */
47 22
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
48
    {
49 22
        $response = $resultSet->getResponse();
50 22
        $fieldName = $facetConfiguration['field'];
51 22
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
52 22
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
53 22
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
54 22
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
55 22
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
56 22
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
57 22
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
58 22
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
59
60 22
        if ($hasNoOptionsToShow && $hideEmpty) {
61 1
            return null;
62
        }
63
64
        /** @var $facet OptionsFacet */
65 22
        $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 22
            OptionsFacet::class,
67
            $resultSet,
68
            $facetName,
69
            $fieldName,
70
            $label,
71
            $facetConfiguration
72
        );
73
74 22
        $hasActiveOptions = count($optionsFromRequest) > 0;
75 22
        $facet->setIsUsed($hasActiveOptions);
76 22
        $facet->setIsAvailable($hasOptionsInResponse);
77
78 22
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
79 22
        foreach ($optionsToCreate as $optionsValue => $count) {
80 20
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
81 1
                continue;
82
            }
83
84 20
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
85 20
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
86 20
            $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 22
        $this->applyManualSortOrder($facet, $facetConfiguration);
93 22
        $this->applyReverseOrder($facet, $facetConfiguration);
94
95 22
        if(!is_null($this->dispatcher)) {
96
            $this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
97
        }
98
99 22
        return $facet;
100
    }
101
102
    /**
103
     * @param string $facetName
104
     * @param ResponseAdapter $response
105
     * @return array
106
     */
107 22
    protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
108
    {
109 22
        $optionsFromSolrResponse = [];
110 22
        if (!isset($response->facets->{$facetName})) {
111 9
            return $optionsFromSolrResponse;
112
        }
113
114 19
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
115 19
            $optionValue = $bucket->val;
116 19
            $optionCount = $bucket->count;
117 19
            $optionsFromSolrResponse[$optionValue] = $optionCount;
118
        }
119
120 19
        return $optionsFromSolrResponse;
121
    }
122
123
    /**
124
     * @param string $facetName
125
     * @param ResponseAdapter $response
126
     * @return array
127
     */
128 22
    protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
129
    {
130 22
        $metricsFromSolrResponse = [];
131
132 22
        if (!isset($response->facets->{$facetName}->buckets)) {
133 9
            return [];
134
        }
135
136 19
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
137 19
            $bucketVariables = get_object_vars($bucket);
138 19
            foreach ($bucketVariables as $key => $value) {
139 19
                if (strpos($key, 'metrics_') === 0) {
140
                    $metricsKey = str_replace('metrics_', '', $key);
141
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
142
                }
143
            }
144
        }
145
146 19
        return $metricsFromSolrResponse;
147
    }
148
}
149