Passed
Push — master ( 847fb8...bb8f79 )
by Timo
24:45
created

SchemaStatus   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 29.4%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 60
ccs 10
cts 34
cp 0.294
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getStatus() 0 38 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Report;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\ConnectionManager;
28
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Reports\Status;
31
32
/**
33
 * Provides an status report about which schema version is used and checks
34
 * whether it fits the recommended version shipping with the extension.
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class SchemaStatus extends AbstractSolrStatus
39
{
40
41
    /**
42
     * The schema name property is constructed as follows:
43
     *
44
     * tx_solr  - The extension key
45
     * x-y-z    - The extension version this schema is meant to work with
46
     * YYYYMMDD - The date the schema file was changed the last time
47
     *
48
     * Must be updated when changing the schema.
49
     *
50
     * @var string
51
     */
52
    const RECOMMENDED_SCHEMA_VERSION = 'tx_solr-8-0-0--20171020';
53
54
    /**
55
     * Compiles a collection of schema version checks against each configured
56
     * Solr server. Only adds an entry if a schema other than the
57
     * recommended one was found.
58
     *
59
     */
60 1
    public function getStatus()
61
    {
62 1
        $reports = [];
63 1
        $solrConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();
64
65 1
        foreach ($solrConnections as $solrConnection) {
66 1
            $adminService = $solrConnection->getAdminService();
67
            /** @var $solrConnection SolrConnection */
68 1
            if (!$adminService->ping()) {
69
                $url = $adminService->__toString();
70
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . (string)$url;
71
                $status = GeneralUtility::makeInstance(
72
                    Status::class,
73
                    /** @scrutinizer ignore-type */ 'Apache Solr Version',
74
                    /** @scrutinizer ignore-type */ 'Not accessible',
75
                    /** @scrutinizer ignore-type */ $pingFailedMsg,
76
                    /** @scrutinizer ignore-type */ Status::ERROR
77
                );
78
                $reports[] = $status;
79
                continue;
80
            }
81
82 1
            $isWrongSchema = $adminService->getSchema()->getName() != self::RECOMMENDED_SCHEMA_VERSION;
83 1
            if ($isWrongSchema) {
84
                $variables = ['solr' => $adminService, 'recommendedVersion' => self::RECOMMENDED_SCHEMA_VERSION];
85
                $report = $this->getRenderedReport('SchemaStatus.html', $variables);
86
                $status = GeneralUtility::makeInstance(
87
                    Status::class,
88
                    /** @scrutinizer ignore-type */ 'Schema Version',
89
                    /** @scrutinizer ignore-type */ 'Unsupported Schema',
90
                    /** @scrutinizer ignore-type */ $report,
91
                    /** @scrutinizer ignore-type */ Status::WARNING
92
                );
93 1
                $reports[] = $status;
94
            }
95
        }
96
97 1
        return $reports;
98
    }
99
}
100