Passed
Push — master ( 14b490...3d7c7d )
by Timo
23:43
created

AbstractRangeFacetParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 39
dl 0
loc 80
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getParsedFacet() 0 56 8
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased;
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 NumericRangeFacetParser
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
26
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets
27
 */
28
abstract class AbstractRangeFacetParser extends AbstractFacetParser
29
{
30
    /**
31
     * @param SearchResultSet $resultSet
32
     * @param string $facetName
33
     * @param array $facetConfiguration
34
     * @param string $facetClass
35
     * @param string $facetItemClass
36
     * @param string $facetRangeCountClass
37
     * @return AbstractRangeFacet|null
38
     */
39 6
    protected function getParsedFacet(SearchResultSet $resultSet, $facetName, array $facetConfiguration, $facetClass, $facetItemClass, $facetRangeCountClass)
40
    {
41 6
        $fieldName = $facetConfiguration['field'];
42 6
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
43 6
        $activeValue = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
44 6
        $response = $resultSet->getResponse();
45
46 6
        $valuesFromResponse = isset($response->facet_counts->facet_ranges->{$fieldName}) ? get_object_vars($response->facet_counts->facet_ranges->{$fieldName}) : [];
47
48 6
        $facet = $this->objectManager->get(
49 6
            $facetClass,
50 6
            $resultSet,
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Object...ManagerInterface::get() has too many arguments starting with $resultSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        /** @scrutinizer ignore-call */ 
51
        $facet = $this->objectManager->get(

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. Please note the @ignore annotation hint above.

Loading history...
51 6
            $facetName,
52 6
            $fieldName,
53 6
            $label,
54 6
            $facetConfiguration
55
        );
56
57 6
        $facet->setIsAvailable(count($valuesFromResponse) > 0);
58 6
        $facet->setIsUsed(count($activeValue) > 0);
59
60 6
        if (is_array($valuesFromResponse)) {
0 ignored issues
show
introduced by
The condition is_array($valuesFromResponse) is always true.
Loading history...
61 6
            $rangeCounts = [];
62 6
            $allCount = 0;
63
64 6
            $countsFromResponse = isset($valuesFromResponse['counts']) ? ParsingUtil::getMapArrayFromFlatArray($valuesFromResponse['counts']) : [];
65
66 6
            foreach ($countsFromResponse as $rangeCountValue => $count) {
67 5
                $rangeCountValue = $this->parseResponseValue($rangeCountValue);
68 5
                $rangeCount = new $facetRangeCountClass($rangeCountValue, $count);
69 5
                $rangeCounts[] = $rangeCount;
70 5
                $allCount += $count;
71
            }
72
73 6
            $fromInResponse = $this->parseResponseValue($valuesFromResponse['start']);
74 6
            $toInResponse = $this->parseResponseValue($valuesFromResponse['end']);
75
76 6
            if (preg_match('/(-?\d*?)-(-?\d*)/', $activeValue[0], $rawValues) == 1) {
77 5
                $rawFrom = $rawValues[1];
78 5
                $rawTo = $rawValues[2];
79
            } else {
80 1
                $rawFrom = 0;
81 1
                $rawTo = 0;
82
            }
83
84 6
            $from = $this->parseRequestValue($rawFrom);
85 6
            $to = $this->parseRequestValue($rawTo);
86
87 6
            $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'numericRange';
88 6
            $gap = isset($facetConfiguration[$type . '.']['gap']) ? $facetConfiguration[$type . '.']['gap'] : 1;
89
90 6
            $range = new $facetItemClass($facet, $from, $to, $fromInResponse, $toInResponse, $gap, $allCount, $rangeCounts, true);
91 6
            $facet->setRange($range);
92
        }
93
94 6
        return $facet;
95
    }
96
97
    /**
98
     * @param string $requestValue
99
     * @return mixed
100
     */
101
    abstract protected function parseRequestValue($requestValue);
102
103
    /**
104
     * @param string $responseValue
105
     * @return mixed
106
     */
107
    abstract protected function parseResponseValue($responseValue);
108
}
109