Passed
Push — master ( c7951d...b759e5 )
by Timo
12:28
created

AbstractBaseController::initializeAction()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6.3188

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 39
ccs 23
cts 29
cp 0.7931
rs 8.9297
c 0
b 0
f 0
cc 6
nc 24
nop 0
crap 6.3188
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 43
     * @return void
94
     */
95 43
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
96 43
    {
97 43
        $this->configurationManager = $configurationManager;
98
        // @extensionScannerIgnoreLine
99
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
100
    }
101
102 1
    /**
103
     * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer
104 1
     */
105 1
    public function setContentObjectRenderer($contentObjectRenderer)
106
    {
107
        $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 43
    /**
119
     * @param SolrConfigurationManager $configurationManager
120 43
     */
121 43
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
122
    {
123
        $this->solrConfigurationManager = $configurationManager;
124
    }
125
126 22
    /**
127
     * @param boolean $resetConfigurationBeforeInitialize
128 22
     */
129 22
    public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
130
    {
131
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
132
    }
133
134
    /**
135
     * Initialize the controller context
136
     *
137 43
     * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext ControllerContext to be passed to the view
138
     * @api
139
     */
140 43
    protected function buildControllerContext()
141 43
    {
142 43
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
143 43
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
144 43
        $controllerContext->setRequest($this->request);
145
        $controllerContext->setResponse($this->response);
146 43
        if ($this->arguments !== null) {
147
            $controllerContext->setArguments($this->arguments);
148 43
        }
149
        $controllerContext->setUriBuilder($this->uriBuilder);
150 43
151
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
152
153
        return $controllerContext;
154
    }
155
156 43
    /**
157
     * Initialize action
158
     */
159 43
    protected function initializeAction()
160 21
    {
161
        // Reset configuration (to reset flexform overrides) if resetting is enabled
162
        if ($this->resetConfigurationBeforeInitialize) {
163 43
            $this->solrConfigurationManager->reset();
164
        }
165
        /** @var TypoScriptService $typoScriptService */
166 43
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
167 43
168 43
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
169 43
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
170 43
        $pluginSettings = [];
171
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
172
            if (isset($frameWorkConfiguration[$key])) {
173
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
174 43
            }
175 43
        }
176 2
177 2
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
178 2
        if ($pluginSettings !== []) {
179 2
            $this->typoScriptConfiguration->mergeSolrConfiguration(
180
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
181
                true,
182
                false
183 43
            );
184 43
        }
185 43
186 43
        $this->objectManager->get(ConfigurationService::class)
187
            ->overrideConfigurationWithFlexFormSettings(
188
                $this->contentObjectRenderer->data['pi_flexform'],
189 43
                $this->typoScriptConfiguration
190 43
            );
191 43
192 43
        parent::initializeAction();
193 43
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
194
        $this->initializeSettings();
195
196
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
197
            $this->initializeSearch();
198
        }
199
    }
200 43
201
    /**
202
     * Inject settings of plugin.tx_solr
203 43
     *
204
     * @return void
205
     */
206 43
    protected function initializeSettings()
207 43
    {
208
        /** @var $typoScriptService TypoScriptService */
209 43
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
210
211
        // 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 43
    }
216
217
    /**
218 43
     * Initialize the Solr connection and
219 43
     * test the connection through a ping
220
     */
221 43
    protected function initializeSearch()
222 43
    {
223 43
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
224 43
        try {
225
            $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
226 43
            $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
227
228
            $this->searchService = GeneralUtility::makeInstance(
229
                SearchResultSetService::class,
230
                /** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
231 38
                /** @scrutinizer ignore-type */ $search
232
            );
233 38
        } catch (NoSolrConnectionFoundException $e) {
234 38
            $this->handleSolrUnavailable();
235
        }
236
    }
237 38
238
    /**
239
     * @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 2
        }
246
247 2
        return $this->searchRequestBuilder;
248
    }
249 2
250 2
    /**
251
     * Called when the solr server is unavailable.
252 2
     *
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 38
264
    /**
265 38
     * Emits signal for various actions
266
     *
267
     * @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