Passed
Push — master ( 498335...52b5ef )
by Rafael
42:19
created

AbstractRangeFacetParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 71.1%

Importance

Changes 0
Metric Value
wmc 8
eloc 39
dl 0
loc 80
ccs 32
cts 45
cp 0.711
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 AbstractRangeFacetParser
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
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 1
    protected function getParsedFacet(SearchResultSet $resultSet, $facetName, array $facetConfiguration, $facetClass, $facetItemClass, $facetRangeCountClass)
39
    {
40 1
        $fieldName = $facetConfiguration['field'];
41 1
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
42 1
        $activeValue = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
43 1
        $response = $resultSet->getResponse();
44
45 1
        $valuesFromResponse = isset($response->facet_counts->facet_ranges->{$fieldName}) ? get_object_vars($response->facet_counts->facet_ranges->{$fieldName}) : [];
46
47 1
        $facet = $this->objectManager->get(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

47
        $facet = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48 1
            $facetClass,
49 1
            $resultSet,
50 1
            $facetName,
51 1
            $fieldName,
52 1
            $label,
53 1
            $facetConfiguration
54
        );
55
56 1
        $facet->setIsAvailable(count($valuesFromResponse) > 0);
57 1
        $facet->setIsUsed(count($activeValue) > 0);
58
59 1
        if (is_array($valuesFromResponse)) {
0 ignored issues
show
introduced by
The condition is_array($valuesFromResponse) is always true.
Loading history...
60 1
            $rangeCounts = [];
61 1
            $allCount = 0;
62
63 1
            $countsFromResponse = isset($valuesFromResponse['counts']) ? ParsingUtil::getMapArrayFromFlatArray($valuesFromResponse['counts']) : [];
64
65 1
            foreach ($countsFromResponse as $rangeCountValue => $count) {
66
                $rangeCountValue = $this->parseResponseValue($rangeCountValue);
67
                $rangeCount = $this->objectManager->get($facetRangeCountClass, $rangeCountValue, $count);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

67
                $rangeCount = /** @scrutinizer ignore-deprecated */ $this->objectManager->get($facetRangeCountClass, $rangeCountValue, $count);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
68
                $rangeCounts[] = $rangeCount;
69
                $allCount += $count;
70
            }
71
72 1
            $fromInResponse = $this->parseResponseValue($valuesFromResponse['start']);
73 1
            $toInResponse = $this->parseResponseValue($valuesFromResponse['end']);
74
75 1
            if (preg_match('/(-?\d*?)-(-?\d*)/', $activeValue[0], $rawValues) == 1) {
76
                $rawFrom = $rawValues[1];
77
                $rawTo = $rawValues[2];
78
            } else {
79 1
                $rawFrom = 0;
80 1
                $rawTo = 0;
81
            }
82
83 1
            $from = $this->parseRequestValue($rawFrom);
84 1
            $to = $this->parseRequestValue($rawTo);
85
86 1
            $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'numericRange';
87 1
            $gap = isset($facetConfiguration[$type . '.']['gap']) ? $facetConfiguration[$type . '.']['gap'] : 1;
88
89 1
            $range = $this->objectManager->get($facetItemClass, $facet, $from, $to, $fromInResponse, $toInResponse, $gap, $allCount, $rangeCounts, true);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

89
            $range = /** @scrutinizer ignore-deprecated */ $this->objectManager->get($facetItemClass, $facet, $from, $to, $fromInResponse, $toInResponse, $gap, $allCount, $rangeCounts, true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90 1
            $facet->setRange($range);
91
        }
92
93 1
        return $facet;
94
    }
95
96
    /**
97
     * @param string $requestValue
98
     * @return mixed
99
     */
100
    abstract protected function parseRequestValue($requestValue);
101
102
    /**
103
     * @param string $responseValue
104
     * @return mixed
105
     */
106
    abstract protected function parseResponseValue($responseValue);
107
}
108