getRootPagesWithIndexingOff()   B
last analyzed

Complexity

Conditions 9
Paths 28

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 30
ccs 18
cts 18
cp 1
rs 8.0555
cc 9
nc 28
nop 0
crap 9
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 TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
31
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Reports\Status;
34
35
/**
36
 * Provides an status report, which checks whether the configuration of the
37
 * extension is ok.
38
 *
39
 * @author Ingo Renner <[email protected]>
40
 */
41
class SolrConfigurationStatus extends AbstractSolrStatus
42
{
43
    /**
44
     * @var ExtensionConfiguration
45
     */
46
    protected $extensionConfiguration;
47
48
    /**
49
     * @var FrontendEnvironment
50
     */
51
    protected $fronendEnvironment = null;
52 6
53
    /**
54 6
     * SolrConfigurationStatus constructor.
55 6
     * @param ExtensionConfiguration|null $extensionConfiguration
56
     * @param FrontendEnvironment|null $frontendEnvironment
57
58
     */
59
    public function __construct(
60
        ExtensionConfiguration $extensionConfiguration = null,
61
        FrontendEnvironment $frontendEnvironment = null
62 6
    )
63
    {
64 6
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
65
        $this->fronendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
66 6
    }
67 6
68 1
    /**
69
     * Compiles a collection of configuration status checks.
70
     *
71 1
     * @return array
72
     */
73
    public function getStatus()
74 5
    {
75 5
        $reports = [];
76 1
77
        $rootPageFlagStatus = $this->getRootPageFlagStatus();
78
        if (!is_null($rootPageFlagStatus)) {
79 5
            $reports[] = $rootPageFlagStatus;
80 5
81 2
            // intended early return, no sense in going on if there are no root pages
82
            return $reports;
83
        }
84 5
85
        $configIndexEnableStatus = $this->getConfigIndexEnableStatus();
86
        if (!is_null($configIndexEnableStatus)) {
87
            $reports[] = $configIndexEnableStatus;
88
        }
89
90
        return $reports;
91
    }
92
93 6
    /**
94
     * Checks whether the "Use as Root Page" page property has been set for any
95 6
     * site.
96 6
     *
97 5
     * @return NULL|Status An error status is returned if no root pages were found.
98
     */
99
    protected function getRootPageFlagStatus()
100 1
    {
101 1
        $rootPages = $this->getRootPages();
102
        if (!empty($rootPages)) {
103
            return null;
104
        }
105
106
        $report = $this->getRenderedReport('RootPageFlagStatus.html');
107
        return GeneralUtility::makeInstance(
108
            Status::class,
109 5
            /** @scrutinizer ignore-type */ 'Sites',
110
            /** @scrutinizer ignore-type */ 'No sites found',
111 5
            /** @scrutinizer ignore-type */ $report,
112 5
            /** @scrutinizer ignore-type */ Status::ERROR
113 4
        );
114
    }
115
116 1
    /**
117 1
     * Checks whether config.index_enable is set to 1, otherwise indexing will
118
     * not work.
119
     *
120
     * @return NULL|Status An error status is returned for each site root page config.index_enable = 0.
121
     */
122
    protected function getConfigIndexEnableStatus()
123
    {
124
        $rootPagesWithIndexingOff = $this->getRootPagesWithIndexingOff();
125
        if (empty($rootPagesWithIndexingOff)) {
126 5
            return null;
127
        }
128 5
129 5
        $report = $this->getRenderedReport('SolrConfigurationStatusIndexing.html', ['pages' => $rootPagesWithIndexingOff]);
130 3
        return GeneralUtility::makeInstance(
131
            Status::class,
132
            /** @scrutinizer ignore-type */ 'Page Indexing',
133 2
            /** @scrutinizer ignore-type */ 'Indexing is disabled',
134 2
            /** @scrutinizer ignore-type */ $report,
135
            /** @scrutinizer ignore-type */ Status::WARNING
136
        );
137
    }
138
139
    /**
140
     * Returns an array of rootPages where the indexing is off and EXT:solr is enabled.
141
     *
142 5
     * @return array
143
     */
144 5
    protected function getRootPagesWithIndexingOff()
145 5
    {
146
        $rootPages = $this->getRootPages();
147 5
        $rootPagesWithIndexingOff = [];
148 5
149 5
        foreach ($rootPages as $rootPage) {
150
            try {
151
                $this->initializeTSFE($rootPage);
152 5
                $solrIsEnabledAndIndexingDisabled = $this->getIsSolrEnabled() && !$this->getIsIndexingEnabled();
153 5
                if ($solrIsEnabledAndIndexingDisabled) {
154 5
                    $rootPagesWithIndexingOff[] = $rootPage;
155 5
                }
156
            } catch (\RuntimeException $rte) {
157 5
                $rootPagesWithIndexingOff[] = $rootPage;
158 1
            } catch (ServiceUnavailableException $sue) {
159
                if ($sue->getCode() == 1294587218) {
160
                    //  No TypoScript template found, continue with next site
161
                    $rootPagesWithIndexingOff[] = $rootPage;
162 5
                    continue;
163 5
                }
164 5
            } catch (SiteNotFoundException $sue) {
165 5
                if ($sue->getCode() == 1521716622) {
166
                    //  No site found, continue with next site
167
                    $rootPagesWithIndexingOff[] = $rootPage;
168 5
                    continue;
169 5
                }
170
            }
171
        }
172 5
173
        return $rootPagesWithIndexingOff;
174
    }
175
176
    /**
177
     * Gets the site's root pages. The "Is root of website" flag must be set,
178
     * which usually is the case for pages with pid = 0.
179
     *
180 5
     * @return array An array of (partial) root page records, containing the uid and title fields
181
     */
182 5
    protected function getRootPages()
183 5
    {
184
        $pagesRepository = GeneralUtility::makeInstance(PagesRepository::class);
185 5
186
        return $pagesRepository->findAllRootPages();
187 5
    }
188 5
189 5
    /**
190 5
     * Checks if the solr plugin is enabled with plugin.tx_solr.enabled.
191
     *
192
     * @return bool
193
     */
194
    protected function getIsSolrEnabled()
195
    {
196
        if (empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['enabled'])) {
197
            return false;
198 5
        }
199
        return (bool)$GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['enabled'];
200
    }
201
202
    /**
203 5
     * Checks if the indexing is enabled with config.index_enable
204
     *
205
     * @return bool
206
     */
207
    protected function getIsIndexingEnabled()
208
    {
209
        if (empty($GLOBALS['TSFE']->config['config']['index_enable'])) {
210
            return false;
211
        }
212 4
213
        return (bool)$GLOBALS['TSFE']->config['config']['index_enable'];
214 4
    }
215
216 4
    /**
217
     * @param $rootPage
218
     */
219
    protected function initializeTSFE($rootPage)
220
    {
221
        $this->fronendEnvironment->initializeTsfe($rootPage['uid']);
222
    }
223
}
224