Failed Conditions
Push — release-11.5.x ( 71e6eb...3bfdb1 )
by Markus
27:37
created

SolrVersionStatus::getSupportedSolrVersions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\Utility\ExtensionManagementUtility;
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
     * Compiles a version check against each configured Solr server.
38
     *
39
     * @noinspection PhpMissingReturnTypeInspection see {@link \TYPO3\CMS\Reports\StatusProviderInterface::getStatus()}
40
     *
41
     * @throws DBALDriverException
42
     * @throws Throwable
43
     */
44 2
    public function getStatus()
45
    {
46 2
        $reports = [];
47 2
        $solrConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();
48
49 2
        foreach ($solrConnections as $solrConnection) {
50 2
            $coreAdmin = $solrConnection->getAdminService();
51
            /** @var $solrConnection SolrConnection */
52 2
            if (!$coreAdmin->ping()) {
53
                $url = $coreAdmin->__toString();
54
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . $url;
55
                $status = GeneralUtility::makeInstance(
56
                    Status::class,
57
                    'Apache Solr Version',
58
                    'Not accessible',
59
                    $pingFailedMsg,
60
                    Status::ERROR
61
                );
62
                $reports[] = $status;
63
                continue;
64
            }
65
66 2
            $solrVersion = $coreAdmin->getSolrServerVersion();
67 2
            $supportedSolrVersions = $this->getSupportedSolrVersions();
68 2
            $isSupported = in_array($this->getCleanSolrVersion($solrVersion), $supportedSolrVersions);
69
70 2
            if ($isSupported) {
71 2
                $reports[] = GeneralUtility::makeInstance(
72 2
                    Status::class,
73 2
                    'Apache Solr Version',
74 2
                    'OK',
75 2
                    'Version of ' . $coreAdmin->__toString() . ' is ok: ' . $solrVersion,
76 2
                    Status::OK
77 2
                );
78 2
                continue;
79
            }
80
81
            $formattedVersion = $this->formatSolrVersion($solrVersion);
82
            $variables = [
83
                'supportedSolrVersions' => $supportedSolrVersions,
84
                'currentVersion' => $formattedVersion,
85
                'solr' => $coreAdmin,
86
            ];
87
            $report = $this->getRenderedReport('SolrVersionStatus.html', $variables);
88
            $status = GeneralUtility::makeInstance(
89
                Status::class,
90
                'Apache Solr Version',
91
                'Unsupported',
92
                $report,
93
                Status::ERROR
94
            );
95
96
            $reports[] = $status;
97
        }
98
99 2
        return $reports;
100
    }
101
102 2
    protected function getSupportedSolrVersions(): array
103
    {
104 2
        $composerContents = file_get_contents(ExtensionManagementUtility::extPath('solr') . 'composer.json');
105 2
        $composerConfiguration = json_decode($composerContents, true, 25, JSON_OBJECT_AS_ARRAY);
106 2
        return $composerConfiguration['extra']['TYPO3-Solr']['version-matrix']['Apache-Solr'] ?? [];
107
    }
108
109
    /**
110
     * Gets the clean Solr version in case of a custom build which may have
111
     * additional information in the version string.
112
     *
113
     * @param string $solrVersion Unformatted Apache Solr version number a provided by Solr.
114
     * @return string Clean Solr version number: mayor.minor.patch-level
115
     */
116 2
    protected function getCleanSolrVersion(string $solrVersion): string
117
    {
118 2
        $explodedSolrVersion = explode('.', $solrVersion);
119
120 2
        return $explodedSolrVersion[0]
121 2
            . '.' . $explodedSolrVersion[1]
122 2
            . '.' . $explodedSolrVersion[2];
123
    }
124
125
    /**
126
     * Formats the Apache Solr server version number. By default, this is going
127
     * to be the simple major.minor.patch-level version. Custom Builds provide
128
     * more information though, in case of custom-builds, their complete
129
     * version will be added, too.
130
     *
131
     * @param string $solrVersion Unformatted Apache Solr version number a provided by Solr.
132
     * @return string formatted short version number, in case of custom-builds followed by the complete version number
133
     */
134
    protected function formatSolrVersion(string $solrVersion): string
135
    {
136
        $shortSolrVersion = $this->getCleanSolrVersion($solrVersion);
137
        $formattedSolrVersion = $shortSolrVersion;
138
139
        if ($solrVersion != $shortSolrVersion) {
140
            $formattedSolrVersion .= ' (' . $solrVersion . ')';
141
        }
142
143
        return $formattedSolrVersion;
144
    }
145
}
146