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