Completed
Branch master (a6ebf8)
by Timo
03:27
created

SolrVersionStatus::formatSolrVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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