Passed
Push — task/3376-TYPO3_12_compatibili... ( dc7e5a...379ca7 )
by Rafael
40:48
created

SolrVersionStatus::formatSolrVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Report;
19
20
use ApacheSolrForTypo3\Solr\ConnectionManager;
21
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
22
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
23
use Throwable;
24
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Reports\Status;
27
28
/**
29
 * Provides a status report about whether the installed Solr version matches
30
 * the required version.
31
 *
32
 * @author Stefan Sprenger <[email protected]>
33
 */
34
class SolrVersionStatus extends AbstractSolrStatus
35
{
36
    /**
37
     * Required Solr version. The version that gets installed when using the
38
     * provided install-script EXT:solr/Resources/Private/Install/install-solr.sh
39
     *
40
     * @var string
41
     */
42
    const REQUIRED_SOLR_VERSION = '8.11.1';
43
44
    /**
45
     * Compiles a version check against each configured Solr server.
46
     *
47
     * @return array
48
     *
49
     * @throws DBALDriverException
50
     * @throws Throwable
51
     */
52
    public function getStatus(): array
53
    {
54
        $reports = [];
55
        $solrConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();
56
57
        foreach ($solrConnections as $solrConnection) {
58
            $coreAdmin = $solrConnection->getAdminService();
59
            /** @var $solrConnection SolrConnection */
60
            if (!$coreAdmin->ping()) {
61
                $url = $coreAdmin->__toString();
62
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . $url;
63
                $status = GeneralUtility::makeInstance(
64
                    Status::class,
65
                    /** @scrutinizer ignore-type */
66
                    'Apache Solr Version',
67
                    /** @scrutinizer ignore-type */
68
                    'Not accessible',
69
                    /** @scrutinizer ignore-type */
70
                    $pingFailedMsg,
71
                    /** @scrutinizer ignore-type */
72
                    ContextualFeedbackSeverity::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 */
91
                'Apache Solr Version',
92
                /** @scrutinizer ignore-type */
93
                'Outdated, Unsupported',
94
                /** @scrutinizer ignore-type */
95
                $report,
96
                /** @scrutinizer ignore-type */
97
                ContextualFeedbackSeverity::ERROR
98
            );
99
100
            $reports[] = $status;
101
        }
102
103
        return $reports;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function getLabel(): string
110
    {
111
        return 'solr/version';
112
    }
113
114
    /**
115
     * Gets the clean Solr version in case of a custom build which may have
116
     * additional information in the version string.
117
     *
118
     * @param string $solrVersion Unformatted Apache Solr version number a provided by Solr.
119
     * @return string Clean Solr version number: mayor.minor.patch-level
120
     */
121
    protected function getCleanSolrVersion(string $solrVersion): string
122
    {
123
        $explodedSolrVersion = explode('.', $solrVersion);
124
125
        return $explodedSolrVersion[0]
126
            . '.' . $explodedSolrVersion[1]
127
            . '.' . $explodedSolrVersion[2];
128
    }
129
130
    /**
131
     * Formats the Apache Solr server version number. By default, this is going
132
     * to be the simple major.minor.patch-level version. Custom Builds provide
133
     * more information though, in case of custom-builds, their complete
134
     * version will be added, too.
135
     *
136
     * @param string $solrVersion Unformatted Apache Solr version number a provided by Solr.
137
     * @return string formatted short version number, in case of custom-builds followed by the complete version number
138
     */
139
    protected function formatSolrVersion(string $solrVersion): string
140
    {
141
        $shortSolrVersion = $this->getCleanSolrVersion($solrVersion);
142
        $formattedSolrVersion = $shortSolrVersion;
143
144
        if ($solrVersion != $shortSolrVersion) {
145
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
146
        }
147
148
        return $formattedSolrVersion;
149
    }
150
}
151