Passed
Pull Request — master (#1378)
by Timo
19:41 queued 20s
created

getLabelFromRenderingInstructions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 4
crap 3
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 57
    public function injectObjectManager(ObjectManagerInterface $objectManager)
46
    {
47 57
        $this->objectManager = $objectManager;
48 57
    }
49
50
    /**
51
     * @return ContentObjectRenderer
52
     */
53 22
    protected function getReUseAbleContentObject()
54
    {
55
        /** @var $contentObject ContentObjectRenderer */
56 22
        if (self::$reUseAbleContentObject !== null) {
57 21
            return self::$reUseAbleContentObject;
58
        }
59
60 22
        self::$reUseAbleContentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
61 22
        return self::$reUseAbleContentObject;
62
    }
63
64
    /**
65
     * @param array $configuration
66
     * @return string
67
     */
68 57
    protected function getPlainLabelOrApplyCObject($configuration)
69
    {
70
        // when no label is configured we return an empty string
71 57
        if (!isset($configuration['label'])) {
72
            return '';
73
        }
74
75
        // when no sub configuration is set, we use the string, configured as label
76 57
        if (!isset($configuration['label.'])) {
77 56
            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 50
    protected function getLabelFromRenderingInstructions($value, $count, $facetName, $facetConfiguration)
92
    {
93 50
        $hasRenderingInstructions = isset($facetConfiguration['renderingInstruction']) && isset($facetConfiguration['renderingInstruction.']);
94 50
        if (!$hasRenderingInstructions) {
95 49
            return $value;
96
        }
97
98 21
        $this->getReUseAbleContentObject()->start(['optionValue' => $value, 'optionCount' => $count, 'facetName' => $facetName]);
99 21
        return $this->getReUseAbleContentObject()->cObjGetSingle(
100 21
            $facetConfiguration['renderingInstruction'],
101 21
            $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 46
    protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName)
113
    {
114 46
        $activeFacetValues = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName);
115 46
        $activeFacetValues = is_array($activeFacetValues) ? $activeFacetValues : [];
116
117 46
        return $activeFacetValues;
118
    }
119
120
121
    /**
122
     * @param array $facetValuesFromSolrResponse
123
     * @param array $facetValuesFromSearchRequest
124
     * @return mixed
125
     */
126 42
    protected function getMergedFacetValueFromSearchRequestAndSolrResponse($facetValuesFromSolrResponse, $facetValuesFromSearchRequest)
127
    {
128 42
        $facetValueItemsToCreate = $facetValuesFromSolrResponse;
129
130 42
        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 8
            if (!isset($facetValueItemsToCreate[$valueFromRequest])) {
133 8
                $facetValueItemsToCreate[$valueFromRequest] = 0;
134
            }
135
        }
136 42
        return $facetValueItemsToCreate;
137
    }
138
139
    /**
140
     * @param AbstractOptionsFacet $facet
141
     * @param array $facetConfiguration
142
     * @return AbstractOptionsFacet
143
     */
144 47
    protected function applyManualSortOrder(AbstractOptionsFacet $facet, array $facetConfiguration)
145
    {
146 47
        if (!isset($facetConfiguration['manualSortOrder'])) {
147 45
            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 47
    protected function applyReverseOrder(AbstractOptionsFacet $facet, array $facetConfiguration)
162
    {
163 47
        if (empty($facetConfiguration['reverseOrder'])) {
164 45
            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
    /**
173
     * @param mixed $value
174
     * @param array $facetConfiguration
175
     * @return boolean
176
     */
177 50
    protected function getIsExcludedFacetValue($value, array $facetConfiguration)
178
    {
179 50
        if (!isset($facetConfiguration['excludeValues'])) {
180 49
            return false;
181
        }
182
183 1
        $excludedValue = GeneralUtility::trimExplode(',', $facetConfiguration['excludeValues']);
184 1
        return in_array($value, $excludedValue);
185
    }
186
}
187