Passed
Pull Request — release-11.5.x (#3165)
by Rafael
23:22
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
3
namespace ApacheSolrForTypo3\Solr\Controller;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\ConnectionManager;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
20
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder;
21
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
22
use ApacheSolrForTypo3\Solr\Search;
23
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
24
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
25
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
26
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
27
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager as SolrConfigurationManager;
28
use ApacheSolrForTypo3\Solr\Util;
29
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
30
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
33
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
34
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
35
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
36
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
37
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
38
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
39
40
/**
41
 * Class AbstractBaseController
42
 *
43
 * @property SolrControllerContext $controllerContext
44
 *
45
 * @author Frans Saris <[email protected]>
46
 * @author Timo Hund <[email protected]>
47
 */
48
abstract class AbstractBaseController extends ActionController
49
{
50
    /**
51
     * The HTTP code 503 message.
52
     * @var string
53
     */
54
    protected const STATUS_503_MESSAGE = 'Apache Solr Server is not available.';
55
56
    /**
57
     * @var ContentObjectRenderer|null
58
     */
59
    private ?ContentObjectRenderer $contentObjectRenderer = null;
60
61
    /**
62
     * @var TypoScriptFrontendController|null
63
     */
64
    protected ?TypoScriptFrontendController $typoScriptFrontendController = null;
65
66
    /**
67
     * @var SolrConfigurationManager|null
68
     */
69
    private ?SolrConfigurationManager $solrConfigurationManager = null;
70
71
    /**
72
     * The configuration is private if you need it please get it from the controllerContext.
73
     *
74
     * @var TypoScriptConfiguration|null
75
     */
76
    protected ?TypoScriptConfiguration $typoScriptConfiguration = null;
77
78
    /**
79
     * @var SearchResultSetService|null
80
     */
81
    protected ?SearchResultSetService $searchService = null;
82
83
    /**
84
     * @var SearchRequestBuilder|null
85
     */
86
    protected ?SearchRequestBuilder $searchRequestBuilder = null;
87
88
    /**
89
     * @var bool
90
     */
91
    protected bool $resetConfigurationBeforeInitialize = true;
92
93
    /**
94
     * @param ConfigurationManagerInterface $configurationManager
95
     * @return void
96
     */
97
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
98
    {
99
        $this->configurationManager = $configurationManager;
100
        // @extensionScannerIgnoreLine
101
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
102
    }
103
104
    /**
105
     * @param ContentObjectRenderer $contentObjectRenderer
106
     */
107
    public function setContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer)
108
    {
109
        $this->contentObjectRenderer = $contentObjectRenderer;
110
    }
111
112
    /**
113
     * @return ContentObjectRenderer|null
114
     */
115
    public function getContentObjectRenderer(): ?ContentObjectRenderer
116
    {
117
        return $this->contentObjectRenderer;
118
    }
119
120
    /**
121
     * @param SolrConfigurationManager $configurationManager
122
     */
123
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
124
    {
125
        $this->solrConfigurationManager = $configurationManager;
126
    }
127
128
    /**
129
     * @param bool $resetConfigurationBeforeInitialize
130
     */
131
    public function setResetConfigurationBeforeInitialize(bool $resetConfigurationBeforeInitialize)
132
    {
133
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
134
    }
135
136
    /**
137
     * Initialize the controller context
138
     *
139
     * @return ControllerContext ControllerContext to be passed to the view
140
     * @api
141
     */
142
    protected function buildControllerContext()
143
    {
144
        /** @var $controllerContext SolrControllerContext */
145
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
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

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

145
        $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...
146
        $controllerContext->setRequest($this->request);
147
//        $controllerContext->setResponse($this->response);
148
        if ($this->arguments !== null) {
149
            $controllerContext->setArguments($this->arguments);
150
        }
151
        $controllerContext->setUriBuilder($this->uriBuilder);
152
153
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $this->typoScriptConfiguration can also be of type null; however, parameter $typoScriptConfiguration of ApacheSolrForTypo3\Solr\...poScriptConfiguration() does only seem to accept ApacheSolrForTypo3\Solr\...TypoScriptConfiguration, maybe add an additional type check? ( Ignorable by Annotation )

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

153
        $controllerContext->setTypoScriptConfiguration(/** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
Loading history...
154
155
        return $controllerContext;
156
    }
157
158
    /**
159
     * Initialize action
160
     * @throws AspectNotFoundException
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();
0 ignored issues
show
Bug introduced by
The method reset() does not exist on null. ( Ignorable by Annotation )

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

166
            $this->solrConfigurationManager->/** @scrutinizer ignore-call */ 
167
                                             reset();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
        if (!empty($this->contentObjectRenderer->data['pi_flexform'])) {
190
            $this->objectManager->get(ConfigurationService::class)
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

190
            /** @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...
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

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

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

214
        $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...
215
216
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
217
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
218
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
0 ignored issues
show
Bug introduced by
The method getObjectByPathOrDefault() does not exist on null. ( Ignorable by Annotation )

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

218
            $this->typoScriptConfiguration->/** @scrutinizer ignore-call */ 
219
                                            getObjectByPathOrDefault('plugin.tx_solr.settings.', [])

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
219
        );
220
    }
221
222
    /**
223
     * Initialize the Solr connection and
224
     * test the connection through a ping
225
     * @throws AspectNotFoundException
226
     */
227
    protected function initializeSearch()
228
    {
229
        /** @var ConnectionManager $solrConnection */
230
        try {
231
            $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

231
            $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

231
            $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...
232
            $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

232
            $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

232
            $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...
233
234
            /** @noinspection PhpParamsInspection */
235
            $this->searchService = $this->objectManager->get(
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

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

235
            $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...
236
                SearchResultSetService::class,
237
                /** @scrutinizer ignore-type */
238
                $this->typoScriptConfiguration,
239
                /** @scrutinizer ignore-type */
240
                $search
241
            );
242
        } catch (NoSolrConnectionFoundException $e) {
243
            $this->logSolrUnavailable();
244
        }
245
    }
246
247
    /**
248
     * @return SearchRequestBuilder
249
     */
250
    protected function getSearchRequestBuilder(): SearchRequestBuilder
251
    {
252
        if ($this->searchRequestBuilder === null) {
253
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
254
        }
255
256
        return $this->searchRequestBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->searchRequestBuilder could return the type null which is incompatible with the type-hinted return ApacheSolrForTypo3\Solr\...ch\SearchRequestBuilder. Consider adding an additional type-check to rule them out.
Loading history...
257
    }
258
259
    /**
260
     * Called when the solr server is unavailable.
261
     *
262
     * @return void
263
     */
264
    protected function logSolrUnavailable()
265
    {
266
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
267
            /** @var SolrLogManager $logger */
268
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
269
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
270
        }
271
    }
272
273
    /**
274
     * Emits signal for various actions
275
     *
276
     * @param string $className Name of the class containing the signal
277
     * @param string $signalName Name of the signal slot
278
     * @param array $signalArguments arguments for the signal slot
279
     *
280
     * @return array|mixed
281
     * @throws InvalidSlotException
282
     * @throws InvalidSlotReturnException
283
     */
284
    protected function emitActionSignal(string $className, string $signalName, array $signalArguments)
285
    {
286
        return $this->signalSlotDispatcher->dispatch($className, $signalName, $signalArguments)[0];
287
    }
288
}
289