Passed
Push — master ( 42d2d3...8f9ec7 )
by Timo
69:32 queued 48:37
created

SolrVersionStatus::getCleanSolrVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0156

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 1.0156
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\System\Solr\SolrConnection;
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 1
            $coreAdmin = $solrConnection->getAdminService();
63
            /** @var $solrConnection SolrConnection */
64 1
            if (!$coreAdmin->ping()) {
65
                $url = $coreAdmin->__toString();
66
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . (string)$url;
67
                $status = GeneralUtility::makeInstance(Status::class, 'Apache Solr Version', 'Not accessible', $pingFailedMsg, Status::ERROR);
68
                $reports[] = $status;
69
                continue;
70
            }
71
72 1
            $solrVersion = $coreAdmin->getSolrServerVersion();
73 1
            $isOutdatedVersion = version_compare($this->getCleanSolrVersion($solrVersion), self::REQUIRED_SOLR_VERSION, '<');
74
75 1
            if (!$isOutdatedVersion) {
76 1
                continue;
77
            }
78
79
            $formattedVersion = $this->formatSolrVersion($solrVersion);
80
            $variables = ['requiredVersion' => self::REQUIRED_SOLR_VERSION, 'currentVersion' => $formattedVersion, 'solr' => $coreAdmin];
81
            $report = $this->getRenderedReport('SolrVersionStatus.html', $variables);
82
            $status = GeneralUtility::makeInstance(Status::class, 'Apache Solr Version', 'Outdated, Unsupported', $report, Status::ERROR);
83
84
            $reports[] = $status;
85
        }
86
87 1
        return $reports;
88
    }
89
90
    /**
91
     * Gets the clean Solr version in case of a custom build which may have
92
     * additional information in the version string.
93
     *
94
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
95
     * @return string Clean Solr version number: mayor.minor.patchlevel
96
     */
97 1
    protected function getCleanSolrVersion($solrVersion)
98
    {
99 1
        $explodedSolrVersion = explode('.', $solrVersion);
100
101 1
        $shortSolrVersion = $explodedSolrVersion[0]
102 1
            . '.' . $explodedSolrVersion[1]
103 1
            . '.' . $explodedSolrVersion[2];
104
105 1
        return $shortSolrVersion;
106
    }
107
108
    /**
109
     * Formats the Apache Solr server version number. By default this is going
110
     * to be the simple major.minor.patch-level version. Custom Builds provide
111
     * more information though, in case of custom builds, their complete
112
     * version will be added, too.
113
     *
114
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
115
     * @return string formatted short version number, in case of custom builds followed by the complete version number
116
     */
117
    protected function formatSolrVersion($solrVersion)
118
    {
119
        $shortSolrVersion = $this->getCleanSolrVersion($solrVersion);
120
        $formattedSolrVersion = $shortSolrVersion;
121
122
        if ($solrVersion != $shortSolrVersion) {
123
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
124
        }
125
126
        return $formattedSolrVersion;
127
    }
128
}
129