Passed
Pull Request — master (#1151)
by
unknown
19:19
created

SolrStatus::getStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 10
cp 0.7
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2.108
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 implements StatusProviderInterface
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 2
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
114 2
        $standaloneView->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Reports/SolrStatus.html'));
115 2
        $standaloneView->assignMultiple([
116 2
            'header' => $header,
117 2
            'connection' => $solrConnection,
118 2
            'solr' => $solr,
119 2
            'solrVersion' => $solrVersion,
120 2
            'pingTime' => $pingTime,
121 2
            'configName' => $configName,
122 2
            'schemaName' => $schemaName,
123 2
            'accessFilter' => $accessFilter
124
        ]);
125
126 2
        return GeneralUtility::makeInstance(Status::class,
127 2
            'Apache Solr',
128 2
            '',
129 2
            $standaloneView->render(),
130 2
            $this->responseStatus
131
        );
132
    }
133
134
    /**
135
     * Checks the solr version and adds it to the report.
136
     *
137
     * @param SolrService $solr
138
     * @return string solr version
139
     */
140 2
    protected function checkSolrVersion(SolrService $solr)
141
    {
142
        try {
143 2
            $solrVersion = $this->formatSolrVersion($solr->getSolrServerVersion());
144 1
        } catch (\Exception $e) {
145 1
            $this->responseStatus = Status::ERROR;
146 1
            $solrVersion = 'Error getting solr version: ' . $e->getMessage();
147
        }
148
149 2
        return $solrVersion;
150
    }
151
152
    /**
153
     * Checks the access filter setup and adds it to the report.
154
     *
155
     * @param SolrService $solr
156
     * @return string
157
     */
158 2
    protected function checkAccessFilter(SolrService $solr)
159
    {
160
        try {
161 2
            $accessFilterPluginStatus = GeneralUtility::makeInstance(AccessFilterPluginInstalledStatus::class);
162 2
            $accessFilterPluginVersion = $accessFilterPluginStatus->getInstalledPluginVersion($solr);
163 1
            $accessFilterMessage = $accessFilterPluginVersion;
164 1
        } catch (\Exception $e) {
165 1
            $this->responseStatus = Status::ERROR;
166 1
            $accessFilterMessage = 'Error getting access filter: ' . $e->getMessage();
167
        }
168 2
        return $accessFilterMessage;
169
    }
170
171
    /**
172
     * Checks the ping time and adds it to the report.
173
     *
174
     * @param SolrService $solr
175
     * @return string
176
     */
177 2
    protected function checkPingTime(SolrService $solr)
178
    {
179
        try {
180 2
            $pingQueryTime = $solr->getPingRoundTripRuntime();
181 1
            $pingMessage = (int)$pingQueryTime . ' ms';
182 1
        } catch (PingFailedException $e) {
183 1
            $this->responseStatus = Status::ERROR;
184 1
            $pingMessage = 'Ping error: ' . $e->getMessage();
185
        }
186 2
        return $pingMessage;
187
    }
188
189
    /**
190
     * Checks the solr config name and adds it to the report.
191
     *
192
     * @param SolrService $solr
193
     * @return string
194
     */
195 2
    protected function checkSolrConfigName(SolrService $solr)
196
    {
197
        try {
198 2
            $solrConfigMessage = $solr->getSolrconfigName();
199 1
        } catch (\Exception $e) {
200 1
            $this->responseStatus = Status::ERROR;
201 1
            $solrConfigMessage = 'Error determining solr config: ' . $e->getMessage();
202
        }
203
204 2
        return $solrConfigMessage;
205
    }
206
207
    /**
208
     * Checks the solr schema name and adds it to the report.
209
     *
210
     * @param SolrService $solr
211
     * @return string
212
     */
213 2
    protected function checkSolrSchemaName(SolrService $solr)
214
    {
215
        try {
216 2
            $solrSchemaMessage = $solr->getSchema()->getName();
217
        } catch (\Exception $e) {
218
            $this->responseStatus = Status::ERROR;
219
            $solrSchemaMessage = 'Error determining schema name: ' . $e->getMessage();
220
        }
221
222 2
        return $solrSchemaMessage;
223
    }
224
225
    /**
226
     * Formats the Apache Solr server version number. By default this is going
227
     * to be the simple major.minor.patch-level version. Custom Builds provide
228
     * more information though, in case of custom builds, their complete
229
     * version will be added, too.
230
     *
231
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
232
     * @return string formatted short version number, in case of custom builds followed by the complete version number
233
     */
234 1
    protected function formatSolrVersion($solrVersion)
235
    {
236 1
        $explodedSolrVersion = explode('.', $solrVersion);
237
238 1
        $shortSolrVersion = $explodedSolrVersion[0]
239 1
            . '.' . $explodedSolrVersion[1]
240 1
            . '.' . $explodedSolrVersion[2];
241
242 1
        $formattedSolrVersion = $shortSolrVersion;
243
244 1
        if ($solrVersion != $shortSolrVersion) {
245
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
246
        }
247
248 1
        return $formattedSolrVersion;
249
    }
250
}
251