Passed
Push — master ( e55157...ed38f6 )
by Timo
27:41
created

AbstractBaseController::getContentObjectRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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\TypoScript\TypoScriptService;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
29
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
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 43
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
94
    {
95 43
        $this->configurationManager = $configurationManager;
96 43
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
97 43
    }
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 43
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
119
    {
120 43
        $this->solrConfigurationManager = $configurationManager;
121 43
    }
122
123
    /**
124
     * @param boolean $resetConfigurationBeforeInitialize
125
     */
126 22
    public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
127
    {
128 22
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
129 22
    }
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 43
    protected function buildControllerContext()
138
    {
139
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
140 43
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
141 43
        $controllerContext->setRequest($this->request);
142 43
        $controllerContext->setResponse($this->response);
143 43
        if ($this->arguments !== null) {
144 43
            $controllerContext->setArguments($this->arguments);
145
        }
146 43
        $controllerContext->setUriBuilder($this->uriBuilder);
147
148 43
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
149
150 43
        return $controllerContext;
151
    }
152
153
    /**
154
     * Initialize action
155
     */
156 43
    protected function initializeAction()
157
    {
158
        // Reset configuration (to reset flexform overrides) if resetting is enabled
159 43
        if ($this->resetConfigurationBeforeInitialize) {
160 21
            $this->solrConfigurationManager->reset();
161
        }
162
        /** @var TypoScriptService $typoScriptService */
163 43
        $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 43
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
167 43
        $pluginSettings = [];
168 43
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
169 43
            if (isset($frameWorkConfiguration[$key])) {
170 43
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
171
            }
172
        }
173
174 43
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
175 43
        if ($pluginSettings !== []) {
176 2
            $this->typoScriptConfiguration->mergeSolrConfiguration(
177 2
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
178 2
                true,
179 2
                false
180
            );
181
        }
182
183 43
        $this->objectManager->get(ConfigurationService::class)
184 43
            ->overrideConfigurationWithFlexFormSettings(
185 43
                $this->contentObjectRenderer->data['pi_flexform'],
186 43
                $this->typoScriptConfiguration
187
            );
188
189 43
        parent::initializeAction();
190 43
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
191 43
        $this->initializeSettings();
192 43
        $this->initializeSearch();
193 43
    }
194
195
    /**
196
     * Inject settings of plugin.tx_solr
197
     *
198
     * @return void
199
     */
200 43
    protected function initializeSettings()
201
    {
202
        /** @var $typoScriptService TypoScriptService */
203 43
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
204
205
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
206 43
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
207 43
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
208
        );
209 43
    }
210
211
    /**
212
     * Initialize the Solr connection and
213
     * test the connection through a ping
214
     */
215 43
    protected function initializeSearch()
216
    {
217
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
218 43
        $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, $this->typoScriptFrontendController->sys_language_uid, $this->typoScriptFrontendController->MP);
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

218
        $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, /** @scrutinizer ignore-deprecated */ $this->typoScriptFrontendController->sys_language_uid, $this->typoScriptFrontendController->MP);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
219 43
        $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
220
221 43
        $this->searchService = GeneralUtility::makeInstance(
222 43
            SearchResultSetService::class,
223 43
            /** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
224 43
            /** @scrutinizer ignore-type */ $search
225
        );
226 43
    }
227
228
    /**
229
     * @return SearchRequestBuilder
230
     */
231 38
    protected function getSearchRequestBuilder()
232
    {
233 38
        if ($this->searchRequestBuilder === null) {
234 38
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
235
        }
236
237 38
        return $this->searchRequestBuilder;
238
    }
239
240
    /**
241
     * Called when the solr server is unavailable.
242
     *
243
     * @return void
244
     */
245 2
    protected function handleSolrUnavailable()
246
    {
247 2
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
248
            /** @var SolrLogManager $logger */
249 2
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
250 2
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
251
        }
252 2
    }
253
254
    /**
255
     * Emits signal for various actions
256
     *
257
     * @param string $className Name of the class containing the signal
258
     * @param string $signalName Name of the signal slot
259
     * @param array $signalArguments arguments for the signal slot
260
     *
261
     * @return array
262
     */
263 38
    protected function emitActionSignal($className, $signalName, array $signalArguments)
264
    {
265 38
        return $this->signalSlotDispatcher->dispatch($className, $signalName, $signalArguments)[0];
266
    }
267
}
268