Passed
Pull Request — master (#1287)
by Sascha
21:29
created

SchemaStatus   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 36.36%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 49
ccs 8
cts 22
cp 0.3636
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getStatus() 0 26 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 2 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\SolrService;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Fluid\View\StandaloneView;
31
use TYPO3\CMS\Reports\Status;
32
33
/**
34
 * Provides an status report about which schema version is used and checks
35
 * whether it fits the recommended version shipping with the extension.
36
 *
37
 * @author Ingo Renner <[email protected]>
38
 */
39
class SchemaStatus extends AbstractSolrStatus
40
{
41
42
    /**
43
     * The schema name property is constructed as follows:
44
     *
45
     * tx_solr  - The extension key
46
     * x-y-z    - The extension version this schema is meant to work with
47
     * YYYYMMDD - The date the schema file was changed the last time
48
     *
49
     * Must be updated when changing the schema.
50
     *
51
     * @var string
52
     */
53
    const RECOMMENDED_SCHEMA_VERSION = 'tx_solr-6-1-0--20170206';
54
55
    /**
56
     * Compiles a collection of schema version checks against each configured
57
     * Solr server. Only adds an entry if a schema other than the
58
     * recommended one was found.
59
     *
60
     */
61 1
    public function getStatus()
62
    {
63 1
        $reports = [];
64 1
        $solrConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();
65
66 1
        foreach ($solrConnections as $solrConnection) {
67
            /** @var $solrConnection SolrService */
68 1
            if (!$solrConnection->ping()) {
69
                $url = $solrConnection->__toString();
70
                $pingFailedMsg = 'Could not ping solr server, can not check version ' . (string)$url;
71
                $status = GeneralUtility::makeInstance(Status::class, 'Apache Solr Version', 'Not accessible', $pingFailedMsg, Status::ERROR);
72
                $reports[] = $status;
73
                continue;
74
            }
75
76 1
            $isWrongSchema = $solrConnection->getSchema()->getName() != self::RECOMMENDED_SCHEMA_VERSION;
77 1
            if ($isWrongSchema) {
78
                $variables = ['solr' => $solrConnection, 'recommendedVersion' => self::RECOMMENDED_SCHEMA_VERSION];
79
                $report = $this->getRenderedReport('SchemaStatus.html', $variables);
80
                $status = GeneralUtility::makeInstance(Status::class, 'Schema Version', 'Unsupported Schema', $report, Status::WARNING);
81
                $reports[] = $status;
82
            }
83
        }
84
85 1
        return $reports;
86
    }
87
}
88