Passed
Push — master ( 96ad23...e674df )
by Timo
12:01
created

OptionsFacetParser::injectDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 55
33
    /**
34 55
     * @param Dispatcher $dispatcher
35 55
     */
36 55
    public function injectDispatcher(Dispatcher $dispatcher)
37 55
    {
38 55
        $this->dispatcher = $dispatcher;
39 55
    }
40 55
41 55
    /**
42 55
     * @param SearchResultSet $resultSet
43 55
     * @param string $facetName
44
     * @param array $facetConfiguration
45 55
     * @return OptionsFacet|null
46 6
     */
47
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
48
    {
49
        $response = $resultSet->getResponse();
50 53
        $fieldName = $facetConfiguration['field'];
51 53
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
52 53
        $optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
53 53
        $metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
54 53
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
55 53
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
56 53
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
57
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
58
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
59 53
60 53
        if ($hasNoOptionsToShow && $hideEmpty) {
61 53
            return null;
62
        }
63 53
64 53
        /** @var $facet OptionsFacet */
65 51
        $facet = $this->objectManager->get(
66 1
            OptionsFacet::class,
67
            $resultSet,
68
            $facetName,
69 51
            $fieldName,
70 51
            $label,
71 51
            $facetConfiguration
72
        );
73
74
        $hasActiveOptions = count($optionsFromRequest) > 0;
75
        $facet->setIsUsed($hasActiveOptions);
76
        $facet->setIsAvailable($hasOptionsInResponse);
77 53
78 53
        $optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
79
        foreach ($optionsToCreate as $optionsValue => $count) {
80 53
            if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
81
                continue;
82
            }
83
84
            $isOptionsActive = in_array($optionsValue, $optionsFromRequest);
85
            $label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
86
            $facet->addOption($this->objectManager->get(Option::class, $facet, $label, $optionsValue, $count, $isOptionsActive, $metricsFromSolrResponse[$optionsValue]));
87
        }
88 55
89
        // after all options have been created we apply a manualSortOrder if configured
90 55
        // the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
91 55
        // need to be handled in the frontend.
92 14
        $this->applyManualSortOrder($facet, $facetConfiguration);
93
        $this->applyReverseOrder($facet, $facetConfiguration);
94
95 52
        if(!is_null($this->dispatcher)) {
96 49
            $this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
97 49
        }
98 49
99
        return $facet;
100
    }
101 52
102
    /**
103
     * @param string $facetName
104
     * @param ResponseAdapter $response
105
     * @return array
106
     */
107
    protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
108
    {
109 55
        $optionsFromSolrResponse = [];
110
        if (!isset($response->facets->{$facetName})) {
111 55
            return $optionsFromSolrResponse;
112
        }
113 55
114 14
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
115
            $optionValue = $bucket->val;
116
            $optionCount = $bucket->count;
117 52
            $optionsFromSolrResponse[$optionValue] = $optionCount;
118 49
        }
119 49
120 49
        return $optionsFromSolrResponse;
121 1
    }
122 49
123
    /**
124
     * @param string $facetName
125
     * @param ResponseAdapter $response
126
     * @return array
127 52
     */
128
    protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
129
    {
130
        $metricsFromSolrResponse = [];
131
132
        if (!isset($response->facets->{$facetName}->buckets)) {
133
            return [];
134
        }
135
136
        foreach ($response->facets->{$facetName}->buckets as $bucket) {
137
            $bucketVariables = get_object_vars($bucket);
138
            foreach ($bucketVariables as $key => $value) {
139
                if (strpos($key, 'metrics_') === 0) {
140
                    $metricsKey = str_replace('metrics_', '', $key);
141
                    $metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
142
                }
143
            }
144
        }
145
146
        return $metricsFromSolrResponse;
147
    }
148
}
149