Passed
Pull Request — release-11.2.x (#3177)
by Rafael
19:03 queued 08:22
created

SolrStatus::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 1
cts 2
cp 0.5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1.125
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
    public function __construct(SiteRepository $siteRepository = null, ConnectionManager $connectionManager = null)
78
    {
79 2
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
80
        $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
81 2
    }
82 2
83 2
    /**
84
     * Compiles a collection of status checks against each configured Solr server.
85
     *
86
     */
87
    public function getStatus()
88
    {
89 2
        $reports = [];
90
        foreach ($this->siteRepository->getAvailableSites() as $site) {
91 2
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
92 2
                $reports[] = $this->getConnectionStatus($solrConfiguration);
93 2
            }
94 2
        }
95
96
        return $reports;
97
    }
98 2
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
    protected function getConnectionStatus(array $solrConnection)
106
    {
107 2
        $header = 'Your site has contacted the Apache Solr server.';
108
        $this->responseStatus = Status::OK;
109 2
110 2
        $solrAdmin = $this->connectionManager
111
            ->getSolrConnectionForNodes($solrConnection['read'], $solrConnection['write'])
112 2
            ->getAdminService();
113 2
114 2
        $solrVersion = $this->checkSolrVersion($solrAdmin);
115
        $accessFilter = $this->checkAccessFilter($solrAdmin);
116 2
        $pingTime = $this->checkPingTime($solrAdmin);
117 2
        $configName = $this->checkSolrConfigName($solrAdmin);
118 2
        $schemaName = $this->checkSolrSchemaName($solrAdmin);
119 2
120 2
        if ($this->responseStatus !== Status::OK) {
121
            $header = 'Failed contacting the Solr server.';
122 2
        }
123 1
124
        $variables = [
125
            'header' => $header,
126
            '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 2
        ];
134 2
135
        $report = $this->getRenderedReport('SolrStatus.html', $variables);
136
        return GeneralUtility::makeInstance(
137 2
            Status::class,
138 2
            /** @scrutinizer ignore-type */ 'Apache Solr',
139 2
            /** @scrutinizer ignore-type */ '',
140 2
            /** @scrutinizer ignore-type */ $report,
141 2
            /** @scrutinizer ignore-type */ $this->responseStatus
142 2
        );
143 2
    }
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
    protected function checkSolrVersion(SolrAdminService $solr)
152
    {
153 2
        try {
154
            $solrVersion = $this->formatSolrVersion($solr->getSolrServerVersion());
155
        } catch (\Exception $e) {
156 2
            $this->responseStatus = Status::ERROR;
157
            $solrVersion = 'Error getting solr version: ' . $e->getMessage();
158
        }
159
160
        return $solrVersion;
161
    }
162 2
163
    /**
164
     * Checks the access filter setup and adds it to the report.
165
     *
166
     * @param SolrAdminService $solrAdminService
167
     * @return string
168
     */
169
    protected function checkAccessFilter(SolrAdminService $solrAdminService)
170
    {
171 2
        try {
172
            $accessFilterPluginStatus = GeneralUtility::makeInstance(AccessFilterPluginInstalledStatus::class);
173
            $accessFilterPluginVersion = $accessFilterPluginStatus->getInstalledPluginVersion($solrAdminService);
174 2
            $accessFilterMessage = $accessFilterPluginVersion;
175 2
        } catch (\Exception $e) {
176 2
            $this->responseStatus = Status::ERROR;
177
            $accessFilterMessage = 'Error getting access filter: ' . $e->getMessage();
178
        }
179
        return $accessFilterMessage;
180
    }
181 2
182
    /**
183
     * Checks the ping time and adds it to the report.
184
     *
185
     * @param SolrAdminService $solrAdminService
186
     * @return string
187
     */
188
    protected function checkPingTime(SolrAdminService $solrAdminService)
189
    {
190 2
        try {
191
            $pingQueryTime = $solrAdminService->getPingRoundTripRuntime();
192
            $pingMessage = (int)$pingQueryTime . ' ms';
193 2
        } catch (PingFailedException $e) {
194 1
            $this->responseStatus = Status::ERROR;
195 1
            $pingMessage = 'Ping error: ' . $e->getMessage();
196 1
        }
197 1
        return $pingMessage;
198
    }
199 2
200
    /**
201
     * Checks the solr config name and adds it to the report.
202
     *
203
     * @param SolrAdminService $solrAdminService
204
     * @return string
205
     */
206
    protected function checkSolrConfigName(SolrAdminService $solrAdminService)
207
    {
208 2
        try {
209
            $solrConfigMessage = $solrAdminService->getSolrconfigName();
210
        } catch (\Exception $e) {
211 2
            $this->responseStatus = Status::ERROR;
212 1
            $solrConfigMessage = 'Error determining solr config: ' . $e->getMessage();
213 1
        }
214 1
215
        return $solrConfigMessage;
216
    }
217 2
218
    /**
219
     * Checks the solr schema name and adds it to the report.
220
     *
221
     * @param SolrAdminService $solrAdminService
222
     * @return string
223
     */
224
    protected function checkSolrSchemaName(SolrAdminService $solrAdminService)
225
    {
226 2
        try {
227
            $solrSchemaMessage = $solrAdminService->getSchema()->getName();
228
        } catch (\Exception $e) {
229 2
            $this->responseStatus = Status::ERROR;
230
            $solrSchemaMessage = 'Error determining schema name: ' . $e->getMessage();
231
        }
232
233
        return $solrSchemaMessage;
234
    }
235 2
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
    protected function formatSolrVersion($solrVersion)
246
    {
247 2
        $explodedSolrVersion = explode('.', $solrVersion);
248
249 2
        $shortSolrVersion = $explodedSolrVersion[0]
250
            . '.' . $explodedSolrVersion[1]
251 2
            . '.' . $explodedSolrVersion[2];
252 2
253 2
        $formattedSolrVersion = $shortSolrVersion;
254
255 2
        if ($solrVersion != $shortSolrVersion) {
256
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
257 2
        }
258 1
259
        return $formattedSolrVersion;
260
    }
261
}
262