Failed Conditions
Push — master ( a052b2...ee9c7d )
by Rafael
18:58
created

AbstractBaseController::getSearchRequestBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2.3149
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller;
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\ConnectionManager;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
19
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder;
20
use ApacheSolrForTypo3\Solr\Search;
21
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
22
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
23
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
24
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
25
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager as SolrConfigurationManager;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
28
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
29
use TYPO3\CMS\Extbase\Service\TypoScriptService;
30
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
31
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
32
33
/**
34
 * Class AbstractBaseController
35
 *
36
 * @author Frans Saris <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 * @package ApacheSolrForTypo3\Solr\Controller
39
 */
40
abstract class AbstractBaseController extends ActionController
41
{
42
    /**
43
     * @var ContentObjectRenderer
44
     */
45
    private $contentObjectRenderer;
46
47
    /**
48
     * @var TypoScriptFrontendController
49
     */
50
    protected $typoScriptFrontendController;
51
52
    /**
53
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
54
     */
55
    protected $configurationManager;
56
57
    /**
58
     * @var SolrConfigurationManager
59
     */
60
    private $solrConfigurationManager;
61
62
    /**
63
     * The configuration is private if you need it please get it from the controllerContext.
64
     *
65
     * @var TypoScriptConfiguration
66
     */
67
    protected $typoScriptConfiguration;
68
69
    /**
70
     * @var \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
71
     */
72
    protected $controllerContext;
73
74
    /**
75
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService
76
     */
77
    protected $searchService;
78
79
    /**
80
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder
81
     */
82
    protected $searchRequestBuilder;
83
84
    /**
85
     * @var bool
86
     */
87
    protected $resetConfigurationBeforeInitialize = true;
88
89
    /**
90
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
91
     * @return void
92
     */
93 36
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
94
    {
95 36
        $this->configurationManager = $configurationManager;
96 36
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
97 36
    }
98
99
    /**
100
     * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer
101
     */
102 1
    public function setContentObjectRenderer($contentObjectRenderer)
103
    {
104 1
        $this->contentObjectRenderer = $contentObjectRenderer;
105 1
    }
106
107
    /**
108
     * @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
109
     */
110
    public function getContentObjectRenderer()
111
    {
112
        return $this->contentObjectRenderer;
113
    }
114
115
    /**
116
     * @param SolrConfigurationManager $configurationManager
117
     */
118 36
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
119
    {
120 36
        $this->solrConfigurationManager = $configurationManager;
121 36
    }
122
123
    /**
124
     * @param boolean $resetConfigurationBeforeInitialize
125
     */
126 17
    public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
127
    {
128 17
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
129 17
    }
130
131
    /**
132
     * Initialize the controller context
133
     *
134
     * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext ControllerContext to be passed to the view
135
     * @api
136
     */
137 36
    protected function buildControllerContext()
138
    {
139
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
140 36
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
141 36
        $controllerContext->setRequest($this->request);
142 36
        $controllerContext->setResponse($this->response);
143 36
        if ($this->arguments !== null) {
144 36
            $controllerContext->setArguments($this->arguments);
145
        }
146 36
        $controllerContext->setUriBuilder($this->uriBuilder);
147
148 36
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
149
150 36
        return $controllerContext;
151
    }
152
153
    /**
154
     * Initialize action
155
     */
156 36
    protected function initializeAction()
157
    {
158
        // Reset configuration (to reset flexform overrides) if resetting is enabled
159 36
        if ($this->resetConfigurationBeforeInitialize) {
160 19
            $this->solrConfigurationManager->reset();
161
        }
162
        /** @var TypoScriptService $typoScriptService */
163 36
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
164
165
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
166 36
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
167 36
        $pluginSettings = [];
168 36
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
169 36
            if (isset($frameWorkConfiguration[$key])) {
170 36
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
171
            }
172
        }
173
174 36
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
175 36
        if ($pluginSettings !== []) {
176
            $this->typoScriptConfiguration->mergeSolrConfiguration(
177
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
178
                true,
179
                false
180
            );
181
        }
182
183 36
        $this->objectManager->get(ConfigurationService::class)
184 36
            ->overrideConfigurationWithFlexFormSettings(
185 36
                $this->contentObjectRenderer->data['pi_flexform'],
186 36
                $this->typoScriptConfiguration
187
            );
188
189 36
        parent::initializeAction();
190 36
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
191 36
        $this->initializeSettings();
192 36
        $this->initializeSearch();
193 36
    }
194
195
    /**
196
     * Inject settings of plugin.tx_solr
197
     *
198
     * @return void
199
     */
200 36
    protected function initializeSettings()
201
    {
202
        /** @var $typoScriptService TypoScriptService */
203 36
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
204
205
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
206 36
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
207 36
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
208
        );
209 36
    }
210
211
    /**
212
     * Initialize the Solr connection and
213
     * test the connection through a ping
214
     */
215 36
    protected function initializeSearch()
216
    {
217
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
218 36
        $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, $this->typoScriptFrontendController->sys_language_uid, $this->typoScriptFrontendController->MP);
219 36
        $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
220
221 36
        $this->searchService = GeneralUtility::makeInstance(SearchResultSetService::class, $this->typoScriptConfiguration, $search);
222 36
    }
223
224
    /**
225
     * @return SearchRequestBuilder
226
     */
227 31
    protected function getSearchRequestBuilder()
228
    {
229 31
        if ($this->searchRequestBuilder === null) {
230 31
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, $this->typoScriptConfiguration);
231
        }
232
233 31
        return $this->searchRequestBuilder;
234
    }
235
236
    /**
237
     * Called when the solr server is unavailable.
238
     *
239
     * @return void
240
     */
241 2
    protected function handleSolrUnavailable()
242
    {
243 2
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
244
            /** @var SolrLogManager $logger */
245 2
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
246 2
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
247
        }
248 2
    }
249
}
250