Passed
Push — master ( 2beb9d...5bc38e )
by Timo
05:35
created

AbstractBaseController::setContentObjectRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

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