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