AbstractRangeFacetParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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
    protected function getParsedFacet(SearchResultSet $resultSet, $facetName, array $facetConfiguration, $facetClass, $facetItemClass, $facetRangeCountClass)
39 6
    {
40
        $fieldName = $facetConfiguration['field'];
41 6
        $label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
42 6
        $activeValue = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
43 6
        $response = $resultSet->getResponse();
44 6
45
        $valuesFromResponse = isset($response->facet_counts->facet_ranges->{$fieldName}) ? get_object_vars($response->facet_counts->facet_ranges->{$fieldName}) : [];
46 6
47
        $facet = $this->objectManager->get(
48 6
            $facetClass,
49 6
            $resultSet,
50 6
            $facetName,
51 6
            $fieldName,
52 6
            $label,
53 6
            $facetConfiguration
54 6
        );
55
56
        $facet->setIsAvailable(count($valuesFromResponse) > 0);
57 6
        $facet->setIsUsed(count($activeValue) > 0);
58 6
59
        if (is_array($valuesFromResponse)) {
0 ignored issues
show
introduced by
The condition is_array($valuesFromResponse) is always true.
Loading history...
60 6
            $rangeCounts = [];
61 6
            $allCount = 0;
62 6
63
            $countsFromResponse = isset($valuesFromResponse['counts']) ? ParsingUtil::getMapArrayFromFlatArray($valuesFromResponse['counts']) : [];
64 6
65
            foreach ($countsFromResponse as $rangeCountValue => $count) {
66 6
                $rangeCountValue = $this->parseResponseValue($rangeCountValue);
67 5
                $rangeCount = $this->objectManager->get($facetRangeCountClass, $rangeCountValue, $count);
68 5
                $rangeCounts[] = $rangeCount;
69 5
                $allCount += $count;
70 5
            }
71
72
            $fromInResponse = $this->parseResponseValue($valuesFromResponse['start']);
73 6
            $toInResponse = $this->parseResponseValue($valuesFromResponse['end']);
74 6
75
            if (preg_match('/(-?\d*?)-(-?\d*)/', $activeValue[0], $rawValues) == 1) {
76 6
                $rawFrom = $rawValues[1];
77 5
                $rawTo = $rawValues[2];
78 5
            } else {
79
                $rawFrom = 0;
80 1
                $rawTo = 0;
81 1
            }
82
83
            $from = $this->parseRequestValue($rawFrom);
84 6
            $to = $this->parseRequestValue($rawTo);
85 6
86
            $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'numericRange';
87 6
            $gap = isset($facetConfiguration[$type . '.']['gap']) ? $facetConfiguration[$type . '.']['gap'] : 1;
88 6
89
            $range = $this->objectManager->get($facetItemClass, $facet, $from, $to, $fromInResponse, $toInResponse, $gap, $allCount, $rangeCounts, true);
90 6
            $facet->setRange($range);
91 6
        }
92
93
        return $facet;
94 6
    }
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