Passed
Push — master ( 498335...52b5ef )
by Rafael
42:19
created

AbstractBaseController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Test Coverage

Coverage 68.27%

Importance

Changes 0
Metric Value
wmc 21
eloc 66
dl 0
loc 234
ccs 71
cts 104
cp 0.6827
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A injectConfigurationManager() 0 5 1
A buildControllerContext() 0 14 2
A getContentObjectRenderer() 0 3 1
A setContentObjectRenderer() 0 3 1
A setResetConfigurationBeforeInitialize() 0 3 1
A injectSolrConfigurationManager() 0 3 1
A emitActionSignal() 0 3 1
A handleSolrUnavailable() 0 6 2
A initializeSettings() 0 8 1
A getSearchRequestBuilder() 0 7 2
B initializeAction() 0 39 6
A initializeSearch() 0 14 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 44
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
95
    {
96 44
        $this->configurationManager = $configurationManager;
97
        // @extensionScannerIgnoreLine
98 44
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
99 44
    }
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 44
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
121
    {
122 44
        $this->solrConfigurationManager = $configurationManager;
123 44
    }
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 44
    protected function buildControllerContext()
140
    {
141
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
142 44
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

142
        $controllerContext = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(SolrControllerContext::class);

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

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

Loading history...
143 44
        $controllerContext->setRequest($this->request);
144 44
        $controllerContext->setResponse($this->response);
145 44
        if ($this->arguments !== null) {
146 44
            $controllerContext->setArguments($this->arguments);
147
        }
148 44
        $controllerContext->setUriBuilder($this->uriBuilder);
149
150 44
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
151
152 44
        return $controllerContext;
153
    }
154
155
    /**
156
     * Initialize action
157
     */
158 44
    protected function initializeAction()
159
    {
160
        // Reset configuration (to reset flexform overrides) if resetting is enabled
161 44
        if ($this->resetConfigurationBeforeInitialize) {
162 22
            $this->solrConfigurationManager->reset();
163
        }
164
        /** @var TypoScriptService $typoScriptService */
165 44
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

165
        $typoScriptService = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(TypoScriptService::class);

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

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

Loading history...
166
167
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
168 44
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
169 44
        $pluginSettings = [];
170 44
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
171 44
            if (isset($frameWorkConfiguration[$key])) {
172 2
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
173
            }
174
        }
175
176 44
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
177 44
        if ($pluginSettings !== []) {
178 2
            $this->typoScriptConfiguration->mergeSolrConfiguration(
179 2
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
180 2
                true,
181 2
                false
182
            );
183
        }
184
185 44
        $this->objectManager->get(ConfigurationService::class)
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

185
        /** @scrutinizer ignore-deprecated */ $this->objectManager->get(ConfigurationService::class)

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

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

Loading history...
186 44
            ->overrideConfigurationWithFlexFormSettings(
187 44
                $this->contentObjectRenderer->data['pi_flexform'],
188 44
                $this->typoScriptConfiguration
189
            );
190
191 44
        parent::initializeAction();
192 44
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
193 44
        $this->initializeSettings();
194
195 44
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
196 43
            $this->initializeSearch();
197
        }
198 44
    }
199
200
    /**
201
     * Inject settings of plugin.tx_solr
202
     *
203
     * @return void
204
     */
205 44
    protected function initializeSettings()
206
    {
207
        /** @var $typoScriptService TypoScriptService */
208 44
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

208
        $typoScriptService = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(TypoScriptService::class);

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

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

Loading history...
209
210
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
211 44
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
212 44
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
213
        );
214 44
    }
215
216
    /**
217
     * Initialize the Solr connection and
218
     * test the connection through a ping
219
     */
220 43
    protected function initializeSearch()
221
    {
222
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
223
        try {
224 43
            $solrConnection = $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

224
            $solrConnection = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);

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

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

Loading history...
225 43
            $search = $this->objectManager->get(Search::class, $solrConnection);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

225
            $search = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Search::class, $solrConnection);

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

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

Loading history...
226
227 43
            $this->searchService = $this->objectManager->get(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

227
            $this->searchService = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(

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

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

Loading history...
228 43
                SearchResultSetService::class,
229 43
                /** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
230 43
                /** @scrutinizer ignore-type */ $search
231
            );
232
        } catch (NoSolrConnectionFoundException $e) {
233
            $this->handleSolrUnavailable();
234
        }
235 43
    }
236
237
    /**
238
     * @return SearchRequestBuilder
239
     */
240 39
    protected function getSearchRequestBuilder()
241
    {
242 39
        if ($this->searchRequestBuilder === null) {
243 39
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
244
        }
245
246 39
        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