Passed
Pull Request — release-11.2.x (#3594)
by Markus
14:43 queued 10:47
created

AbstractRangeFacetParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 8
eloc 39
dl 0
loc 81
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getParsedFacet() 0 57 8
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetParser;
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased\DateRange\DateRangeFacet;
21
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased\NumericRange\NumericRangeFacet;
22
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
23
use ApacheSolrForTypo3\Solr\System\Solr\ParsingUtil;
24
25
/**
26
 * Class AbstractRangeFacetParser
27
 *
28
 * @author Frans Saris <[email protected]>
29
 * @author Timo Hund <[email protected]>
30
 */
31
abstract class AbstractRangeFacetParser extends AbstractFacetParser
32
{
33
    /**
34
     * @param SearchResultSet $resultSet
35
     * @param string $facetName
36
     * @param array $facetConfiguration
37
     * @param string $facetClass
38
     * @param string $facetItemClass
39
     * @param string $facetRangeCountClass
40
     * @return AbstractFacet|null
41 5
     */
42
    protected function getParsedFacet(SearchResultSet $resultSet, $facetName, array $facetConfiguration, $facetClass, $facetItemClass, $facetRangeCountClass)
43 5
    {
44 5
        $fieldName = $facetConfiguration['field'];
45 5
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
46 5
        $activeValue = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
47
        $response = $resultSet->getResponse();
48 5
49
        $valuesFromResponse = isset($response->facet_counts->facet_ranges->{$fieldName}) ? get_object_vars($response->facet_counts->facet_ranges->{$fieldName}) : [];
50
51 5
        /** @var NumericRangeFacet|DateRangeFacet $facet */
52
        $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

52
        $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...
53
            $facetClass,
54
            $resultSet,
55
            $facetName,
56
            $fieldName,
57 5
            $label,
58
            $facetConfiguration
59
        );
60 5
61 5
        $facet->setIsAvailable(count($valuesFromResponse) > 0);
62
        $facet->setIsUsed(count($activeValue) > 0);
63 5
64 5
        if (is_array($valuesFromResponse)) {
0 ignored issues
show
introduced by
The condition is_array($valuesFromResponse) is always true.
Loading history...
65 5
            $rangeCounts = [];
66
            $allCount = 0;
67 5
68
            $countsFromResponse = isset($valuesFromResponse['counts']) ? ParsingUtil::getMapArrayFromFlatArray($valuesFromResponse['counts']) : [];
69 5
70 5
            foreach ($countsFromResponse as $rangeCountValue => $count) {
71 5
                $rangeCountValue = $this->parseResponseValue($rangeCountValue);
72 5
                $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

72
                $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...
73 5
                $rangeCounts[] = $rangeCount;
74
                $allCount += $count;
75
            }
76 5
77 5
            $fromInResponse = $this->parseResponseValue($valuesFromResponse['start']);
78
            $toInResponse = $this->parseResponseValue($valuesFromResponse['end']);
79 5
80 5
            if (preg_match('/(-?\d*?)-(-?\d*)/', $activeValue[0], $rawValues) == 1) {
81 5
                $rawFrom = $rawValues[1];
82
                $rawTo = $rawValues[2];
83
            } else {
84
                $rawFrom = 0;
85
                $rawTo = 0;
86
            }
87 5
88 5
            $from = $this->parseRequestValue($rawFrom);
89
            $to = $this->parseRequestValue($rawTo);
90 5
91 5
            $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'numericRange';
92
            $gap = isset($facetConfiguration[$type . '.']['gap']) ? $facetConfiguration[$type . '.']['gap'] : 1;
93 5
94 5
            $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

94
            $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...
95
            $facet->setRange($range);
96
        }
97 5
98
        return $facet;
99
    }
100
101
    /**
102
     * @param string $requestValue
103
     * @return mixed
104
     */
105
    abstract protected function parseRequestValue($requestValue);
106
107
    /**
108
     * @param string $responseValue
109
     * @return mixed
110
     */
111
    abstract protected function parseResponseValue($responseValue);
112
}
113