Failed Conditions
Push — task/3376-TYPO3_12_compatibili... ( 631fb4...fe02d9 )
by Markus
66:27 queued 21:25
created

AbstractBaseController::buildControllerContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\NoSolrConnectionFoundException;
22
use ApacheSolrForTypo3\Solr\Search;
23
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager as SolrConfigurationManager;
24
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
25
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
26
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
27
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
28
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\GeneralUtility 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...
30
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
31
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
32
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
33
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
34
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
35
36
/**
37
 * Class AbstractBaseController
38
 *
39
 * @author Frans Saris <[email protected]>
40
 * @author Timo Hund <[email protected]>
41
 */
42
abstract class AbstractBaseController extends ActionController
43
{
44
    /**
45
     * The HTTP code 503 message.
46
     * @var string
47
     */
48
    protected const STATUS_503_MESSAGE = 'Apache Solr Server is not available.';
49
50
    /**
51
     * @var ContentObjectRenderer|null
52
     */
53
    private ?ContentObjectRenderer $contentObjectRenderer = null;
54
55
    /**
56
     * @var TypoScriptFrontendController|null
57
     */
58
    protected ?TypoScriptFrontendController $typoScriptFrontendController = null;
59
60
    /**
61
     * @var SolrConfigurationManager|null
62
     */
63
    private ?SolrConfigurationManager $solrConfigurationManager = null;
64
65
    /**
66
     * The configuration is private if you need it please get it from the controllerContext.
67
     *
68
     * @var TypoScriptConfiguration|null
69
     */
70
    protected ?TypoScriptConfiguration $typoScriptConfiguration = null;
71
72
    /**
73
     * @var SearchResultSetService|null
74
     */
75
    protected ?SearchResultSetService $searchService = null;
76
77
    /**
78
     * @var SearchRequestBuilder|null
79
     */
80
    protected ?SearchRequestBuilder $searchRequestBuilder = null;
81
82
    /**
83
     * @var bool
84
     */
85
    protected bool $resetConfigurationBeforeInitialize = true;
86
87
    /**
88
     * @param ConfigurationManagerInterface $configurationManager
89
     */
90
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
91
    {
92
        $this->configurationManager = $configurationManager;
93
        // @extensionScannerIgnoreLine
94
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
95
        $this->arguments = GeneralUtility::makeInstance(Arguments::class);
96
    }
97
98
    /**
99
     * @param ContentObjectRenderer $contentObjectRenderer
100
     */
101
    public function setContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer)
102
    {
103
        $this->contentObjectRenderer = $contentObjectRenderer;
104
    }
105
106
    /**
107
     * @return ContentObjectRenderer|null
108
     */
109
    public function getContentObjectRenderer(): ?ContentObjectRenderer
110
    {
111
        return $this->contentObjectRenderer;
112
    }
113
114
    /**
115
     * @param SolrConfigurationManager $configurationManager
116
     */
117
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
118
    {
119
        $this->solrConfigurationManager = $configurationManager;
120
    }
121
122
    /**
123
     * @param bool $resetConfigurationBeforeInitialize
124
     */
125
    public function setResetConfigurationBeforeInitialize(bool $resetConfigurationBeforeInitialize)
126
    {
127
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
128
    }
129
130
    /**
131
     * Initialize action
132
     * @throws AspectNotFoundException
133
     */
134
    protected function initializeAction()
135
    {
136
        // Reset configuration (to reset flexform overrides) if resetting is enabled
137
        if ($this->resetConfigurationBeforeInitialize) {
138
            $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

138
            $this->solrConfigurationManager->/** @scrutinizer ignore-call */ 
139
                                             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...
139
        }
140
        /** @var TypoScriptService $typoScriptService */
141
        $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
142
143
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
144
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
145
        $pluginSettings = [];
146
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
147
            if (isset($frameWorkConfiguration[$key])) {
148
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
149
            }
150
        }
151
152
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
153
        if ($pluginSettings !== []) {
154
            $this->typoScriptConfiguration->mergeSolrConfiguration(
155
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
156
                true,
157
                false
158
            );
159
        }
160
161
        if (!empty($this->contentObjectRenderer->data['pi_flexform'])) {
162
            GeneralUtility::makeInstance(ConfigurationService::class)
163
                ->overrideConfigurationWithFlexFormSettings(
164
                    $this->contentObjectRenderer->data['pi_flexform'],
165
                    $this->typoScriptConfiguration
166
                );
167
        }
168
169
        parent::initializeAction();
170
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
171
        $this->initializeSettings();
172
173
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
174
            $this->initializeSearch();
175
        }
176
    }
177
178
    /**
179
     * Inject settings of plugin.tx_solr
180
     */
181
    protected function initializeSettings()
182
    {
183
        $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
184
185
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
186
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
187
            $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

187
            $this->typoScriptConfiguration->/** @scrutinizer ignore-call */ 
188
                                            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...
188
        );
189
    }
190
191
    /**
192
     * Initialize the Solr connection and
193
     * test the connection through a ping
194
     * @throws AspectNotFoundException
195
     */
196
    protected function initializeSearch()
197
    {
198
        try {
199
            $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByTypo3Site(
200
                $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

200
                $this->typoScriptFrontendController->/** @scrutinizer ignore-call */ 
201
                                                     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...
201
                $this->typoScriptFrontendController->getLanguage()->getLanguageId()
202
            );
203
204
            $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
205
206
            /** @noinspection PhpParamsInspection */
207
            $this->searchService = GeneralUtility::makeInstance(
208
                SearchResultSetService::class,
209
                /** @scrutinizer ignore-type */
210
                $this->typoScriptConfiguration,
211
                /** @scrutinizer ignore-type */
212
                $search
213
            );
214
        } catch (NoSolrConnectionFoundException $e) {
215
            $this->logSolrUnavailable();
216
        }
217
    }
218
219
    /**
220
     * @return SearchRequestBuilder
221
     */
222
    protected function getSearchRequestBuilder(): SearchRequestBuilder
223
    {
224
        if ($this->searchRequestBuilder === null) {
225
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
226
        }
227
228
        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...
229
    }
230
231
    /**
232
     * Called when the solr server is unavailable.
233
     */
234
    protected function logSolrUnavailable()
235
    {
236
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
237
            /** @var SolrLogManager $logger */
238
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
239
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
240
        }
241
    }
242
}
243