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

NoResultsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 65.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 124
ccs 49
cts 75
cp 0.6533
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 20 3
B getLabelMarkers() 0 44 1
B getSuggestionResults() 0 24 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
28
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginBase;
29
use ApacheSolrForTypo3\Solr\Plugin\PluginCommand;
30
use ApacheSolrForTypo3\Solr\SpellChecker;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * No Results found view command
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class NoResultsCommand implements PluginCommand
39
{
40
41
    /**
42
     * Parent plugin
43
     *
44
     * @var Results
45
     */
46
    protected $parentPlugin;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param CommandPluginBase $parentPlugin Parent plugin object.
52
     */
53 3
    public function __construct(CommandPluginBase $parentPlugin)
54
    {
55 3
        $this->parentPlugin = $parentPlugin;
0 ignored issues
show
Documentation Bug introduced by
$parentPlugin is of type object<ApacheSolrForTypo...ugin\CommandPluginBase>, but the property $parentPlugin was declared to be of type object<ApacheSolrForTypo...Plugin\Results\Results>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
56 3
    }
57
58 3
    public function execute()
59
    {
60 3
        $spellChecker = GeneralUtility::makeInstance(SpellChecker::class);
61 3
        $suggestionsLink = $spellChecker->getSpellCheckingSuggestions();
62
63 3
        $markers = $this->getLabelMarkers();
64
65 3
        if ($this->parentPlugin->typoScriptConfiguration->getSearchSpellcheckingSearchUsingSpellCheckerSuggestion()) {
66 1
            $suggestionResults =  $this->getSuggestionResults();
67 1
            $markers['suggestion_results'] = $suggestionResults;
68 1
            $markers['has_suggestion_results'] = trim($suggestionResults) !== '';
69
        }
70
71
        // TODO change to if $spellChecker->hasSuggestions()
72 3
        if (!empty($suggestionsLink)) {
73 2
            $markers['suggestion'] = $suggestionsLink;
74
        }
75
76 3
        return $markers;
77
    }
78
79
    /**
80
     * Constructs label markers.
81
     *
82
     * @return array Array of label markers.
83
     */
84 3
    protected function getLabelMarkers()
85
    {
86 3
        $spellChecker = GeneralUtility::makeInstance(SpellChecker::class);
87 3
        $searchWord = $this->parentPlugin->getCleanUserQuery();
88
89 3
        $nothingFound = strtr(
90 3
            $this->parentPlugin->pi_getLL('no_results_nothing_found'),
91
            array(
92 3
                '@searchWord' => $searchWord
93
            )
94
        );
95
96 3
        $showingResultsSuggestion = strtr(
97 3
            $this->parentPlugin->pi_getLL('no_results_showing_results_suggestion'),
98
            array(
99 3
                '@suggestedWord' => $spellChecker->getCollatedSuggestion()
100
            )
101
        );
102
103
        # TODO add link to execute query
104 3
        $searchForOriginal = strtr(
105 3
            $this->parentPlugin->pi_getLL('no_results_search_for_original'),
106
            array(
107 3
                '@searchWord' => $searchWord
108
            )
109
        );
110
111 3
        $searchedFor = strtr(
112 3
            $this->parentPlugin->pi_getLL('results_searched_for'),
113
            array(
114 3
                '@searchWord' => $searchWord
115
            )
116
        );
117
118
        $markers = array(
119 3
            'query' => $searchWord,
120 3
            'nothing_found' => $nothingFound,
121 3
            'showing_results_suggestion' => $showingResultsSuggestion,
122 3
            'search_for_original' => $searchForOriginal,
123 3
            'searched_for' => $searchedFor,
124
        );
125
126 3
        return $markers;
127
    }
128
129
    /**
130
     * Gets the results for the suggested keywords.
131
     *
132
     * Conducts a new search using the suggested keywords and uses that search
133
     * to render the regular results command.
134
     *
135
     * @return string The rendered results command for the results of the suggested keywords.
136
     */
137 1
    protected function getSuggestionResults()
138
    {
139 1
        $spellChecker = GeneralUtility::makeInstance(SpellChecker::class);
140 1
        $suggestedKeywords = $spellChecker->getCollatedSuggestion();
141 1
        $suggestionResults = '';
142
143 1
        if (empty($suggestedKeywords)) {
144
            return $suggestionResults;
145
        }
146
147 1
        $plugin = $this->parentPlugin;
148 1
        $searchResultSetService = $this->parentPlugin->getSearchResultSetService();
149
        // repeat the last search with suggestion as query string
150 1
        $usedRequest = $searchResultSetService->getLastResultSet()->getUsedSearchRequest();
151 1
        $usedRequest->setRawQueryString($suggestedKeywords);
152 1
        $searchResultSetService->search($usedRequest);
153
154 1
        $resultsCommand = GeneralUtility::makeInstance(ResultsCommand::class, $plugin
155
        );
156 1
        $commandVariables = $resultsCommand->execute();
157 1
        $suggestionResults = $plugin->renderCommand('results', $commandVariables);
158
159 1
        return $suggestionResults;
160
    }
161
}
162