Passed
Pull Request — master (#1190)
by Timo
06:38
created

SolrStatus::checkPingTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.1922

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 11
cp 0.6364
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2.1922
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 2 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\SolrService;
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 2
    public function getStatus()
70
    {
71 2
        $reports = [];
72 2
        $this->connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
73
74 2
        $solrConnections = $this->connectionManager->getAllConfigurations();
75
76 2
        foreach ($solrConnections as $solrConnection) {
77 2
            $reports[] = $this->getConnectionStatus($solrConnection);
78
        }
79
80 2
        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 2
    protected function getConnectionStatus(array $solrConnection)
90
    {
91 2
        $header = 'Your site has contacted the Apache Solr server.';
92 2
        $this->responseStatus = Status::OK;
93
94 2
        $solr = $this->connectionManager->getConnection(
95 2
            $solrConnection['solrHost'],
96 2
            $solrConnection['solrPort'],
97 2
            $solrConnection['solrPath'],
98 2
            $solrConnection['solrScheme'],
99 2
            $solrConnection['solrUsername'],
100 2
            $solrConnection['solrPassword']
101
        );
102
103 2
        $solrVersion = $this->checkSolrVersion($solr);
104 2
        $accessFilter = $this->checkAccessFilter($solr);
105 2
        $pingTime = $this->checkPingTime($solr);
106 2
        $configName = $this->checkSolrConfigName($solr);
107 2
        $schemaName = $this->checkSolrSchemaName($solr);
108
109 2
        if ($this->responseStatus !== Status::OK) {
110 1
            $header = 'Failed contacting the Solr server.';
111
        }
112
113
        $variables = [
114 2
            'header' => $header,
115 2
            'connection' => $solrConnection,
116 2
            'solr' => $solr,
117 2
            'solrVersion' => $solrVersion,
118 2
            'pingTime' => $pingTime,
119 2
            'configName' => $configName,
120 2
            'schemaName' => $schemaName,
121 2
            'accessFilter' => $accessFilter
122
        ];
123
124 2
        $report = $this->getRenderedReport('SolrStatus.html', $variables);
125 2
        return GeneralUtility::makeInstance(Status::class, 'Apache Solr', '', $report, $this->responseStatus);
126
    }
127
128
    /**
129
     * Checks the solr version and adds it to the report.
130
     *
131
     * @param SolrService $solr
132
     * @return string solr version
133
     */
134 2
    protected function checkSolrVersion(SolrService $solr)
135
    {
136
        try {
137 2
            $solrVersion = $this->formatSolrVersion($solr->getSolrServerVersion());
138 1
        } catch (\Exception $e) {
139 1
            $this->responseStatus = Status::ERROR;
140 1
            $solrVersion = 'Error getting solr version: ' . $e->getMessage();
141
        }
142
143 2
        return $solrVersion;
144
    }
145
146
    /**
147
     * Checks the access filter setup and adds it to the report.
148
     *
149
     * @param SolrService $solr
150
     * @return string
151
     */
152 2
    protected function checkAccessFilter(SolrService $solr)
153
    {
154
        try {
155 2
            $accessFilterPluginStatus = GeneralUtility::makeInstance(AccessFilterPluginInstalledStatus::class);
156 2
            $accessFilterPluginVersion = $accessFilterPluginStatus->getInstalledPluginVersion($solr);
157 1
            $accessFilterMessage = $accessFilterPluginVersion;
158 1
        } catch (\Exception $e) {
159 1
            $this->responseStatus = Status::ERROR;
160 1
            $accessFilterMessage = 'Error getting access filter: ' . $e->getMessage();
161
        }
162 2
        return $accessFilterMessage;
163
    }
164
165
    /**
166
     * Checks the ping time and adds it to the report.
167
     *
168
     * @param SolrService $solr
169
     * @return string
170
     */
171 2
    protected function checkPingTime(SolrService $solr)
172
    {
173
        try {
174 2
            $pingQueryTime = $solr->getPingRoundTripRuntime();
175 1
            $pingMessage = (int)$pingQueryTime . ' ms';
176 1
        } catch (PingFailedException $e) {
177 1
            $this->responseStatus = Status::ERROR;
178 1
            $pingMessage = 'Ping error: ' . $e->getMessage();
179
        }
180 2
        return $pingMessage;
181
    }
182
183
    /**
184
     * Checks the solr config name and adds it to the report.
185
     *
186
     * @param SolrService $solr
187
     * @return string
188
     */
189 2
    protected function checkSolrConfigName(SolrService $solr)
190
    {
191
        try {
192 2
            $solrConfigMessage = $solr->getSolrconfigName();
193 1
        } catch (\Exception $e) {
194 1
            $this->responseStatus = Status::ERROR;
195 1
            $solrConfigMessage = 'Error determining solr config: ' . $e->getMessage();
196
        }
197
198 2
        return $solrConfigMessage;
199
    }
200
201
    /**
202
     * Checks the solr schema name and adds it to the report.
203
     *
204
     * @param SolrService $solr
205
     * @return string
206
     */
207 2
    protected function checkSolrSchemaName(SolrService $solr)
208
    {
209
        try {
210 2
            $solrSchemaMessage = $solr->getSchema()->getName();
211
        } catch (\Exception $e) {
212
            $this->responseStatus = Status::ERROR;
213
            $solrSchemaMessage = 'Error determining schema name: ' . $e->getMessage();
214
        }
215
216 2
        return $solrSchemaMessage;
217
    }
218
219
    /**
220
     * Formats the Apache Solr server version number. By default this is going
221
     * to be the simple major.minor.patch-level version. Custom Builds provide
222
     * more information though, in case of custom builds, their complete
223
     * version will be added, too.
224
     *
225
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
226
     * @return string formatted short version number, in case of custom builds followed by the complete version number
227
     */
228 1
    protected function formatSolrVersion($solrVersion)
229
    {
230 1
        $explodedSolrVersion = explode('.', $solrVersion);
231
232 1
        $shortSolrVersion = $explodedSolrVersion[0]
233 1
            . '.' . $explodedSolrVersion[1]
234 1
            . '.' . $explodedSolrVersion[2];
235
236 1
        $formattedSolrVersion = $shortSolrVersion;
237
238 1
        if ($solrVersion != $shortSolrVersion) {
239
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
240
        }
241
242 1
        return $formattedSolrVersion;
243
    }
244
}
245