Passed
Push — task/3376-TYPO3_12_compatibili... ( b42ab1...4e0f1e )
by Rafael
49:28 queued 04:28
created

AbstractBaseController::emitActionSignal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Controller;
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\Mvc\Controller\SolrControllerContext;
22
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
23
use ApacheSolrForTypo3\Solr\Search;
24
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager as SolrConfigurationManager;
25
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
26
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
27
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
28
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
29
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
32
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
33
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
35
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
36
37
/**
38
 * Class AbstractBaseController
39
 *
40
 * @property SolrControllerContext $controllerContext
41
 *
42
 * @author Frans Saris <[email protected]>
43
 * @author Timo Hund <[email protected]>
44
 */
45
abstract class AbstractBaseController extends ActionController
46
{
47
    /**
48
     * The HTTP code 503 message.
49
     * @var string
50
     */
51
    protected const STATUS_503_MESSAGE = 'Apache Solr Server is not available.';
52
53
    /**
54
     * @var ContentObjectRenderer|null
55
     */
56
    private ?ContentObjectRenderer $contentObjectRenderer = null;
57
58
    /**
59
     * @var TypoScriptFrontendController|null
60
     */
61
    protected ?TypoScriptFrontendController $typoScriptFrontendController = null;
62
63
    /**
64
     * @var SolrConfigurationManager|null
65
     */
66
    private ?SolrConfigurationManager $solrConfigurationManager = null;
67
68
    /**
69
     * The configuration is private if you need it please get it from the controllerContext.
70
     *
71
     * @var TypoScriptConfiguration|null
72
     */
73
    protected ?TypoScriptConfiguration $typoScriptConfiguration = null;
74
75
    /**
76
     * @var SearchResultSetService|null
77
     */
78
    protected ?SearchResultSetService $searchService = null;
79
80
    /**
81
     * @var SearchRequestBuilder|null
82
     */
83
    protected ?SearchRequestBuilder $searchRequestBuilder = null;
84
85
    /**
86
     * @var bool
87
     */
88
    protected bool $resetConfigurationBeforeInitialize = true;
89
90
    /**
91
     * @param ConfigurationManagerInterface $configurationManager
92
     */
93
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
94
    {
95
        $this->configurationManager = $configurationManager;
96
        // @extensionScannerIgnoreLine
97
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
98
    }
99
100
    /**
101
     * @param ContentObjectRenderer $contentObjectRenderer
102
     */
103
    public function setContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer)
104
    {
105
        $this->contentObjectRenderer = $contentObjectRenderer;
106
    }
107
108
    /**
109
     * @return ContentObjectRenderer|null
110
     */
111
    public function getContentObjectRenderer(): ?ContentObjectRenderer
112
    {
113
        return $this->contentObjectRenderer;
114
    }
115
116
    /**
117
     * @param SolrConfigurationManager $configurationManager
118
     */
119
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
120
    {
121
        $this->solrConfigurationManager = $configurationManager;
122
    }
123
124
    /**
125
     * @param bool $resetConfigurationBeforeInitialize
126
     */
127
    public function setResetConfigurationBeforeInitialize(bool $resetConfigurationBeforeInitialize)
128
    {
129
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
130
    }
131
132
    /**
133
     * Initialize the controller context
134
     *
135
     * @return ControllerContext ControllerContext to be passed to the view
136
     * @api
137
     */
138
    protected function buildControllerContext()
139
    {
140
        /** @var $controllerContext SolrControllerContext */
141
        $controllerContext = GeneralUtility::makeInstance(SolrControllerContext::class);
142
        $controllerContext->setRequest($this->request);
143
//        $controllerContext->setResponse($this->response);
144
        if ($this->arguments !== null) {
145
            $controllerContext->setArguments($this->arguments);
146
        }
147
        $controllerContext->setUriBuilder($this->uriBuilder);
148
149
        $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

149
        $controllerContext->setTypoScriptConfiguration(/** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
Loading history...
150
151
        return $controllerContext;
152
    }
153
154
    /**
155
     * Initialize action
156
     * @throws AspectNotFoundException
157
     */
158
    protected function initializeAction()
159
    {
160
        // Reset configuration (to reset flexform overrides) if resetting is enabled
161
        if ($this->resetConfigurationBeforeInitialize) {
162
            $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

162
            $this->solrConfigurationManager->/** @scrutinizer ignore-call */ 
163
                                             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...
163
        }
164
        /** @var TypoScriptService $typoScriptService */
165
        $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
166
167
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
168
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
169
        $pluginSettings = [];
170
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
171
            if (isset($frameWorkConfiguration[$key])) {
172
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
173
            }
174
        }
175
176
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
177
        if ($pluginSettings !== []) {
178
            $this->typoScriptConfiguration->mergeSolrConfiguration(
179
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
180
                true,
181
                false
182
            );
183
        }
184
185
        if (!empty($this->contentObjectRenderer->data['pi_flexform'])) {
186
            GeneralUtility::makeInstance(ConfigurationService::class)
187
                ->overrideConfigurationWithFlexFormSettings(
188
                    $this->contentObjectRenderer->data['pi_flexform'],
189
                    $this->typoScriptConfiguration
190
                );
191
        }
192
193
        parent::initializeAction();
194
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
195
        $this->initializeSettings();
196
197
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
198
            $this->initializeSearch();
199
        }
200
    }
201
202
    /**
203
     * Inject settings of plugin.tx_solr
204
     */
205
    protected function initializeSettings()
206
    {
207
        $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
208
209
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
210
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
211
            $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

211
            $this->typoScriptConfiguration->/** @scrutinizer ignore-call */ 
212
                                            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...
212
        );
213
    }
214
215
    /**
216
     * Initialize the Solr connection and
217
     * test the connection through a ping
218
     * @throws AspectNotFoundException
219
     */
220
    protected function initializeSearch()
221
    {
222
        try {
223
            $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByTypo3Site(
224
                $this->typoScriptFrontendController->getSite(),
0 ignored issues
show
Bug introduced by
The method getSite() 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

224
                $this->typoScriptFrontendController->/** @scrutinizer ignore-call */ 
225
                                                     getSite(),

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...
225
                $this->typoScriptFrontendController->getLanguage()->getLanguageId()
226
            );
227
228
            $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
229
230
            /** @noinspection PhpParamsInspection */
231
            $this->searchService = GeneralUtility::makeInstance(
232
                SearchResultSetService::class,
233
                /** @scrutinizer ignore-type */
234
                $this->typoScriptConfiguration,
235
                /** @scrutinizer ignore-type */
236
                $search
237
            );
238
        } catch (NoSolrConnectionFoundException $e) {
239
            $this->logSolrUnavailable();
240
        }
241
    }
242
243
    /**
244
     * @return SearchRequestBuilder
245
     */
246
    protected function getSearchRequestBuilder(): SearchRequestBuilder
247
    {
248
        if ($this->searchRequestBuilder === null) {
249
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
250
        }
251
252
        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...
253
    }
254
255
    /**
256
     * Called when the solr server is unavailable.
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