Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( ecf396...f1093c )
by Rafael
23:38
created

AbstractBaseController::logSolrUnavailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
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\Extbase\SignalSlot\Exception\InvalidSlotException;
33
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
34
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
35
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
36
37
/**
38
 * Class AbstractBaseController
39
 *
40
 * @author Frans Saris <[email protected]>
41
 * @author Timo Hund <[email protected]>
42
 */
43
abstract class AbstractBaseController extends ActionController
44
{
45
    protected const STATUS_503_MESSAGE = 'Apache Solr Server is not available.';
46
47
    /**
48
     * @var ContentObjectRenderer
49
     */
50
    private $contentObjectRenderer;
51
52
    /**
53
     * @var TypoScriptFrontendController
54
     */
55
    protected $typoScriptFrontendController;
56
57
    /**
58
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
59
     */
60
    protected $configurationManager;
61
62
    /**
63
     * @var SolrConfigurationManager
64
     */
65
    private $solrConfigurationManager;
66
67
    /**
68
     * The configuration is private if you need it please get it from the controllerContext.
69
     *
70
     * @var TypoScriptConfiguration
71
     */
72
    protected $typoScriptConfiguration;
73
74
    /**
75
     * @var \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
76
     */
77
    protected $controllerContext;
78
79
    /**
80
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService
81
     */
82
    protected $searchService;
83
84
    /**
85
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder
86
     */
87
    protected $searchRequestBuilder;
88
89
    /**
90
     * @var bool
91
     */
92
    protected $resetConfigurationBeforeInitialize = true;
93
94
    /**
95
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
96
     * @return void
97
     */
98
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
99
    {
100
        $this->configurationManager = $configurationManager;
101
        // @extensionScannerIgnoreLine
102
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
103
    }
104
105
    /**
106
     * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer
107
     */
108
    public function setContentObjectRenderer($contentObjectRenderer)
109
    {
110
        $this->contentObjectRenderer = $contentObjectRenderer;
111
    }
112
113
    /**
114
     * @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
115
     */
116
    public function getContentObjectRenderer()
117
    {
118
        return $this->contentObjectRenderer;
119
    }
120
121
    /**
122
     * @param SolrConfigurationManager $configurationManager
123
     */
124
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
125
    {
126
        $this->solrConfigurationManager = $configurationManager;
127
    }
128
129
    /**
130
     * @param boolean $resetConfigurationBeforeInitialize
131
     */
132
    public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
133
    {
134
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
135
    }
136
137
    /**
138
     * Initialize the controller context
139
     *
140
     * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext ControllerContext to be passed to the view
141
     * @api
142
     */
143
    protected function buildControllerContext()
144
    {
145
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
146
        $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

146
        $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...
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

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

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...
147
        $controllerContext->setRequest($this->request);
148
//        $controllerContext->setResponse($this->response);
149
        if ($this->arguments !== null) {
150
            $controllerContext->setArguments($this->arguments);
151
        }
152
        $controllerContext->setUriBuilder($this->uriBuilder);
153
154
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
155
156
        return $controllerContext;
157
    }
158
159
    /**
160
     * Initialize action
161
     */
162
    protected function initializeAction()
163
    {
164
        // Reset configuration (to reset flexform overrides) if resetting is enabled
165
        if ($this->resetConfigurationBeforeInitialize) {
166
            $this->solrConfigurationManager->reset();
167
        }
168
        /** @var TypoScriptService $typoScriptService */
169
        $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

169
        $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...
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

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

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...
170
171
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
172
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
173
        $pluginSettings = [];
174
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
175
            if (isset($frameWorkConfiguration[$key])) {
176
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
177
            }
178
        }
179
180
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
181
        if ($pluginSettings !== []) {
182
            $this->typoScriptConfiguration->mergeSolrConfiguration(
183
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
184
                true,
185
                false
186
            );
187
        }
188
189
        $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

189
        /** @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...
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

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

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...
190
            ->overrideConfigurationWithFlexFormSettings(
191
                $this->contentObjectRenderer->data['pi_flexform'],
192
                $this->typoScriptConfiguration
193
            );
194
195
        parent::initializeAction();
196
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
197
        $this->initializeSettings();
198
199
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
200
            $this->initializeSearch();
201
        }
202
    }
203
204
    /**
205
     * Inject settings of plugin.tx_solr
206
     *
207
     * @return void
208
     */
209
    protected function initializeSettings()
210
    {
211
        /** @var $typoScriptService TypoScriptService */
212
        $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

212
        $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...
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

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

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...
213
214
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
215
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
216
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
217
        );
218
    }
219
220
    /**
221
     * Initialize the Solr connection and
222
     * test the connection through a ping
223
     */
224
    protected function initializeSearch()
225
    {
226
        /** @var ConnectionManager $solrConnection */
227
        try {
228
            $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

228
            $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...
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

228
            $solrConnection = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $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...
229
            $search = $this->objectManager->get(Search::class, $solrConnection);
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

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

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...
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

229
            $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...
230
231
            $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

231
            $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...
Deprecated Code introduced by
The property TYPO3\CMS\Extbase\Mvc\Co...troller::$objectManager has been deprecated: since v11, will be removed in v12 ( Ignorable by Annotation )

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

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

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...
232
                SearchResultSetService::class,
233
                /** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
234
                /** @scrutinizer ignore-type */ $search
235
            );
236
        } catch (NoSolrConnectionFoundException $e) {
237
            $this->logSolrUnavailable();
238
        }
239
    }
240
241
    /**
242
     * @return SearchRequestBuilder
243
     */
244
    protected function getSearchRequestBuilder(): SearchRequestBuilder
245
    {
246
        if ($this->searchRequestBuilder === null) {
247
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
248
        }
249
250
        return $this->searchRequestBuilder;
251
    }
252
253
    /**
254
     * Called when the solr server is unavailable.
255
     *
256
     * @return void
257
     */
258
    protected function logSolrUnavailable()
259
    {
260
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
261
            /** @var SolrLogManager $logger */
262
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
263
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
264
        }
265
    }
266
267
    /**
268
     * Emits signal for various actions
269
     *
270
     * @param string $className Name of the class containing the signal
271
     * @param string $signalName Name of the signal slot
272
     * @param array $signalArguments arguments for the signal slot
273
     *
274
     * @return array|mixed
275
     * @throws InvalidSlotException
276
     * @throws InvalidSlotReturnException
277
     */
278
    protected function emitActionSignal(string $className, string $signalName, array $signalArguments)
279
    {
280
        return $this->signalSlotDispatcher->dispatch($className, $signalName, $signalArguments)[0];
281
    }
282
}
283