Passed
Push — master ( e8fd7e...af5cc1 )
by Timo
27:05 queued 18:51
created

HierarchyFacetParser::getPathAsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy;
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\ParsingUtil;
20
21
/**
22
 * Class OptionsFacetParser
23
 */
24
class HierarchyFacetParser extends AbstractFacetParser
25
{
26
    /**
27
     * @param SearchResultSet $resultSet
28
     * @param string $facetName
29
     * @param array $facetConfiguration
30
     * @return HierarchyFacet|null
31
     */
32 39
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
33
    {
34 39
        $response = $resultSet->getResponse();
35 39
        $fieldName = $facetConfiguration['field'];
36 39
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
37 39
        $optionsFromSolrResponse = isset($response->facet_counts->facet_fields->{$fieldName}) ? ParsingUtil::getMapArrayFromFlatArray($response->facet_counts->facet_fields->{$fieldName}) : [];
38 39
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
39 39
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
40 39
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
41 39
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
42 39
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
43
44 39
        if ($hasNoOptionsToShow && $hideEmpty) {
45 5
            return null;
46
        }
47
48
        /** @var $facet HierarchyFacet */
49 36
        $facet = $this->objectManager->get(HierarchyFacet::class, $resultSet, $facetName, $fieldName, $label, $facetConfiguration);
50
51 36
        $hasActiveOptions = count($optionsFromRequest) > 0;
52 36
        $facet->setIsUsed($hasActiveOptions);
53
54 36
        $facet->setIsAvailable($hasOptionsInResponse);
55
56 36
        $nodesToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
57
58 36
        if ($this->facetOptionsMustBeResorted($facetConfiguration)) {
59 1
            $nodesToCreate = $this->sortFacetOptionsInNaturalOrder($nodesToCreate);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $nodesToCreate is correct as $this->sortFacetOptionsI...alOrder($nodesToCreate) targeting ApacheSolrForTypo3\Solr\...OptionsInNaturalOrder() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
        }
61
62 36
        foreach ($nodesToCreate as $value => $count) {
63 36
            if ($this->getIsExcludedFacetValue($value, $facetConfiguration)) {
64
                continue;
65
            }
66 36
            $isActive = in_array($value, $optionsFromRequest);
67 36
            $delimiterPosition = strpos($value, '-');
68 36
            $path = substr($value, $delimiterPosition + 1);
69 36
            $pathArray = $this->getPathAsArray($path);
70 36
            $key = array_pop($pathArray);
71 36
            $parentKey = array_pop($pathArray);
72 36
            $value = '/' . $path;
73 36
            $label = $this->getLabelFromRenderingInstructions($key, $count, $facetName, $facetConfiguration);
74
75 36
            $facet->createNode($parentKey, $key, $label, $value, $count, $isActive);
76
        }
77
78 36
        return $facet;
79
    }
80
81
    /**
82
     * Sorts facet options in natural order.
83
     * Options must be sorted in natural order,
84
     * because lower nesting levels must be instantiated first, to serve as parents for higher nested levels.
85
     * See implementation of HierarchyFacet::createNode().
86
     *
87
     * @param array $flatOptionsListForFacet
88
     * @return void sorted list of facet options
89
     */
90 1
    protected function sortFacetOptionsInNaturalOrder(array $flatOptionsListForHierarchyFacet)
91
    {
92 1
        uksort($flatOptionsListForHierarchyFacet, "strnatcmp");
93 1
        return $flatOptionsListForHierarchyFacet;
94
    }
95
96
    /**
97
     * Checks if options must be resorted.
98
     *
99
     * Apache Solr facet.sort can be set globally or per facet.
100
     * Relevant TypoScript paths:
101
     * plugin.tx_solr.search.faceting.sortBy causes facet.sort Apache Solr parameter
102
     * plugin.tx_solr.search.faceting.facets.[facetName].sortBy causes f.<fieldname>.facet.sort parameter
103
     *
104
     * see: https://lucene.apache.org/solr/guide/6_6/faceting.html#Faceting-Thefacet.sortParameter
105
     * see: https://wiki.apache.org/solr/SimpleFacetParameters#facet.sort : "This parameter can be specified on a per field basis."
106
     *
107
     * @param array $facetConfiguration
108
     * @return bool
109
     */
110 36
    protected function facetOptionsMustBeResorted(array $facetConfiguration)
111
    {
112 36
        if (isset($facetConfiguration['sortBy']) && $facetConfiguration['sortBy'] === 'index') {
113 1
            return true;
114
        }
115
116 35
        return false;
117
    }
118
119
    /**
120
     * This method is used to get the path array from a hierarchical facet. It substitutes escaped slashes to keep them
121
     * when they are used inside a facetValue.
122
     *
123
     * @param string $path
124
     * @return array
125
     */
126 36
    protected function getPathAsArray($path)
127
    {
128 36
        $path = str_replace('\/', '@@@', $path);
129 36
        $path = rtrim($path, "/");
130 36
        $segments = explode('/', $path);
131 36
        return array_map(function($item) {
132 36
            return str_replace('@@@', '/', $item);
133 36
        }, $segments);
134
    }
135
136
    /**
137
     * Retrieves the active facetValue for a facet from the search request.
138
     * @param SearchResultSet $resultSet
139
     * @param string $facetName
140
     * @return array
141
     */
142 39
    protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName)
143
    {
144 39
        $activeFacetValues = [];
145 39
        $values = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName);
146
147 39
        foreach (is_array($values) ? $values : [] as $valueFromRequest) {
148
            // Attach the 'depth' param again to the value
149 4
            if (strpos($valueFromRequest, '-') === false) {
150 4
                $valueFromRequest = HierarchyTool::substituteSlashes($valueFromRequest);
151 4
                $valueFromRequest = trim($valueFromRequest, '/');
152 4
                $valueFromRequest = (count(explode('/', $valueFromRequest)) - 1) . '-' . $valueFromRequest . '/';
153 4
                $valueFromRequest = HierarchyTool::unSubstituteSlashes($valueFromRequest);
154
            }
155 4
            $activeFacetValues[] = $valueFromRequest;
156
        }
157 39
        return $activeFacetValues;
158
    }
159
}
160