Passed
Pull Request — release-11.5.x (#3206)
by Michael
41:07
created

SolrVersionStatus::getStatus()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.8098

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 44
ccs 11
cts 25
cp 0.44
rs 9.44
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 6.8098
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 3 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\Reports\Status;
32
33
/**
34
 * Provides a status report about whether the installed Solr version matches
35
 * the required version.
36
 *
37
 * @author Stefan Sprenger <[email protected]>
38
 */
39
class SolrVersionStatus extends AbstractSolrStatus
40
{
41
42
    /**
43
     * Required Solr version. The version that gets installed when using the
44
     * provided install script EXT:solr/Resources/Private/Install/install-solr.sh
45
     *
46
     * @var string
47
     */
48
    const REQUIRED_SOLR_VERSION = '8.11.1';
49
50
    /**
51
     * Compiles a version check against each configured Solr server.
52
     *
53
     */
54 1
    public function getStatus()
55
    {
56 1
        $reports = [];
57 1
        $solrConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();
58
59 1
        foreach ($solrConnections as $solrConnection) {
60 1
            $coreAdmin = $solrConnection->getAdminService();
61
            /** @var $solrConnection SolrConnection */
62 1
            if (!$coreAdmin->ping()) {
63
                $url = $coreAdmin->__toString();
64
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . (string)$url;
65
                $status = GeneralUtility::makeInstance(
66
                    Status::class,
67
                    /** @scrutinizer ignore-type */ 'Apache Solr Version',
68
                    /** @scrutinizer ignore-type */ 'Not accessible',
69
                    /** @scrutinizer ignore-type */ $pingFailedMsg,
70
                    /** @scrutinizer ignore-type */ Status::ERROR
71
                );
72
                $reports[] = $status;
73
                continue;
74
            }
75
76 1
            $solrVersion = $coreAdmin->getSolrServerVersion();
77 1
            $isOutdatedVersion = version_compare($this->getCleanSolrVersion($solrVersion), self::REQUIRED_SOLR_VERSION, '<');
78
79 1
            if (!$isOutdatedVersion) {
80 1
                continue;
81
            }
82
83
            $formattedVersion = $this->formatSolrVersion($solrVersion);
84
            $variables = ['requiredVersion' => self::REQUIRED_SOLR_VERSION, 'currentVersion' => $formattedVersion, 'solr' => $coreAdmin];
85
            $report = $this->getRenderedReport('SolrVersionStatus.html', $variables);
86
            $status = GeneralUtility::makeInstance(
87
                Status::class,
88
                /** @scrutinizer ignore-type */ 'Apache Solr Version',
89
                /** @scrutinizer ignore-type */ 'Outdated, Unsupported',
90
                /** @scrutinizer ignore-type */ $report,
91
                /** @scrutinizer ignore-type */ Status::ERROR
92
            );
93
94
            $reports[] = $status;
95
        }
96
97 1
        return $reports;
98
    }
99
100
    /**
101
     * Gets the clean Solr version in case of a custom build which may have
102
     * additional information in the version string.
103
     *
104
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
105
     * @return string Clean Solr version number: mayor.minor.patchlevel
106
     */
107 1
    protected function getCleanSolrVersion($solrVersion)
108
    {
109 1
        $explodedSolrVersion = explode('.', $solrVersion);
110
111 1
        $shortSolrVersion = $explodedSolrVersion[0]
112 1
            . '.' . $explodedSolrVersion[1]
113 1
            . '.' . $explodedSolrVersion[2];
114
115 1
        return $shortSolrVersion;
116
    }
117
118
    /**
119
     * Formats the Apache Solr server version number. By default this is going
120
     * to be the simple major.minor.patch-level version. Custom Builds provide
121
     * more information though, in case of custom builds, their complete
122
     * version will be added, too.
123
     *
124
     * @param string $solrVersion Unformatted Apache Solr version number as provided by Solr.
125
     * @return string formatted short version number, in case of custom builds followed by the complete version number
126
     */
127
    protected function formatSolrVersion($solrVersion)
128
    {
129
        $shortSolrVersion = $this->getCleanSolrVersion($solrVersion);
130
        $formattedSolrVersion = $shortSolrVersion;
131
132
        if ($solrVersion != $shortSolrVersion) {
133
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
134
        }
135
136
        return $formattedSolrVersion;
137
    }
138
}
139