Passed
Pull Request — release-11.2.x (#3528)
by Rafael
12:34
created

SolrConfigurationStatus::getIsSolrEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Report;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
28
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
29
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
30
use RuntimeException;
31
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
32
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
33
use TYPO3\CMS\Core\Http\ImmediateResponseException;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Reports\Status;
36
37
/**
38
 * Provides an status report, which checks whether the configuration of the
39
 * extension is ok.
40
 *
41
 * @author Ingo Renner <[email protected]>
42
 */
43
class SolrConfigurationStatus extends AbstractSolrStatus
44
{
45
    /**
46
     * @var ExtensionConfiguration
47
     */
48
    protected $extensionConfiguration;
49
50
    /**
51
     * @var FrontendEnvironment
52
     */
53
    protected $frontendEnvironment = null;
54
55
    /**
56
     * SolrConfigurationStatus constructor.
57
     * @param ExtensionConfiguration|null $extensionConfiguration
58
     * @param FrontendEnvironment|null $frontendEnvironment
59
60
     */
61 5
    public function __construct(
62
        ExtensionConfiguration $extensionConfiguration = null,
63
        FrontendEnvironment $frontendEnvironment = null
64
    )
65
    {
66 5
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
67 5
        $this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
68 5
    }
69
70
    /**
71
     * Compiles a collection of configuration status checks.
72
     *
73
     * @return array
74
     * @throws ImmediateResponseException
75
     */
76 5
    public function getStatus(): array
77
    {
78 5
        $reports = [];
79
80 5
        $rootPageFlagStatus = $this->getRootPageFlagStatus();
81 5
        if (!is_null($rootPageFlagStatus)) {
82 1
            $reports[] = $rootPageFlagStatus;
83
84
            // intended early return, no sense in going on if there are no root pages
85 1
            return $reports;
86
        }
87
88 4
        $configIndexEnableStatus = $this->getConfigIndexEnableStatus();
89 4
        if (!is_null($configIndexEnableStatus)) {
90 2
            $reports[] = $configIndexEnableStatus;
91
        }
92
93 4
        return $reports;
94
    }
95
96
    /**
97
     * Checks whether the "Use as Root Page" page property has been set for any
98
     * site.
99
     *
100
     * @return NULL|Status An error status is returned if no root pages were found.
101
     */
102 5
    protected function getRootPageFlagStatus(): ?Status
103
    {
104 5
        $rootPages = $this->getRootPages();
105 5
        if (!empty($rootPages)) {
106 4
            return null;
107
        }
108
109 1
        $report = $this->getRenderedReport('RootPageFlagStatus.html');
110 1
        return GeneralUtility::makeInstance(
111
            Status::class,
112
            /** @scrutinizer ignore-type */ 'Sites',
113
            /** @scrutinizer ignore-type */ 'No sites found',
114 1
            /** @scrutinizer ignore-type */ $report,
115 1
            /** @scrutinizer ignore-type */ Status::ERROR
116
        );
117
    }
118
119
    /**
120
     * Checks whether config.index_enable is set to 1, otherwise indexing will
121
     * not work.
122
     *
123
     * @return NULL|Status An error status is returned for each site root page config.index_enable = 0.
124
     * @throws ImmediateResponseException
125
     */
126 4
    protected function getConfigIndexEnableStatus(): ?Status
127
    {
128 4
        $rootPagesWithIndexingOff = $this->getRootPagesWithIndexingOff();
129 4
        if (empty($rootPagesWithIndexingOff)) {
130 2
            return null;
131
        }
132
133 2
        $report = $this->getRenderedReport('SolrConfigurationStatusIndexing.html', ['pages' => $rootPagesWithIndexingOff]);
134 2
        return GeneralUtility::makeInstance(
135
            Status::class,
136
            /** @scrutinizer ignore-type */ 'Page Indexing',
137
            /** @scrutinizer ignore-type */ 'Indexing is disabled',
138 2
            /** @scrutinizer ignore-type */ $report,
139 2
            /** @scrutinizer ignore-type */ Status::WARNING
140
        );
141
    }
142
143
    /**
144
     * Returns an array of rootPages where the indexing is off and EXT:solr is enabled.
145
     *
146
     * @return array
147
     * @throws ImmediateResponseException
148
     */
149 4
    protected function getRootPagesWithIndexingOff(): array
150
    {
151 4
        $rootPages = $this->getRootPages();
152 4
        $rootPagesWithIndexingOff = [];
153
154 4
        foreach ($rootPages as $rootPage) {
155
            try {
156 4
                $this->initializeTSFE($rootPage);
157 4
                $solrIsEnabledAndIndexingDisabled = $this->getIsSolrEnabled() && !$this->getIsIndexingEnabled();
158 4
                if ($solrIsEnabledAndIndexingDisabled) {
159 4
                    $rootPagesWithIndexingOff[] = $rootPage;
160
                }
161
            } catch (RuntimeException $rte) {
162
                $rootPagesWithIndexingOff[] = $rootPage;
163
            } catch (ServiceUnavailableException $sue) {
164
                if ($sue->getCode() == 1294587218) {
165
                    //  No TypoScript template found, continue with next site
166
                    $rootPagesWithIndexingOff[] = $rootPage;
167
                    continue;
168
                }
169
            } catch (SiteNotFoundException $sue) {
170
                if ($sue->getCode() == 1521716622) {
171
                    //  No site found, continue with next site
172
                    $rootPagesWithIndexingOff[] = $rootPage;
173
                    continue;
174
                }
175
            }
176
        }
177
178 4
        return $rootPagesWithIndexingOff;
179
    }
180
181
    /**
182
     * Gets the site's root pages. The "Is root of website" flag must be set,
183
     * which usually is the case for pages with pid = 0.
184
     *
185
     * @return array An array of (partial) root page records, containing the uid and title fields
186
     */
187 3
    protected function getRootPages()
188
    {
189 3
        $pagesRepository = GeneralUtility::makeInstance(PagesRepository::class);
190
191 3
        return $pagesRepository->findAllRootPages();
192
    }
193
194
    /**
195
     * Checks if the solr plugin is enabled with plugin.tx_solr.enabled.
196
     *
197
     * @return bool
198
     */
199 2
    protected function getIsSolrEnabled(): bool
200
    {
201 2
        if (empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['enabled'])) {
202
            return false;
203
        }
204 2
        return (bool)$GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['enabled'];
205
    }
206
207
    /**
208
     * Checks if the indexing is enabled with config.index_enable
209
     *
210
     * @return bool
211
     */
212 2
    protected function getIsIndexingEnabled(): bool
213
    {
214 2
        if (empty($GLOBALS['TSFE']->config['config']['index_enable'])) {
215 1
            return false;
216
        }
217
218 1
        return (bool)$GLOBALS['TSFE']->config['config']['index_enable'];
219
    }
220
221
    /**
222
     * Initializes TSFE via FrontendEnvironment.
223
     *
224
     * Purpose: Unit test mocking helper method.
225
     *
226
     * @param array $rootPageRecord
227
     * @throws ImmediateResponseException
228
     * @throws ServiceUnavailableException
229
     * @throws SiteNotFoundException
230
     */
231 2
    protected function initializeTSFE(array $rootPageRecord)
232
    {
233 2
        $this->frontendEnvironment->initializeTsfe($rootPageRecord['uid']);
234 2
    }
235
}
236