Completed
Branch master (a6ebf8)
by Timo
03:27
created

SolrStatus::checkPingTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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