Passed
Pull Request — master (#1308)
by Timo
22:26
created

AbstractFacetParser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 6
dl 0
loc 142
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A injectObjectManager() 0 4 1
A getReUseAbleContentObject() 0 10 2
A getPlainLabelOrApplyCObject() 0 15 3
A getLabelFromRenderingInstructions() 0 13 3
A getActiveFacetValuesFromRequest() 0 7 2
A getMergedFacetValueFromSearchRequestAndSolrResponse() 0 12 3
A applyManualSortOrder() 0 11 2
A applyReverseOrder() 0 10 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets;
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\OptionBased\AbstractOptionsFacet;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22
23
/**
24
 * Class AbstractFacetParser
25
 *
26
 * @author Frans Saris <[email protected]>
27
 * @author Timo Hund <[email protected]>
28
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets
29
 */
30
abstract class AbstractFacetParser implements FacetParserInterface
31
{
32
    /**
33
     * @var ContentObjectRenderer
34
     */
35
    protected static $reUseAbleContentObject;
36
37
    /**
38
     * @var ObjectManagerInterface
39
     */
40
    protected $objectManager;
41
42
    /**
43
     * @param ObjectManagerInterface $objectManager
44
     */
45 52
    public function injectObjectManager(ObjectManagerInterface $objectManager)
46
    {
47 52
        $this->objectManager = $objectManager;
48 52
    }
49
50
    /**
51
     * @return ContentObjectRenderer
52
     */
53 21
    protected function getReUseAbleContentObject()
54
    {
55
        /** @var $contentObject ContentObjectRenderer */
56 21
        if (self::$reUseAbleContentObject !== null) {
57 20
            return self::$reUseAbleContentObject;
58
        }
59
60 21
        self::$reUseAbleContentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
61 21
        return self::$reUseAbleContentObject;
62
    }
63
64
    /**
65
     * @param array $configuration
66
     * @return string
67
     */
68 52
    protected function getPlainLabelOrApplyCObject($configuration)
69
    {
70
        // when no label is configured we return an empty string
71 52
        if (!isset($configuration['label'])) {
72
            return '';
73
        }
74
75
        // when no sub configuration is set, we use the string, configured as label
76 52
        if (!isset($configuration['label.'])) {
77 51
            return $configuration['label'];
78
        }
79
80
        // when label and label. was set, we apply the cObject
81 1
        return $this->getReUseAbleContentObject()->cObjGetSingle($configuration['label'], $configuration['label.']);
82
    }
83
84
    /**
85
     * @param mixed $value
86
     * @param integer $count
87
     * @param string $facetName
88
     * @param array $facetConfiguration
89
     * @return string
90
     */
91 45
    protected function getLabelFromRenderingInstructions($value, $count, $facetName, $facetConfiguration)
92
    {
93 45
        $hasRenderingInstructions = isset($facetConfiguration['renderingInstruction']) && isset($facetConfiguration['renderingInstruction.']);
94 45
        if (!$hasRenderingInstructions) {
95 44
            return $value;
96
        }
97
98 20
        $this->getReUseAbleContentObject()->start(['optionValue' => $value, 'optionCount' => $count, 'facetName' => $facetName]);
99 20
        return $this->getReUseAbleContentObject()->cObjGetSingle(
100 20
            $facetConfiguration['renderingInstruction'],
101 20
            $facetConfiguration['renderingInstruction.']
102
        );
103
    }
104
105
106
    /**
107
     * Retrieves the active facetValue for a facet from the search request.
108
     * @param SearchResultSet $resultSet
109
     * @param string $facetName
110
     * @return array
111
     */
112 41
    protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName)
113
    {
114 41
        $activeFacetValues = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName);
115 41
        $activeFacetValues = is_array($activeFacetValues) ? $activeFacetValues : [];
116
117 41
        return $activeFacetValues;
118
    }
119
120
121
    /**
122
     * @param array $facetValuesFromSolrResponse
123
     * @param array $facetValuesFromSearchRequest
124
     * @return mixed
125
     */
126 37
    protected function getMergedFacetValueFromSearchRequestAndSolrResponse($facetValuesFromSolrResponse, $facetValuesFromSearchRequest)
127
    {
128 37
        $facetValueItemsToCreate = $facetValuesFromSolrResponse;
129
130 37
        foreach ($facetValuesFromSearchRequest as $valueFromRequest) {
131
            // if we have options in the request that have not been in the response we add them with a count of 0
132 7
            if (!isset($facetValueItemsToCreate[$valueFromRequest])) {
133 7
                $facetValueItemsToCreate[$valueFromRequest] = 0;
134
            }
135
        }
136 37
        return $facetValueItemsToCreate;
137
    }
138
139
    /**
140
     * @param AbstractOptionsFacet $facet
141
     * @param array $facetConfiguration
142
     * @return AbstractOptionsFacet
143
     */
144 42
    protected function applyManualSortOrder(AbstractOptionsFacet $facet, array $facetConfiguration)
145
    {
146 42
        if (!isset($facetConfiguration['manualSortOrder'])) {
147 40
            return $facet;
148
        }
149 3
        $fields = GeneralUtility::trimExplode(',', $facetConfiguration['manualSortOrder']);
150 3
        $sortedOptions = $facet->getOptions()->getManualSortedCopy($fields);
151 3
        $facet->setOptions($sortedOptions);
0 ignored issues
show
Compatibility introduced by
$sortedOptions of type object<ApacheSolrForTypo...actFacetItemCollection> is not a sub-type of object<ApacheSolrForTypo...Based\OptionCollection>. It seems like you assume a child class of the class ApacheSolrForTypo3\Solr\...ractFacetItemCollection to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
152
153 3
        return $facet;
154
    }
155
156
    /**
157
     * @param AbstractOptionsFacet $facet
158
     * @param array $facetConfiguration
159
     * @return AbstractOptionsFacet
160
     */
161 42
    protected function applyReverseOrder(AbstractOptionsFacet $facet, array $facetConfiguration)
162
    {
163 42
        if (empty($facetConfiguration['reverseOrder'])) {
164 40
            return $facet;
165
        }
166
167 2
        $facet->setOptions($facet->getOptions()->getReversedOrderCopy());
0 ignored issues
show
Compatibility introduced by
$facet->getOptions()->getReversedOrderCopy() of type object<ApacheSolrForTypo...actFacetItemCollection> is not a sub-type of object<ApacheSolrForTypo...Based\OptionCollection>. It seems like you assume a child class of the class ApacheSolrForTypo3\Solr\...ractFacetItemCollection to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
168
169 2
        return $facet;
170
    }
171
}
172