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

SolrVersionStatus::formatSolrVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Report;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Stefan Sprenger <[email protected]>
8
 *  (c) 2012-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\ConnectionManager;
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 a status report about whether the installed Solr version matches
37
 * the required version.
38
 *
39
 * @author Stefan Sprenger <[email protected]>
40
 */
41
class SolrVersionStatus extends AbstractSolrStatus
42
{
43
44
    /**
45
     * Required Solr version. The version that gets installed when using the
46
     * provided install script EXT:solr/Resources/Private/Install/install-solr.sh
47
     *
48
     * @var string
49
     */
50
    const REQUIRED_SOLR_VERSION = '6.3.0';
51
52
    /**
53
     * Compiles a version check against each configured Solr server.
54
     *
55
     */
56 1
    public function getStatus()
57
    {
58 1
        $reports = [];
59 1
        $solrConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();
60
61 1
        foreach ($solrConnections as $solrConnection) {
62
            /** @var $solrConnection SolrService */
63 1
            if (!$solrConnection->ping()) {
64
                $url = $solrConnection->__toString();
65
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . (string)$url;
66
                $status = GeneralUtility::makeInstance(Status::class, 'Apache Solr Version', 'Not accessible', $pingFailedMsg, Status::ERROR);
67
                $reports[] = $status;
68
                continue;
69
            }
70
71 1
            $solrVersion = $solrConnection->getSolrServerVersion();
72 1
            $isOutdatedVersion = version_compare($this->getCleanSolrVersion($solrVersion), self::REQUIRED_SOLR_VERSION, '<');
73
74 1
            if (!$isOutdatedVersion) {
75 1
                continue;
76
            }
77
78
            $formattedVersion = $this->formatSolrVersion($solrVersion);
79
            $variables = ['requiredVersion' => self::REQUIRED_SOLR_VERSION, 'currentVersion' => $formattedVersion, 'solr' => $solrConnection];
80
            $report = $this->getRenderedReport('SolrVersionStatus.html', $variables);
81
            $status = GeneralUtility::makeInstance(Status::class, 'Apache Solr Version', 'Outdated, Unsupported', $report, Status::ERROR);
82
83
            $reports[] = $status;
84
        }
85
86 1
        return $reports;
87
    }
88
89
    /**
90
     * Gets the clean Solr version in case of a custom build which may have
91
     * additional information in the version string.
92
     *
93
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
94
     * @return string Clean Solr version number: mayor.minor.patchlevel
95
     */
96 1
    protected function getCleanSolrVersion($solrVersion)
97
    {
98 1
        $explodedSolrVersion = explode('.', $solrVersion);
99
100 1
        $shortSolrVersion = $explodedSolrVersion[0]
101 1
            . '.' . $explodedSolrVersion[1]
102 1
            . '.' . $explodedSolrVersion[2];
103
104 1
        return $shortSolrVersion;
105
    }
106
107
    /**
108
     * Formats the Apache Solr server version number. By default this is going
109
     * to be the simple major.minor.patch-level version. Custom Builds provide
110
     * more information though, in case of custom builds, their complete
111
     * version will be added, too.
112
     *
113
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
114
     * @return string formatted short version number, in case of custom builds followed by the complete version number
115
     */
116
    protected function formatSolrVersion($solrVersion)
117
    {
118
        $shortSolrVersion = $this->getCleanSolrVersion($solrVersion);
119
        $formattedSolrVersion = $shortSolrVersion;
120
121
        if ($solrVersion != $shortSolrVersion) {
122
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
123
        }
124
125
        return $formattedSolrVersion;
126
    }
127
}
128