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

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

163
            $this->solrConfigurationManager->/** @scrutinizer ignore-call */ 
164
                                             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...
164
        }
165
        /** @var TypoScriptService $typoScriptService */
166
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
0 ignored issues
show
Bug Best Practice introduced by
The property objectManager does not exist on ApacheSolrForTypo3\Solr\...\AbstractBaseController. Did you maybe forget to declare it?
Loading history...
167
168
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
169
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
170
        $pluginSettings = [];
171
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
172
            if (isset($frameWorkConfiguration[$key])) {
173
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
174
            }
175
        }
176
177
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
178
        if ($pluginSettings !== []) {
179
            $this->typoScriptConfiguration->mergeSolrConfiguration(
180
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
181
                true,
182
                false
183
            );
184
        }
185
186
        if (!empty($this->contentObjectRenderer->data['pi_flexform'])) {
187
            $this->objectManager->get(ConfigurationService::class)
188
                ->overrideConfigurationWithFlexFormSettings(
189
                    $this->contentObjectRenderer->data['pi_flexform'],
190
                    $this->typoScriptConfiguration
191
                );
192
        }
193
194
        parent::initializeAction();
195
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
196
        $this->initializeSettings();
197
198
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
199
            $this->initializeSearch();
200
        }
201
    }
202
203
    /**
204
     * Inject settings of plugin.tx_solr
205
     */
206
    protected function initializeSettings()
207
    {
208
        /** @var $typoScriptService TypoScriptService */
209
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
0 ignored issues
show
Bug Best Practice introduced by
The property objectManager does not exist on ApacheSolrForTypo3\Solr\...\AbstractBaseController. Did you maybe forget to declare it?
Loading history...
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.', [])
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

213
            $this->typoScriptConfiguration->/** @scrutinizer ignore-call */ 
214
                                            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...
214
        );
215
    }
216
217
    /**
218
     * Initialize the Solr connection and
219
     * test the connection through a ping
220
     * @throws AspectNotFoundException
221
     */
222
    protected function initializeSearch()
223
    {
224
        /** @var ConnectionManager $solrConnection */
225
        try {
226
            $solrConnection = $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
0 ignored issues
show
Bug Best Practice introduced by
The property objectManager does not exist on ApacheSolrForTypo3\Solr\...\AbstractBaseController. Did you maybe forget to declare it?
Loading history...
227
            $search = $this->objectManager->get(Search::class, $solrConnection);
228
229
            /** @noinspection PhpParamsInspection */
230
            $this->searchService = $this->objectManager->get(
231
                SearchResultSetService::class,
232
                /** @scrutinizer ignore-type */
233
                $this->typoScriptConfiguration,
234
                /** @scrutinizer ignore-type */
235
                $search
236
            );
237
        } catch (NoSolrConnectionFoundException $e) {
238
            $this->logSolrUnavailable();
239
        }
240
    }
241
242
    /**
243
     * @return SearchRequestBuilder
244
     */
245
    protected function getSearchRequestBuilder(): SearchRequestBuilder
246
    {
247
        if ($this->searchRequestBuilder === null) {
248
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
249
        }
250
251
        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...
252
    }
253
254
    /**
255
     * Called when the solr server is unavailable.
256
     */
257
    protected function logSolrUnavailable()
258
    {
259
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
260
            /** @var SolrLogManager $logger */
261
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
262
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
263
        }
264
    }
265
}
266