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

SchemaStatus::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 which schema version is used and checks
30
 * whether it fits the recommended version shipping with the extension.
31
 *
32
 * @author Ingo Renner <[email protected]>
33
 */
34
class SchemaStatus extends AbstractSolrStatus
35
{
36
    /**
37
     * The schema name property is constructed as follows:
38
     *
39
     * tx_solr  - The extension key
40
     * x-y-z    - The extension version this schema is meant to work with
41
     * YYYYMMDD - The date the schema file was changed the last time
42
     *
43
     * Must be updated when changing the schema.
44
     *
45
     * @var string
46
     */
47
    const RECOMMENDED_SCHEMA_VERSION = 'tx_solr-11-5-0--20211001';
48
49
    /**
50
     * Compiles a collection of schema version checks against each configured
51
     * Solr server. Only adds an entry if a schema other than the
52
     * recommended one was found.
53
     *
54
     * @return array
55
     *
56
     * @throws DBALDriverException
57
     * @throws Throwable
58
     */
59
    public function getStatus(): array
60
    {
61
        $reports = [];
62
        /** @var $connectionManager ConnectionManager */
63
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
64
        $solrConnections = $connectionManager->getAllConnections();
65
66
        foreach ($solrConnections as $solrConnection) {
67
            $adminService = $solrConnection->getAdminService();
68
            /** @var $solrConnection SolrConnection */
69
            if (!$adminService->ping()) {
70
                $url = $adminService->__toString();
71
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . $url;
72
                $status = GeneralUtility::makeInstance(
73
                    Status::class,
74
                    /** @scrutinizer ignore-type */
75
                    'Apache Solr Version',
76
                    /** @scrutinizer ignore-type */
77
                    'Not accessible',
78
                    /** @scrutinizer ignore-type */
79
                    $pingFailedMsg,
80
                    /** @scrutinizer ignore-type */
81
                    ContextualFeedbackSeverity::ERROR
82
                );
83
                $reports[] = $status;
84
                continue;
85
            }
86
87
            $isWrongSchema = $adminService->getSchema()->getName() != self::RECOMMENDED_SCHEMA_VERSION;
88
            if ($isWrongSchema) {
89
                $variables = ['solr' => $adminService, 'recommendedVersion' => self::RECOMMENDED_SCHEMA_VERSION];
90
                $report = $this->getRenderedReport('SchemaStatus.html', $variables);
91
                $status = GeneralUtility::makeInstance(
92
                    Status::class,
93
                    /** @scrutinizer ignore-type */
94
                    'Schema Version',
95
                    /** @scrutinizer ignore-type */
96
                    'Unsupported Schema',
97
                    /** @scrutinizer ignore-type */
98
                    $report,
99
                    /** @scrutinizer ignore-type */
100
                    ContextualFeedbackSeverity::WARNING
101
                );
102
                $reports[] = $status;
103
            }
104
        }
105
106
        return $reports;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function getLabel(): string
113
    {
114
        return 'solr/schema';
115
    }
116
}
117