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

SolrStatus::checkSolrSchemaName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1852

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 3
cts 9
cp 0.3333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 3.1852
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Report;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-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\ConnectionManager;
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use ApacheSolrForTypo3\Solr\PingFailedException;
30
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrAdminService;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Reports\Status;
33
34
/**
35
 * Provides an status report about whether a connection to the Solr server can
36
 * be established.
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class SolrStatus extends AbstractSolrStatus
41
{
42
43
    /**
44
     * Site Repository
45
     *
46
     * @var SiteRepository
47
     */
48
    protected $siteRepository = null;
49
50
    /**
51
     * Connection Manager
52
     *
53
     * @var ConnectionManager
54
     */
55
    protected $connectionManager = null;
56
57
    /**
58
     * Holds the response status
59
     *
60
     * @var int
61
     */
62
    protected $responseStatus = Status::OK;
63
64
    /**
65
     * Holds the response message build by the checks
66
     *
67
     * @var string
68
     */
69
    protected $responseMessage = '';
70
71
72
    /**
73
     * SolrStatus constructor.
74
     * @param SiteRepository|null $siteRepository
75
     * @param ConnectionManager|null $connectionManager
76
     */
77 2
    public function __construct(SiteRepository $siteRepository = null, ConnectionManager $connectionManager = null)
78
    {
79 2
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
80 2
        $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
81 2
    }
82
83
    /**
84
     * Compiles a collection of status checks against each configured Solr server.
85
     *
86
     */
87 2
    public function getStatus()
88
    {
89 2
        $reports = [];
90 2
        foreach ($this->siteRepository->getAvailableSites() as $site) {
91 2
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
92 2
                $reports[] = $this->getConnectionStatus($solrConfiguration);
93
            }
94
        }
95
96 2
        return $reports;
97
    }
98
99
    /**
100
     * Checks whether a Solr server is available and provides some information.
101
     *
102
     * @param array $solrConnection Solr connection parameters
103
     * @return Status Status of the Solr connection
104
     */
105 2
    protected function getConnectionStatus(array $solrConnection)
106
    {
107 2
        $header = 'Your site has contacted the Apache Solr server.';
108 2
        $this->responseStatus = Status::OK;
109
110 2
        $solrAdmin = $this->connectionManager
111 2
            ->getSolrConnectionForNodes($solrConnection['read'], $solrConnection['write'])
112 2
            ->getAdminService();
113
114 2
        $solrVersion = $this->checkSolrVersion($solrAdmin);
115 2
        $accessFilter = $this->checkAccessFilter($solrAdmin);
116 2
        $pingTime = $this->checkPingTime($solrAdmin);
117 2
        $configName = $this->checkSolrConfigName($solrAdmin);
118 2
        $schemaName = $this->checkSolrSchemaName($solrAdmin);
119
120 2
        if ($this->responseStatus !== Status::OK) {
121 1
            $header = 'Failed contacting the Solr server.';
122
        }
123
124
        $variables = [
125 2
            'header' => $header,
126 2
            'connection' => $solrConnection,
127 2
            'solr' => $solrAdmin,
128 2
            'solrVersion' => $solrVersion,
129 2
            'pingTime' => $pingTime,
130 2
            'configName' => $configName,
131 2
            'schemaName' => $schemaName,
132 2
            'accessFilter' => $accessFilter
133
        ];
134
135 2
        $report = $this->getRenderedReport('SolrStatus.html', $variables);
136 2
        return GeneralUtility::makeInstance(
137
            Status::class,
138
            /** @scrutinizer ignore-type */ 'Apache Solr',
139
            /** @scrutinizer ignore-type */ '',
140 2
            /** @scrutinizer ignore-type */ $report,
141 2
            /** @scrutinizer ignore-type */ $this->responseStatus
142
        );
143
    }
144
145
    /**
146
     * Checks the solr version and adds it to the report.
147
     *
148
     * @param SolrAdminService $solr
149
     * @return string solr version
150
     */
151 2
    protected function checkSolrVersion(SolrAdminService $solr)
152
    {
153
        try {
154 2
            $solrVersion = $this->formatSolrVersion($solr->getSolrServerVersion());
155
        } catch (\Exception $e) {
156
            $this->responseStatus = Status::ERROR;
157
            $solrVersion = 'Error getting solr version: ' . $e->getMessage();
158
        }
159
160 2
        return $solrVersion;
161
    }
162
163
    /**
164
     * Checks the access filter setup and adds it to the report.
165
     *
166
     * @param SolrAdminService $solrAdminService
167
     * @return string
168
     */
169 2
    protected function checkAccessFilter(SolrAdminService $solrAdminService)
170
    {
171
        try {
172 2
            $accessFilterPluginStatus = GeneralUtility::makeInstance(AccessFilterPluginInstalledStatus::class);
173 2
            $accessFilterPluginVersion = $accessFilterPluginStatus->getInstalledPluginVersion($solrAdminService);
174 2
            $accessFilterMessage = $accessFilterPluginVersion;
175
        } catch (\Exception $e) {
176
            $this->responseStatus = Status::ERROR;
177
            $accessFilterMessage = 'Error getting access filter: ' . $e->getMessage();
178
        }
179 2
        return $accessFilterMessage;
180
    }
181
182
    /**
183
     * Checks the ping time and adds it to the report.
184
     *
185
     * @param SolrAdminService $solrAdminService
186
     * @return string
187
     */
188 2
    protected function checkPingTime(SolrAdminService $solrAdminService)
189
    {
190
        try {
191 2
            $pingQueryTime = $solrAdminService->getPingRoundTripRuntime();
192 1
            $pingMessage = (int)$pingQueryTime . ' ms';
193 1
        } catch (PingFailedException $e) {
194 1
            $this->responseStatus = Status::ERROR;
195 1
            $pingMessage = 'Ping error: ' . $e->getMessage();
196
        }
197 2
        return $pingMessage;
198
    }
199
200
    /**
201
     * Checks the solr config name and adds it to the report.
202
     *
203
     * @param SolrAdminService $solrAdminService
204
     * @return string
205
     */
206 2
    protected function checkSolrConfigName(SolrAdminService $solrAdminService)
207
    {
208
        try {
209 2
            $solrConfigMessage = $solrAdminService->getSolrconfigName();
210 1
        } catch (\Exception $e) {
211 1
            $this->responseStatus = Status::ERROR;
212 1
            $solrConfigMessage = 'Error determining solr config: ' . $e->getMessage();
213
        }
214
215 2
        return $solrConfigMessage;
216
    }
217
218
    /**
219
     * Checks the solr schema name and adds it to the report.
220
     *
221
     * @param SolrAdminService $solrAdminService
222
     * @return string
223
     */
224 2
    protected function checkSolrSchemaName(SolrAdminService $solrAdminService)
225
    {
226
        try {
227 2
            $solrSchemaMessage = $solrAdminService->getSchema()->getName();
228
        } catch (\Exception $e) {
229
            $this->responseStatus = Status::ERROR;
230
            $solrSchemaMessage = 'Error determining schema name: ' . $e->getMessage();
231
        }
232
233 2
        return $solrSchemaMessage;
234
    }
235
236
    /**
237
     * Formats the Apache Solr server version number. By default this is going
238
     * to be the simple major.minor.patch-level version. Custom Builds provide
239
     * more information though, in case of custom builds, their complete
240
     * version will be added, too.
241
     *
242
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
243
     * @return string formatted short version number, in case of custom builds followed by the complete version number
244
     */
245 2
    protected function formatSolrVersion($solrVersion)
246
    {
247 2
        $explodedSolrVersion = explode('.', $solrVersion);
248
249 2
        $shortSolrVersion = $explodedSolrVersion[0]
250 2
            . '.' . $explodedSolrVersion[1]
251 2
            . '.' . $explodedSolrVersion[2];
252
253 2
        $formattedSolrVersion = $shortSolrVersion;
254
255 2
        if ($solrVersion != $shortSolrVersion) {
256 1
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
257
        }
258
259 2
        return $formattedSolrVersion;
260
    }
261
}
262