Completed
Push — master ( 694264...44bc0a )
by Timo
11s
created

UsedFacetRenderer::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 2.1798

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 29
cts 45
cp 0.6444
rs 9.6818
c 0
b 0
f 0
cc 2
eloc 37
nc 2
nop 0
crap 2.1798

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace ApacheSolrForTypo3\Solr\Facet;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2011 Markus Goldbach <[email protected]>
8
 *  (c) 2012-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Query;
29
use ApacheSolrForTypo3\Solr\Query\FilterEncoder\Hierarchy;
30
use ApacheSolrForTypo3\Solr\Template;
31
use ApacheSolrForTypo3\Solr\Util;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
34
35
/**
36
 * Renderer for Used Facets.
37
 *
38
 * FIXME merge into default renderer as renderUsedFacetOption()
39
 *
40
 * @author Markus Goldbach <[email protected]>
41
 * @author Ingo Renner <[email protected]>
42
 */
43
class UsedFacetRenderer extends SimpleFacetOptionsRenderer
44
{
45
46
    /**
47
     * The name of the facet the filter is applied to.
48
     *
49
     * @var string
50
     */
51
    protected $filter;
52
53
    /**
54
     * The filter value that has been applied to a query.
55
     *
56
     * @var string
57
     */
58
    protected $filterValue;
59
60
    /**
61
     * Constructor
62
     *
63
     * @param string $facetName
64
     * @param array $filterValue
65
     * @param \ApacheSolrForTypo3\Solr\Template $filter
66
     * @param \ApacheSolrForTypo3\Solr\Template $template
67
     * @param \ApacheSolrForTypo3\Solr\Query $query
68
     */
69 1
    public function __construct(
70
        $facetName,
71
        $filterValue,
72
        $filter,
73
        Template $template,
74
        Query $query
75
    ) {
76 1
        parent::__construct($facetName, array(), $template, $query);
77
78 1
        $this->filter = $filter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filter of type object<ApacheSolrForTypo3\Solr\Template> is incompatible with the declared type string of property $filter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79 1
        $this->filterValue = $filterValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filterValue of type array is incompatible with the declared type string of property $filterValue.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80 1
    }
81
82
    /**
83
     * Renders the block of used / applied facets.
84
     *
85
     * @return string Rendered HTML representing the used facet.
86
     */
87 1
    public function render()
88
    {
89 1
        $solrConfiguration = Util::getSolrConfiguration();
90
91 1
        $facetOption = GeneralUtility::makeInstance(FacetOption::class,
92 1
            $this->facetName,
93 1
            $this->filterValue
94
        );
95
96 1
        $facetLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class,
97 1
            $this->query,
98 1
            $this->facetName,
99
            $facetOption
100
        );
101
        /* @var $facetLinkBuilder LinkBuilder */
102 1
        $facetLinkBuilder->setLinkTargetPageId($this->linkTargetPageId);
103
104 1
        if ($this->facetConfiguration['type'] == 'hierarchy') {
105
            // FIXME decouple this
106
            $filterEncoder = GeneralUtility::makeInstance(Hierarchy::class);
107
            $facet = GeneralUtility::makeInstance(Facet::class, $this->facetName);
108
            $facetRenderer = GeneralUtility::makeInstance(HierarchicalFacetRenderer::class, $facet);
109
110
            $facetText = $facetRenderer->getLastPathSegmentFromHierarchicalFacetOption($filterEncoder->decodeFilter($this->filterValue));
111
        } else {
112 1
            $facetText = $facetOption->render();
113
        }
114
115 1
        $facetText = $this->getModifiedFacetTextFromHook($facetText);
116
117 1
        $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
118 1
        $facetConfiguration = $solrConfiguration->getSearchFacetingFacetByName($this->facetName);
119 1
        $facetLabel = $contentObject->stdWrap($facetConfiguration['label'], $facetConfiguration['label.']);
120
121 1
        $removeFacetText = strtr(
122 1
            $solrConfiguration->getSearchFacetingRemoveFacetLinkText(),
123
            array(
124 1
                '@facetValue' => $this->filterValue,
125 1
                '@facetName' => $this->facetName,
126 1
                '@facetLabel' => $facetLabel,
127 1
                '@facetText' => $facetText
128
            )
129
        );
130
131 1
        $removeFacetLink = $facetLinkBuilder->getRemoveFacetOptionLink($removeFacetText);
132 1
        $removeFacetUrl = $facetLinkBuilder->getRemoveFacetOptionUrl();
133
134
        $facetToRemove = array(
135 1
            'link' => $removeFacetLink,
136 1
            'url' => $removeFacetUrl,
137 1
            'text' => $removeFacetText,
138 1
            'value' => $this->filterValue,
139 1
            'facet_name' => $this->facetName
140
        );
141
142 1
        return $facetToRemove;
143
    }
144
145
    /**
146
     * Provides a hook to overwrite the facetText.
147
     *
148
     * @param string $facetText
149
     * @return mixed
150
     */
151 1
    protected function getModifiedFacetTextFromHook($facetText)
152
    {
153 1
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['processUsedFacetText'])) {
154 1
            return $facetText;
155
        }
156
157
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['processUsedFacetText'] as $classReference) {
158
            $usedFacetOptionsRenderer = GeneralUtility::getUserObj($classReference);
159
            $params = array(
160
                'facetName' => $this->facetName,
161
                'facetValue' => $this->filterValue,
162
                'facetConfiguration' => $this->facetConfiguration,
163
                'facetText' => $facetText
164
            );
165
166
            if (!$usedFacetOptionsRenderer instanceof UsedFacetOptionsRenderer) {
167
                $message = 'Invalid hook configured in processUsedFacetText. Hook needs to implement Interface UsedFacetOptionsRenderer!';
168
                throw new \InvalidArgumentException($message);
169
            }
170
171
            $newText = $usedFacetOptionsRenderer->getUsedFacetText($params, $this);
172
173
            if (!empty($newText)) {
174
                $facetText = $newText;
175
            }
176
        }
177
178
        return $facetText;
179
    }
180
}
181