Completed
Pull Request — master (#1364)
by Timo
32:18
created

HierarchyFacetParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 92
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 44 7
A getPathAsArray() 0 9 1
A getActiveFacetValuesFromRequest() 0 15 4
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
20
/**
21
 * Class OptionsFacetParser
22
 */
23
class HierarchyFacetParser extends AbstractFacetParser
24
{
25
    /**
26
     * @param SearchResultSet $resultSet
27
     * @param string $facetName
28
     * @param array $facetConfiguration
29
     * @return HierarchyFacet|null
30
     */
31 25
    public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
32
    {
33 25
        $response = $resultSet->getResponse();
34 25
        $fieldName = $facetConfiguration['field'];
35 25
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
36 25
        $optionsFromSolrResponse = isset($response->facet_counts->facet_fields->{$fieldName}) ? get_object_vars($response->facet_counts->facet_fields->{$fieldName}) : [];
37 25
        $optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
38 25
        $hasOptionsInResponse = !empty($optionsFromSolrResponse);
39 25
        $hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
40 25
        $hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
41 25
        $hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
42
43 25
        if ($hasNoOptionsToShow && $hideEmpty) {
44 2
            return null;
45
        }
46
47
        /** @var $facet HierarchyFacet */
48 23
        $facet = $this->objectManager->get(HierarchyFacet::class, $resultSet, $facetName, $fieldName, $label, $facetConfiguration);
0 ignored issues
show
Unused Code introduced by
The call to ObjectManagerInterface::get() has too many arguments starting with $resultSet.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
50 23
        $hasActiveOptions = count($optionsFromRequest) > 0;
51 23
        $facet->setIsUsed($hasActiveOptions);
52
53 23
        $facet->setIsAvailable($hasOptionsInResponse);
54
55 23
        $nodesToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
56
57 23
        foreach ($nodesToCreate as $value => $count) {
58 23
            if ($this->getIsExcludedFacetValue($value, $facetConfiguration)) {
59 23
                continue;
60 23
            }
61 23
            $isActive = in_array($value, $optionsFromRequest);
62 23
            $delimiterPosition = strpos($value, '-');
63 23
            $path = substr($value, $delimiterPosition + 1);
64 23
            $pathArray = $this->getPathAsArray($path);
65 23
            $key = array_pop($pathArray);
66
            $parentKey = array_pop($pathArray);
67 23
            $value = '/' . $path;
68
            $label = $this->getLabelFromRenderingInstructions($key, $count, $facetName, $facetConfiguration);
69
70 23
            $facet->createNode($parentKey, $key, $label, $value, $count, $isActive);
71
        }
72
73
        return $facet;
74
    }
75
76
    /**
77
     * This method is used to get the path array from a hierarchical facet. It substitutes escaped slashes to keep them
78
     * when they are used inside a facetValue.
79
     *
80 23
     * @param string $path
81
     * @return array
82 23
     */
83 23
    protected function getPathAsArray($path)
84 23
    {
85 23
        $path = str_replace('\/', '@@@', $path);
86 23
        $path = rtrim($path, "/");
87 23
        $segments = explode('/', $path);
88
        return array_map(function($item) {
89
            return str_replace('@@@', '/', $item);
90
        }, $segments);
91
    }
92
93
    /**
94
     * Retrieves the active facetValue for a facet from the search request.
95
     * @param SearchResultSet $resultSet
96 25
     * @param string $facetName
97
     * @return array
98 25
     */
99 25
    protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName)
100
    {
101 25
        $activeFacetValues = [];
102
        $values = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName);
103 1
104 1
        foreach (is_array($values) ? $values : [] as $valueFromRequest) {
105 1
            // Attach the 'depth' param again to the value
106
            if (strpos($valueFromRequest, '-') === false) {
107 1
                $valueFromRequest = trim($valueFromRequest, '/');
108
                $valueFromRequest = (count(explode('/', $valueFromRequest)) - 1) . '-' . $valueFromRequest . '/';
109 25
            }
110
            $activeFacetValues[] = $valueFromRequest;
111
        }
112
        return $activeFacetValues;
113
    }
114
}
115