Passed
Push — master ( 7d0241...87493b )
by Timo
04:16
created

SolrStatus::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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