Passed
Push — master ( ae02be...f196a8 )
by Timo
21:07
created

AbstractRangeFacetParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 79
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getParsedFacet() 0 55 8
parseRequestValue() 0 1 ?
parseResponseValue() 0 1 ?
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
20
/**
21
 * Class NumericRangeFacetParser
22
 *
23
 * @author Frans Saris <[email protected]>
24
 * @author Timo Hund <[email protected]>
25
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets
26
 */
27
abstract class AbstractRangeFacetParser extends AbstractFacetParser
28
{
29
    /**
30
     * @param SearchResultSet $resultSet
31
     * @param string $facetName
32
     * @param array $facetConfiguration
33
     * @param string $facetClass
34
     * @param string $facetItemClass
35
     * @param string $facetRangeCountClass
36
     * @return AbstractRangeFacet|null
37
     */
38 6
    protected function getParsedFacet(SearchResultSet $resultSet, $facetName, array $facetConfiguration, $facetClass, $facetItemClass, $facetRangeCountClass)
39
    {
40 6
        $fieldName = $facetConfiguration['field'];
41 6
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
42 6
        $activeValue = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
43 6
        $response = $resultSet->getResponse();
44 6
        $valuesFromResponse = isset($response->facet_counts->facet_ranges->{$fieldName}) ? get_object_vars($response->facet_counts->facet_ranges->{$fieldName}) : [];
45
46 6
        $facet = $this->objectManager->get(
47
            $facetClass,
48
            $resultSet,
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
            $facetName,
50
            $fieldName,
51
            $label,
52
            $facetConfiguration
53
        );
54
55 6
        $facet->setIsAvailable(count($valuesFromResponse) > 0);
56 6
        $facet->setIsUsed(count($activeValue) > 0);
57
58 6
        if (is_array($valuesFromResponse)) {
59 6
            $rangeCounts = [];
60 6
            $allCount = 0;
61
62 6
            $countsFromResponse = isset($valuesFromResponse['counts']) ? get_object_vars($valuesFromResponse['counts']) : [];
63 6
            foreach ($countsFromResponse as $rangeCountValue => $count) {
64 5
                $rangeCountValue = $this->parseResponseValue($rangeCountValue);
65 5
                $rangeCount = new $facetRangeCountClass($rangeCountValue, $count);
66 5
                $rangeCounts[] = $rangeCount;
67 5
                $allCount += $count;
68
            }
69
70 6
            $fromInResponse = $this->parseResponseValue($valuesFromResponse['start']);
71 6
            $toInResponse = $this->parseResponseValue($valuesFromResponse['end']);
72
73 6
            if (preg_match('/(-?\d*?)-(-?\d*)/', $activeValue[0], $rawValues) == 1) {
74 5
                $rawFrom = $rawValues[1];
75 5
                $rawTo = $rawValues[2];
76
            } else {
77 1
                $rawFrom = 0;
78 1
                $rawTo = 0;
79
            }
80
81 6
            $from = $this->parseRequestValue($rawFrom);
82 6
            $to = $this->parseRequestValue($rawTo);
83
84 6
            $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'numericRange';
85 6
            $gap = isset($facetConfiguration[$type . '.']['gap']) ? $facetConfiguration[$type . '.']['gap'] : 1;
86
87 6
            $range = new $facetItemClass($facet, $from, $to, $fromInResponse, $toInResponse, $gap, $allCount, $rangeCounts, true);
88 6
            $facet->setRange($range);
89
        }
90
91 6
        return $facet;
92
    }
93
94
    /**
95
     * @param string $requestValue
96
     * @return mixed
97
     */
98
    abstract protected function parseRequestValue($requestValue);
99
100
    /**
101
     * @param string $responseValue
102
     * @return mixed
103
     */
104
    abstract protected function parseResponseValue($responseValue);
105
}
106