Passed
Push — master ( d4f966...dddccf )
by Rafael
34:04 queued 25:52
created

OptionsFacetParser::getOptionsFromSolrResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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